casts.c revision 7e6b564d59df6c0594bc3a577f33536850290dec
1// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-checker=core,alpha.core -analyzer-store=region -verify %s
2// RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-checker=core,alpha.core -analyzer-store=region -verify %s
3// expected-no-diagnostics
4
5// Test if the 'storage' region gets properly initialized after it is cast to
6// 'struct sockaddr *'.
7
8typedef unsigned char __uint8_t;
9typedef unsigned int __uint32_t;
10typedef __uint32_t __darwin_socklen_t;
11typedef __uint8_t sa_family_t;
12typedef __darwin_socklen_t socklen_t;
13struct sockaddr { sa_family_t sa_family; };
14struct sockaddr_storage {};
15
16void getsockname();
17
18void f(int sock) {
19  struct sockaddr_storage storage;
20  struct sockaddr* sockaddr = (struct sockaddr*)&storage;
21  socklen_t addrlen = sizeof(storage);
22  getsockname(sock, sockaddr, &addrlen);
23  switch (sockaddr->sa_family) { // no-warning
24  default:
25    ;
26  }
27}
28
29struct s {
30  struct s *value;
31};
32
33void f1(struct s **pval) {
34  int *tbool = ((void*)0);
35  struct s *t = *pval;
36  pval = &(t->value);
37  tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
38  char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
39  if (*tbool == -1) // here load the element region with the correct type 'int'
40    (void)3;
41}
42
43void f2(const char *str) {
44 unsigned char ch, cl, *p;
45
46 p = (unsigned char *)str;
47 ch = *p++; // use cast-to type 'unsigned char' to create element region.
48 cl = *p++;
49 if(!cl)
50    cl = 'a';
51}
52
53// Test cast VariableSizeArray to pointer does not crash.
54void *memcpy(void *, void const *, unsigned long);
55typedef unsigned char Byte;
56void doit(char *data, int len) {
57    if (len) {
58        Byte buf[len];
59        memcpy(buf, data, len);
60    }
61}
62
63// PR 6013 and 6035 - Test that a cast of a pointer to long and then to int does not crash SValuator.
64void pr6013_6035_test(void *p) {
65  unsigned int foo;
66  foo = ((long)(p));
67  (void) foo;
68}
69
70// PR12511 and radar://11215362 - Test that we support SymCastExpr, which represents symbolic int to float cast.
71char ttt(int intSeconds) {
72  double seconds = intSeconds;
73  if (seconds)
74    return 0;
75  return 0;
76}
77
78int foo (int* p) {
79  int y = 0;
80  if (p == 0) {
81    if ((*((void**)&p)) == (void*)0) // Test that the cast to void preserves the symbolic region.
82      return 0;
83    else
84      return 5/y; // This code should be unreachable: no-warning.
85  }
86  return 0;
87}
88