blob: 96c0226a0053c5f8e50bff244d83b1ba40b3d746 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -verify %s
Zhongxing Xuc14f0972009-04-29 01:50:12 +00002
Zhongxing Xu12233fd2009-04-29 05:59:48 +00003// Test if the 'storage' region gets properly initialized after it is cast to
4// 'struct sockaddr *'.
5
Ted Kremeneke95b4392009-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 Friedman7d369cd2009-07-10 20:10:06 +000013
Zhongxing Xu3c3fee02009-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 Xucea65782009-06-18 06:29:10 +000024
25struct s {
26 struct s *value;
27};
28
Mike Stump41ecf6c2009-07-21 18:45:53 +000029void f1(struct s **pval) {
Zhongxing Xucea65782009-06-18 06:29:10 +000030 int *tbool = ((void*)0);
31 struct s *t = *pval;
32 pval = &(t->value);
Zhongxing Xu96924292009-10-14 06:05:09 +000033 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
Zhongxing Xub21175c2009-06-18 06:49:35 +000034 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
Zhongxing Xu96924292009-10-14 06:05:09 +000035 if (*tbool == -1) // here load the element region with the correct type 'int'
Anders Carlsson499de422009-07-30 22:37:41 +000036 (void)3;
Zhongxing Xucea65782009-06-18 06:29:10 +000037}
38
Zhongxing Xucc457622009-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}
Zhongxing Xuf22afe32010-01-05 11:49:21 +000048
49// Test cast VariableSizeArray to pointer does not crash.
50void *memcpy(void *, void const *, unsigned long);
51typedef unsigned char Byte;
52void doit(char *data, int len) {
53 if (len) {
54 Byte buf[len];
55 memcpy(buf, data, len);
56 }
57}