1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.cstring,alpha.unix.cstring,debug.ExprInspection -analyzer-store=region -verify %s
2 // RUN: %clang_analyze_cc1 -DUSE_BUILTINS -analyzer-checker=core,unix.cstring,alpha.unix.cstring,debug.ExprInspection -analyzer-store=region -verify %s
3 // RUN: %clang_analyze_cc1 -DVARIANT -analyzer-checker=core,unix.cstring,alpha.unix.cstring,debug.ExprInspection -analyzer-store=region -verify %s
4 // RUN: %clang_analyze_cc1 -DUSE_BUILTINS -DVARIANT -analyzer-checker=core,unix.cstring,alpha.unix.cstring,debug.ExprInspection -analyzer-store=region -verify %s
5 
6 #include "Inputs/system-header-simulator-cxx.h"
7 #include "Inputs/system-header-simulator-for-malloc.h"
8 
9 // This provides us with four possible mempcpy() definitions.
10 // See also comments in bstring.c.
11 
12 #ifdef USE_BUILTINS
13 #define BUILTIN(f) __builtin_##f
14 #else /* USE_BUILTINS */
15 #define BUILTIN(f) f
16 #endif /* USE_BUILTINS */
17 
18 #ifdef VARIANT
19 
20 #define __mempcpy_chk BUILTIN(__mempcpy_chk)
21 void *__mempcpy_chk(void *__restrict__ s1, const void *__restrict__ s2,
22                     size_t n, size_t destlen);
23 
24 #define mempcpy(a,b,c) __mempcpy_chk(a,b,c,(size_t)-1)
25 
26 #else /* VARIANT */
27 
28 #define mempcpy BUILTIN(mempcpy)
29 void *mempcpy(void *__restrict__ s1, const void *__restrict__ s2, size_t n);
30 
31 #endif /* VARIANT */
32 
33 void clang_analyzer_eval(int);
34 
35 int *testStdCopyInvalidatesBuffer(std::vector<int> v) {
36   int n = v.size();
37   int *buf = (int *)malloc(n * sizeof(int));
38 
39   buf[0] = 66;
40 
41   // Call to copy should invalidate buf.
42   std::copy(v.begin(), v.end(), buf);
43 
44   int i = buf[0];
45 
46   clang_analyzer_eval(i == 66); // expected-warning {{UNKNOWN}}
47 
48   return buf;
49 }
50 
51 int *testStdCopyBackwardInvalidatesBuffer(std::vector<int> v) {
52   int n = v.size();
53   int *buf = (int *)malloc(n * sizeof(int));
54 
55   buf[0] = 66;
56 
57   // Call to copy_backward should invalidate buf.
58   std::copy_backward(v.begin(), v.end(), buf + n);
59 
60   int i = buf[0];
61 
62   clang_analyzer_eval(i == 66); // expected-warning {{UNKNOWN}}
63 
64   return buf;
65 }
66 
67 namespace pr34460 {
68 short a;
69 class b {
70   int c;
71   long g;
72   void d() {
73     int e = c;
74     f += e;
75     mempcpy(f, &a, g);
76   }
77   unsigned *f;
78 };
79 }
80