casts.c revision 65d39251ff57b8e33cf6d3a7fcc6aa1c6f8cdc68
1// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-checker=core.experimental -analyzer-check-objc-mem -analyzer-store=region -verify %s
2// RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-checker=core.experimental -analyzer-check-objc-mem -analyzer-store=region -verify %s
3
4// Test if the 'storage' region gets properly initialized after it is cast to
5// 'struct sockaddr *'.
6
7typedef unsigned char __uint8_t;
8typedef unsigned int __uint32_t;
9typedef __uint32_t __darwin_socklen_t;
10typedef __uint8_t sa_family_t;
11typedef __darwin_socklen_t socklen_t;
12struct sockaddr { sa_family_t sa_family; };
13struct sockaddr_storage {};
14
15void getsockname();
16
17void 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
28struct s {
29  struct s *value;
30};
31
32void 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
42void 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.
53void *memcpy(void *, void const *, unsigned long);
54typedef unsigned char Byte;
55void 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.
63void pr6013_6035_test(void *p) {
64  unsigned int foo;
65  foo = ((long)(p));
66  (void) foo;
67}
68