Clang Project

clang_source_code/test/Analysis/bsd-string.c
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.cstring.NullArg,alpha.unix.cstring,debug.ExprInspection -analyzer-store=region -verify %s
2
3#define NULL ((void *)0)
4
5typedef __typeof(sizeof(int)) size_t;
6size_t strlcpy(char *dst, const char *src, size_t n);
7size_t strlcat(char *dst, const char *src, size_t n);
8void clang_analyzer_eval(int);
9
10void f1() {
11  char overlap[] = "123456789";
12  strlcpy(overlap, overlap + 1, 3); // expected-warning{{Arguments must not be overlapping buffers}}
13}
14
15void f2() {
16  char buf[5];
17  strlcpy(buf, "abcd", sizeof(buf)); // expected-no-warning
18  strlcat(buf, "efgh", sizeof(buf)); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
19}
20
21void f3() {
22  char dst[2];
23  const char *src = "abdef";
24  strlcpy(dst, src, 5); // expected-warning{{Size argument is greater than the length of the destination buffer}}
25}
26
27void f4() {
28  strlcpy(NULL, "abcdef", 6); // expected-warning{{Null pointer argument in call to string copy function}}
29}
30
31void f5() {
32  strlcat(NULL, "abcdef", 6); // expected-warning{{Null pointer argument in call to string copy function}}
33}
34
35void f6() {
36  char buf[8];
37  strlcpy(buf, "abc", 3);
38  size_t len = strlcat(buf, "defg", 4);
39  clang_analyzer_eval(len == 7); // expected-warning{{TRUE}}
40}
41
42int f7() {
43  char buf[8];
44  return strlcpy(buf, "1234567", 0); // no-crash
45}
46