1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN: -analyzer-checker=core,alpha.unix.cstring
4 //===----------------------------------------------------------------------===//
5 // mempcpy() using character array. This is the easiest case, as memcpy
6 // intepretrs the dst and src buffers as character arrays (regardless of their
8 //===----------------------------------------------------------------------===//
10 typedef typeof(sizeof(int)) size_t;
12 void clang_analyzer_eval(int);
14 void *memcpy(void *restrict s1
, const void *restrict s2
, size_t n
);
16 void memcpy_array_fully_uninit(char *dst
) {
18 memcpy(dst
, buf
, 10); // expected-warning{{The first element of the 2nd argument is undefined}}
19 // expected-note@-1{{Other elements might also be undefined}}
23 void memcpy_array_partially_uninit(char *dst
) {
26 memcpy(dst
, buf
, 10); // expected-warning{{The last accessed element (at index 9) in the 2nd argument is undefined}}
27 // expected-note@-1{{Other elements might also be undefined}}
31 void memcpy_array_only_init_portion(char *dst
) {
38 void memcpy_array_partially_init_error(char *dst
) {
41 memcpy(dst
, buf
, 2); // expected-warning{{The last accessed element (at index 1) in the 2nd argument is undefined}}
42 // expected-note@-1{{Other elements might also be undefined}}
46 // The interesting case here is that the portion we're copying is initialized,
47 // but not the whole matrix. We need to be careful to extract buf[1], and not
48 // buf when trying to peel region layers off from the source argument.
49 void memcpy_array_from_matrix(char *dst
) {
53 // FIXME: This is a FP -- we mistakenly retrieve the first element of buf,
54 // instead of the first element of buf[1]. getLValueElement simply peels off
55 // another ElementRegion layer, when in this case it really shouldn't.
56 memcpy(dst
, buf
[1], 2); // expected-warning{{The first element of the 2nd argument is undefined}}
57 // expected-note@-1{{Other elements might also be undefined}}
61 //===----------------------------------------------------------------------===//
62 // mempcpy() using non-character arrays.
63 //===----------------------------------------------------------------------===//
65 void *mempcpy(void *restrict s1
, const void *restrict s2
, size_t n
);
67 void memcpy_int_array_fully_init() {
68 int src
[] = {1, 2, 3, 4};
72 p
= mempcpy(dst
, src
, 4 * sizeof(int));
73 clang_analyzer_eval(p
== &dst
[4]);
76 void memcpy_int_array_fully_init2(int *dest
) {
78 memcpy(dest
, t
, sizeof(t
));
81 //===----------------------------------------------------------------------===//
82 // mempcpy() using nonarrays.
83 //===----------------------------------------------------------------------===//
90 void mempcpy_struct_partially_uninit() {
98 // FIXME: Maybe ask UninitializedObjectChecker whether s1 is fully
100 p2
= mempcpy(&s2
, &s1
, sizeof(struct st
));
102 clang_analyzer_eval(p1
== p2
);
105 void mempcpy_struct_fully_uninit() {
109 // FIXME: Maybe ask UninitializedObjectChecker whether s1 is fully
111 mempcpy(&s2
, &s1
, sizeof(struct st
));
114 // Creduced crash. In this case, an symbolicregion is wrapped in an
115 // elementregion for the src argument.
116 void *ga_copy_strings_from_0
;
119 void ga_copy_strings() {
122 memmove(alloc
, ((char **)ga_copy_strings_from_0
)[i
], 1);
125 // Creduced crash. In this case, retrieving the Loc for the first element failed.
126 char mov_mdhd_language_map
[][4] = {};
127 int ff_mov_lang_to_iso639_code
;
128 char *ff_mov_lang_to_iso639_to
;
129 void ff_mov_lang_to_iso639() {
130 memcpy(ff_mov_lang_to_iso639_to
,
131 mov_mdhd_language_map
[ff_mov_lang_to_iso639_code
], 4);