blob: fea76cc082faf0ae966213cc2247de01938bdf3b [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.cstring,alpha.unix.cstring,debug.ExprInspection -analyzer-store=region -verify %s
Artem Dergachevdb65f962017-10-13 20:11:00 +00002// 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
Devin Coughlin9165df12016-02-07 16:55:44 +00005
6#include "Inputs/system-header-simulator-cxx.h"
7#include "Inputs/system-header-simulator-for-malloc.h"
8
Artem Dergachevdb65f962017-10-13 20:11:00 +00009// 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)
21void *__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)
29void *mempcpy(void *__restrict__ s1, const void *__restrict__ s2, size_t n);
30
31#endif /* VARIANT */
32
Devin Coughlin9165df12016-02-07 16:55:44 +000033void clang_analyzer_eval(int);
34
35int *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
51int *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}
Artem Dergachevdb65f962017-10-13 20:11:00 +000066
67namespace pr34460 {
68short a;
69class 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}