blob: 087bd978e11388bc53932f5208fa7f8c2a9d2cab [file] [log] [blame]
Ted Kremenekcdc3a892012-08-24 20:39:55 +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
Andy Gibbs8e8fb3b2012-10-19 12:44:48 +00003// expected-no-diagnostics
Zhongxing Xu2fc32592009-04-29 01:50:12 +00004
Zhongxing Xub1080ed2009-04-29 05:59:48 +00005// Test if the 'storage' region gets properly initialized after it is cast to
6// 'struct sockaddr *'.
7
Ted Kremenekcaac0892009-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 Friedmancb52d282009-07-10 20:10:06 +000015
Chris Lattnere0303582010-01-09 20:43:19 +000016void getsockname();
17
Zhongxing Xuc58e7852009-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 Xu88c675f2009-06-18 06:29:10 +000028
29struct s {
30 struct s *value;
31};
32
Mike Stump95992262009-07-21 18:45:53 +000033void f1(struct s **pval) {
Zhongxing Xu88c675f2009-06-18 06:29:10 +000034 int *tbool = ((void*)0);
35 struct s *t = *pval;
36 pval = &(t->value);
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000037 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
Zhongxing Xu59c03ff2009-06-18 06:49:35 +000038 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000039 if (*tbool == -1) // here load the element region with the correct type 'int'
Anders Carlsson9668b1f2009-07-30 22:37:41 +000040 (void)3;
Zhongxing Xu88c675f2009-06-18 06:29:10 +000041}
42
Zhongxing Xu005f07b2009-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 Xu6607aca2010-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 Xu7b81e8f2010-01-14 03:45:06 +000062
Ted Kremenek33ec2f82010-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 Xu7b81e8f2010-01-14 03:45:06 +000068}
Anna Zaksb3b1ae82012-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 Zaksbeca02f2013-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}