blob: 1cb88e611224d8ca51cf4e44bb1a51cec120cabc [file] [log] [blame]
Ted Kremenek565e4652010-02-05 02:06:54 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -verify %s
2// RUN: %clang_cc1 -triple i386-apple-darwin9 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -verify %s
Zhongxing Xu2fc32592009-04-29 01:50:12 +00003
Zhongxing Xub1080ed2009-04-29 05:59:48 +00004// Test if the 'storage' region gets properly initialized after it is cast to
5// 'struct sockaddr *'.
6
Ted Kremenekcaac0892009-08-20 04:48:23 +00007typedef unsigned char __uint8_t;
8typedef unsigned int __uint32_t;
9typedef __uint32_t __darwin_socklen_t;
10typedef __uint8_t sa_family_t;
11typedef __darwin_socklen_t socklen_t;
12struct sockaddr { sa_family_t sa_family; };
13struct sockaddr_storage {};
Eli Friedmancb52d282009-07-10 20:10:06 +000014
Chris Lattnere0303582010-01-09 20:43:19 +000015void getsockname();
16
Zhongxing Xuc58e7852009-04-28 13:52:13 +000017void f(int sock) {
18 struct sockaddr_storage storage;
19 struct sockaddr* sockaddr = (struct sockaddr*)&storage;
20 socklen_t addrlen = sizeof(storage);
21 getsockname(sock, sockaddr, &addrlen);
22 switch (sockaddr->sa_family) { // no-warning
23 default:
24 ;
25 }
26}
Zhongxing Xu88c675f2009-06-18 06:29:10 +000027
28struct s {
29 struct s *value;
30};
31
Mike Stump95992262009-07-21 18:45:53 +000032void f1(struct s **pval) {
Zhongxing Xu88c675f2009-06-18 06:29:10 +000033 int *tbool = ((void*)0);
34 struct s *t = *pval;
35 pval = &(t->value);
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000036 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
Zhongxing Xu59c03ff2009-06-18 06:49:35 +000037 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000038 if (*tbool == -1) // here load the element region with the correct type 'int'
Anders Carlsson9668b1f2009-07-30 22:37:41 +000039 (void)3;
Zhongxing Xu88c675f2009-06-18 06:29:10 +000040}
41
Zhongxing Xu005f07b2009-06-19 04:51:14 +000042void f2(const char *str) {
43 unsigned char ch, cl, *p;
44
45 p = (unsigned char *)str;
46 ch = *p++; // use cast-to type 'unsigned char' to create element region.
47 cl = *p++;
48 if(!cl)
49 cl = 'a';
50}
Zhongxing Xu6607aca2010-01-05 11:49:21 +000051
52// Test cast VariableSizeArray to pointer does not crash.
53void *memcpy(void *, void const *, unsigned long);
54typedef unsigned char Byte;
55void doit(char *data, int len) {
56 if (len) {
57 Byte buf[len];
58 memcpy(buf, data, len);
59 }
60}
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000061
Ted Kremenek33ec2f82010-01-14 19:47:50 +000062// PR 6013 and 6035 - Test that a cast of a pointer to long and then to int does not crash SValuator.
63void pr6013_6035_test(void *p) {
64 unsigned int foo;
65 foo = ((long)(p));
66 (void) foo;
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000067}