Ted Kremenek | caac089 | 2009-08-20 04:48:23 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -triple x86_64-apple-darwin9 -analyze -checker-cfref -analyzer-store=region --verify %s |
Zhongxing Xu | 2fc3259 | 2009-04-29 01:50:12 +0000 | [diff] [blame] | 2 | |
Zhongxing Xu | b1080ed | 2009-04-29 05:59:48 +0000 | [diff] [blame] | 3 | // Test if the 'storage' region gets properly initialized after it is cast to |
| 4 | // 'struct sockaddr *'. |
| 5 | |
Ted Kremenek | caac089 | 2009-08-20 04:48:23 +0000 | [diff] [blame] | 6 | typedef unsigned char __uint8_t; |
| 7 | typedef unsigned int __uint32_t; |
| 8 | typedef __uint32_t __darwin_socklen_t; |
| 9 | typedef __uint8_t sa_family_t; |
| 10 | typedef __darwin_socklen_t socklen_t; |
| 11 | struct sockaddr { sa_family_t sa_family; }; |
| 12 | struct sockaddr_storage {}; |
Eli Friedman | cb52d28 | 2009-07-10 20:10:06 +0000 | [diff] [blame] | 13 | |
Zhongxing Xu | c58e785 | 2009-04-28 13:52:13 +0000 | [diff] [blame] | 14 | void 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 | } |
Zhongxing Xu | 88c675f | 2009-06-18 06:29:10 +0000 | [diff] [blame] | 24 | |
| 25 | struct s { |
| 26 | struct s *value; |
| 27 | }; |
| 28 | |
Mike Stump | 9599226 | 2009-07-21 18:45:53 +0000 | [diff] [blame] | 29 | void f1(struct s **pval) { |
Zhongxing Xu | 88c675f | 2009-06-18 06:29:10 +0000 | [diff] [blame] | 30 | int *tbool = ((void*)0); |
| 31 | struct s *t = *pval; |
| 32 | pval = &(t->value); |
Zhongxing Xu | 18e7a3d | 2009-10-14 06:05:09 +0000 | [diff] [blame] | 33 | tbool = (int *)pval; // use the cast-to type 'int *' to create element region. |
Zhongxing Xu | 59c03ff | 2009-06-18 06:49:35 +0000 | [diff] [blame] | 34 | char c = (unsigned char) *tbool; // Should use cast-to type to create symbol. |
Zhongxing Xu | 18e7a3d | 2009-10-14 06:05:09 +0000 | [diff] [blame] | 35 | if (*tbool == -1) // here load the element region with the correct type 'int' |
Anders Carlsson | 9668b1f | 2009-07-30 22:37:41 +0000 | [diff] [blame] | 36 | (void)3; |
Zhongxing Xu | 88c675f | 2009-06-18 06:29:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Zhongxing Xu | 005f07b | 2009-06-19 04:51:14 +0000 | [diff] [blame] | 39 | void 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 | } |