casts.c revision e030358cc06e1cbce3c2e00ca67c946f9164b2a8
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 getsockname();
15
16void f(int sock) {
17  struct sockaddr_storage storage;
18  struct sockaddr* sockaddr = (struct sockaddr*)&storage;
19  socklen_t addrlen = sizeof(storage);
20  getsockname(sock, sockaddr, &addrlen);
21  switch (sockaddr->sa_family) { // no-warning
22  default:
23    ;
24  }
25}
26
27struct s {
28  struct s *value;
29};
30
31void f1(struct s **pval) {
32  int *tbool = ((void*)0);
33  struct s *t = *pval;
34  pval = &(t->value);
35  tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
36  char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
37  if (*tbool == -1) // here load the element region with the correct type 'int'
38    (void)3;
39}
40
41void f2(const char *str) {
42 unsigned char ch, cl, *p;
43
44 p = (unsigned char *)str;
45 ch = *p++; // use cast-to type 'unsigned char' to create element region.
46 cl = *p++;
47 if(!cl)
48    cl = 'a';
49}
50
51// Test cast VariableSizeArray to pointer does not crash.
52void *memcpy(void *, void const *, unsigned long);
53typedef unsigned char Byte;
54void doit(char *data, int len) {
55    if (len) {
56        Byte buf[len];
57        memcpy(buf, data, len);
58    }
59}
60