blob: 087bd978e11388bc53932f5208fa7f8c2a9d2cab [file] [log] [blame]
Jordan Rose49f888b2013-04-29 17:23:03 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-checker=core,alpha.core -analyzer-store=region -verify %s
2// RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-checker=core,alpha.core -analyzer-store=region -verify %s
3// expected-no-diagnostics
Zhongxing Xuc14f0972009-04-29 01:50:12 +00004
Zhongxing Xu12233fd2009-04-29 05:59:48 +00005// Test if the 'storage' region gets properly initialized after it is cast to
6// 'struct sockaddr *'.
7
Ted Kremeneke95b4392009-08-20 04:48:23 +00008typedef unsigned char __uint8_t;
9typedef unsigned int __uint32_t;
10typedef __uint32_t __darwin_socklen_t;
11typedef __uint8_t sa_family_t;
12typedef __darwin_socklen_t socklen_t;
13struct sockaddr { sa_family_t sa_family; };
14struct sockaddr_storage {};
Eli Friedman7d369cd2009-07-10 20:10:06 +000015
Chris Lattnerf9895c42010-01-09 20:43:19 +000016void getsockname();
17
Zhongxing Xu3c3fee02009-04-28 13:52:13 +000018void f(int sock) {
19 struct sockaddr_storage storage;
20 struct sockaddr* sockaddr = (struct sockaddr*)&storage;
21 socklen_t addrlen = sizeof(storage);
22 getsockname(sock, sockaddr, &addrlen);
23 switch (sockaddr->sa_family) { // no-warning
24 default:
25 ;
26 }
27}
Zhongxing Xucea65782009-06-18 06:29:10 +000028
29struct s {
30 struct s *value;
31};
32
Mike Stump41ecf6c2009-07-21 18:45:53 +000033void f1(struct s **pval) {
Zhongxing Xucea65782009-06-18 06:29:10 +000034 int *tbool = ((void*)0);
35 struct s *t = *pval;
36 pval = &(t->value);
Zhongxing Xu96924292009-10-14 06:05:09 +000037 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
Zhongxing Xub21175c2009-06-18 06:49:35 +000038 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
Zhongxing Xu96924292009-10-14 06:05:09 +000039 if (*tbool == -1) // here load the element region with the correct type 'int'
Anders Carlsson499de422009-07-30 22:37:41 +000040 (void)3;
Zhongxing Xucea65782009-06-18 06:29:10 +000041}
42
Zhongxing Xucc457622009-06-19 04:51:14 +000043void f2(const char *str) {
44 unsigned char ch, cl, *p;
45
46 p = (unsigned char *)str;
47 ch = *p++; // use cast-to type 'unsigned char' to create element region.
48 cl = *p++;
49 if(!cl)
50 cl = 'a';
51}
Zhongxing Xuf22afe32010-01-05 11:49:21 +000052
53// Test cast VariableSizeArray to pointer does not crash.
54void *memcpy(void *, void const *, unsigned long);
55typedef unsigned char Byte;
56void doit(char *data, int len) {
57 if (len) {
58 Byte buf[len];
59 memcpy(buf, data, len);
60 }
61}
Zhongxing Xu803ade22010-01-14 03:45:06 +000062
Ted Kremenek91df0ec2010-01-14 19:47:50 +000063// PR 6013 and 6035 - Test that a cast of a pointer to long and then to int does not crash SValuator.
64void pr6013_6035_test(void *p) {
65 unsigned int foo;
66 foo = ((long)(p));
67 (void) foo;
Zhongxing Xu803ade22010-01-14 03:45:06 +000068}
Anna Zaksfe1ccee2012-05-10 21:49:52 +000069
70// PR12511 and radar://11215362 - Test that we support SymCastExpr, which represents symbolic int to float cast.
71char ttt(int intSeconds) {
72 double seconds = intSeconds;
73 if (seconds)
74 return 0;
75 return 0;
76}
Anna Zaksfe9c7c82013-02-05 19:52:28 +000077
78int foo (int* p) {
79 int y = 0;
80 if (p == 0) {
81 if ((*((void**)&p)) == (void*)0) // Test that the cast to void preserves the symbolic region.
82 return 0;
83 else
84 return 5/y; // This code should be unreachable: no-warning.
85 }
86 return 0;
87}