blob: ae51ffb69aa5b7fec44e517d2b6fb52bf730b900 [file] [log] [blame]
Ted Kremenekcaac0892009-08-20 04:48:23 +00001// RUN: clang-cc -triple x86_64-apple-darwin9 -analyze -checker-cfref -analyzer-store=region --verify %s
Zhongxing Xu2fc32592009-04-29 01:50:12 +00002
Zhongxing Xub1080ed2009-04-29 05:59:48 +00003// Test if the 'storage' region gets properly initialized after it is cast to
4// 'struct sockaddr *'.
5
Ted Kremenekcaac0892009-08-20 04:48:23 +00006typedef 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 {};
Eli Friedmancb52d282009-07-10 20:10:06 +000013
Zhongxing Xuc58e7852009-04-28 13:52:13 +000014void 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 Xu88c675f2009-06-18 06:29:10 +000024
25struct s {
26 struct s *value;
27};
28
Mike Stump95992262009-07-21 18:45:53 +000029void f1(struct s **pval) {
Zhongxing Xu88c675f2009-06-18 06:29:10 +000030 int *tbool = ((void*)0);
31 struct s *t = *pval;
32 pval = &(t->value);
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000033 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
Zhongxing Xu59c03ff2009-06-18 06:49:35 +000034 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000035 if (*tbool == -1) // here load the element region with the correct type 'int'
Anders Carlsson9668b1f2009-07-30 22:37:41 +000036 (void)3;
Zhongxing Xu88c675f2009-06-18 06:29:10 +000037}
38
Zhongxing Xu005f07b2009-06-19 04:51:14 +000039void 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}