blob: e106f3483290c5f86750f29bf5639c2ff57436e4 [file] [log] [blame]
Daniel Dunbara5728872009-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 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
Chris Lattnere0303582010-01-09 20:43:19 +000014void getsockname();
15
Zhongxing Xuc58e7852009-04-28 13:52:13 +000016void f(int sock) {
17 struct sockaddr_storage storage;
18 struct sockaddr* sockaddr = (struct sockaddr*)&storage;
19 socklen_t addrlen = sizeof(storage);
20 getsockname(sock, sockaddr, &addrlen);
21 switch (sockaddr->sa_family) { // no-warning
22 default:
23 ;
24 }
25}
Zhongxing Xu88c675f2009-06-18 06:29:10 +000026
27struct s {
28 struct s *value;
29};
30
Mike Stump95992262009-07-21 18:45:53 +000031void f1(struct s **pval) {
Zhongxing Xu88c675f2009-06-18 06:29:10 +000032 int *tbool = ((void*)0);
33 struct s *t = *pval;
34 pval = &(t->value);
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000035 tbool = (int *)pval; // use the cast-to type 'int *' to create element region.
Zhongxing Xu59c03ff2009-06-18 06:49:35 +000036 char c = (unsigned char) *tbool; // Should use cast-to type to create symbol.
Zhongxing Xu18e7a3d2009-10-14 06:05:09 +000037 if (*tbool == -1) // here load the element region with the correct type 'int'
Anders Carlsson9668b1f2009-07-30 22:37:41 +000038 (void)3;
Zhongxing Xu88c675f2009-06-18 06:29:10 +000039}
40
Zhongxing Xu005f07b2009-06-19 04:51:14 +000041void f2(const char *str) {
42 unsigned char ch, cl, *p;
43
44 p = (unsigned char *)str;
45 ch = *p++; // use cast-to type 'unsigned char' to create element region.
46 cl = *p++;
47 if(!cl)
48 cl = 'a';
49}
Zhongxing Xu6607aca2010-01-05 11:49:21 +000050
51// Test cast VariableSizeArray to pointer does not crash.
52void *memcpy(void *, void const *, unsigned long);
53typedef unsigned char Byte;
54void doit(char *data, int len) {
55 if (len) {
56 Byte buf[len];
57 memcpy(buf, data, len);
58 }
59}
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000060
61struct pcm_feeder {
62 void *data;
63};
64// Test cast a pointer to long and then to int does not crash SValuator.
65void feed_swaplr (struct pcm_feeder *f)
66{
67 int bps;
68 bps = (long) f->data;
69 (void) bps;
70}