casts.c revision ef74f4c6dcd59b3af1de9d8f613c1caf3e6cb63d
1// RUN: clang -cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -verify %s
2
3// Test if the 'storage' region gets properly initialized after it is cast to
4// 'struct sockaddr *'.
5
6typedef unsigned char __uint8_t;
7typedef unsigned int __uint32_t;
8typedef __uint32_t __darwin_socklen_t;
9typedef __uint8_t sa_family_t;
10typedef __darwin_socklen_t socklen_t;
11struct sockaddr { sa_family_t sa_family; };
12struct sockaddr_storage {};
13
14void f(int sock) {
15  struct sockaddr_storage storage;
16  struct sockaddr* sockaddr = (struct sockaddr*)&storage;
17  socklen_t addrlen = sizeof(storage);
18  getsockname(sock, sockaddr, &addrlen);
19  switch (sockaddr->sa_family) { // no-warning
20  default:
21    ;
22  }
23}
24
25struct s {
26  struct s *value;
27};
28
29void f1(struct s **pval) {
30  int *tbool = ((void*)0);
31  struct s *t = *pval;
32  pval = &(t->value);
33  tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
34  char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
35  if (*tbool == -1) // here load the element region with the correct type 'int'
36    (void)3;
37}
38
39void f2(const char *str) {
40 unsigned char ch, cl, *p;
41
42 p = (unsigned char *)str;
43 ch = *p++; // use cast-to type 'unsigned char' to create element region.
44 cl = *p++;
45 if(!cl)
46    cl = 'a';
47}
48