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