xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/pr_4164.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin9 -analyze -analyzer-checker=core,alpha.core -analyzer-store=region -verify %s
2*f4a2713aSLionel Sambuc // expected-no-diagnostics
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc // PR 4164: http://llvm.org/bugs/show_bug.cgi?id=4164
5*f4a2713aSLionel Sambuc //
6*f4a2713aSLionel Sambuc // Eventually this should be pulled into misc-ps.m.  This is in a separate test
7*f4a2713aSLionel Sambuc // file for now to play around with the specific issues for BasicStoreManager
8*f4a2713aSLionel Sambuc // and StoreManager (i.e., we can make a copy of this file for either
9*f4a2713aSLionel Sambuc // StoreManager should one start to fail in the near future).
10*f4a2713aSLionel Sambuc //
11*f4a2713aSLionel Sambuc // The basic issue is that the VarRegion for 'size' is casted to (char*),
12*f4a2713aSLionel Sambuc // resulting in an ElementRegion.  'getsockopt' is an unknown function that
13*f4a2713aSLionel Sambuc // takes a void*, which means the ElementRegion should get stripped off.
14*f4a2713aSLionel Sambuc typedef unsigned int __uint32_t;
15*f4a2713aSLionel Sambuc typedef __uint32_t __darwin_socklen_t;
16*f4a2713aSLionel Sambuc typedef __darwin_socklen_t socklen_t;
17*f4a2713aSLionel Sambuc int getsockopt(int, int, int, void * restrict, socklen_t * restrict);
18*f4a2713aSLionel Sambuc 
test1()19*f4a2713aSLionel Sambuc int test1() {
20*f4a2713aSLionel Sambuc   int s = -1;
21*f4a2713aSLionel Sambuc   int size;
22*f4a2713aSLionel Sambuc   socklen_t size_len = sizeof(size);
23*f4a2713aSLionel Sambuc   if (getsockopt(s, 0xffff, 0x1001, (char *)&size, &size_len) < 0)
24*f4a2713aSLionel Sambuc           return -1;
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc   return size; // no-warning
27*f4a2713aSLionel Sambuc }
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc // Similar case: instead of passing a 'void*', we pass 'char*'.  In this
30*f4a2713aSLionel Sambuc // case we pass an ElementRegion to the invalidation logic.  Since it is
31*f4a2713aSLionel Sambuc // an ElementRegion that just layers on top of another typed region and the
32*f4a2713aSLionel Sambuc // ElementRegion itself has elements whose type are integral (essentially raw
33*f4a2713aSLionel Sambuc // data) we strip off the ElementRegion when doing the invalidation.
34*f4a2713aSLionel Sambuc int takes_charptr(char* p);
test2()35*f4a2713aSLionel Sambuc int test2() {
36*f4a2713aSLionel Sambuc   int size;
37*f4a2713aSLionel Sambuc   if (takes_charptr((char*)&size))
38*f4a2713aSLionel Sambuc     return -1;
39*f4a2713aSLionel Sambuc   return size; // no-warning
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42