Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-checker=core,experimental.core -analyzer-store=region -verify %s
      2 // RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-checker=core,experimental.core -analyzer-store=region -verify %s
      3 
      4 // Test if the 'storage' region gets properly initialized after it is cast to
      5 // 'struct sockaddr *'.
      6 
      7 typedef unsigned char __uint8_t;
      8 typedef unsigned int __uint32_t;
      9 typedef __uint32_t __darwin_socklen_t;
     10 typedef __uint8_t sa_family_t;
     11 typedef __darwin_socklen_t socklen_t;
     12 struct sockaddr { sa_family_t sa_family; };
     13 struct sockaddr_storage {};
     14 
     15 void getsockname();
     16 
     17 void f(int sock) {
     18   struct sockaddr_storage storage;
     19   struct sockaddr* sockaddr = (struct sockaddr*)&storage;
     20   socklen_t addrlen = sizeof(storage);
     21   getsockname(sock, sockaddr, &addrlen);
     22   switch (sockaddr->sa_family) { // no-warning
     23   default:
     24     ;
     25   }
     26 }
     27 
     28 struct s {
     29   struct s *value;
     30 };
     31 
     32 void f1(struct s **pval) {
     33   int *tbool = ((void*)0);
     34   struct s *t = *pval;
     35   pval = &(t->value);
     36   tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
     37   char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
     38   if (*tbool == -1) // here load the element region with the correct type 'int'
     39     (void)3;
     40 }
     41 
     42 void f2(const char *str) {
     43  unsigned char ch, cl, *p;
     44 
     45  p = (unsigned char *)str;
     46  ch = *p++; // use cast-to type 'unsigned char' to create element region.
     47  cl = *p++;
     48  if(!cl)
     49     cl = 'a';
     50 }
     51 
     52 // Test cast VariableSizeArray to pointer does not crash.
     53 void *memcpy(void *, void const *, unsigned long);
     54 typedef unsigned char Byte;
     55 void doit(char *data, int len) {
     56     if (len) {
     57         Byte buf[len];
     58         memcpy(buf, data, len);
     59     }
     60 }
     61 
     62 // PR 6013 and 6035 - Test that a cast of a pointer to long and then to int does not crash SValuator.
     63 void pr6013_6035_test(void *p) {
     64   unsigned int foo;
     65   foo = ((long)(p));
     66   (void) foo;
     67 }
     68