llvm-project/clang/test/Analysis/null-deref-ps-region.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
1.8 KiB
C
Raw Normal View History

// RUN: %clang_analyze_cc1 -verify %s -std=gnu99 \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=alpha.core \
// RUN: -analyzer-checker=unix \
// RUN: -analyzer-checker=alpha.unix
#include "Inputs/system-header-simulator.h"
typedef __typeof(sizeof(int)) size_t;
void *memset(void *__s, int __c, size_t __n);
void *malloc(size_t __size);
void free(void *__ptr);
// The store for 'a[1]' should not be removed mistakenly. SymbolicRegions may
// also be live roots.
void f14(int *a) {
int i;
a[1] = 1;
i = a[1];
if (i != 1) {
int *p = 0;
i = *p; // no-warning
}
}
void foo() {
int *x = malloc(sizeof(int));
memset(x, 0, sizeof(int));
int n = 1 / *x; // expected-warning {{Division by zero}}
free(x);
}
void bar() {
int *x = malloc(sizeof(int));
memset(x, 0, 1);
int n = 1 / *x; // no-warning
free(x);
}
void testConcreteNull() {
int *x = 0;
memset(x, 0, 1); // expected-warning {{Null pointer passed as 1st argument to memory set function}}
}
void testStackArray() {
char buf[13];
memset(buf, 0, 1); // no-warning
}
void testHeapSymbol() {
char *buf = (char *)malloc(13);
memset(buf, 0, 1); // no-warning
free(buf);
}
void testStackArrayOutOfBound() {
char buf[1];
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API Summary: I wanted to extend the diagnostics of the CStringChecker with taintedness. This requires the CStringChecker to be refactored to support a more flexible reporting mechanism. This patch does only refactorings, such: - eliminates always false parameters (like WarnAboutSize) - reduces the number of parameters - makes strong types differentiating *source* and *destination* buffers (same with size expressions) - binds the argument expression and the index, making diagnostics accurate and easy to emit - removes a bunch of default parameters to make it more readable - remove random const char* warning message parameters, making clear where and what is going to be emitted Note that: - CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC code duplication - not every function was refactored to use the /new/ strongly typed API, since the CString related functions are really closely coupled monolithic beasts, I will refactor them separately - all tests are preserved and passing; only the message changed at some places. In my opinion, these messages are holding the same information. I would also highlight that this refactoring caught a bug in clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality. This catch backs my effort on simplifying this monolithic CStringChecker. Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso Reviewed By: NoQ Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
memset(buf, 0, 1024);
// expected-warning@-1 {{Memory set function overflows the destination buffer}}
// expected-warning@-2 {{'memset' will always overflow; destination buffer has size 1, but size argument is 1024}}
}
void testHeapSymbolOutOfBound() {
char *buf = (char *)malloc(1);
[analyzer] NFCi: Refactor CStringChecker: use strongly typed internal API Summary: I wanted to extend the diagnostics of the CStringChecker with taintedness. This requires the CStringChecker to be refactored to support a more flexible reporting mechanism. This patch does only refactorings, such: - eliminates always false parameters (like WarnAboutSize) - reduces the number of parameters - makes strong types differentiating *source* and *destination* buffers (same with size expressions) - binds the argument expression and the index, making diagnostics accurate and easy to emit - removes a bunch of default parameters to make it more readable - remove random const char* warning message parameters, making clear where and what is going to be emitted Note that: - CheckBufferAccess now checks *only* one buffer, this removed about 100 LOC code duplication - not every function was refactored to use the /new/ strongly typed API, since the CString related functions are really closely coupled monolithic beasts, I will refactor them separately - all tests are preserved and passing; only the message changed at some places. In my opinion, these messages are holding the same information. I would also highlight that this refactoring caught a bug in clang/test/Analysis/string.c:454 where the diagnostic did not reflect reality. This catch backs my effort on simplifying this monolithic CStringChecker. Reviewers: NoQ, baloghadamsoftware, Szelethus, rengolin, Charusso Reviewed By: NoQ Subscribers: whisperity, xazax.hun, szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, Charusso, martong, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D74806
2020-04-09 22:06:32 +08:00
memset(buf, 0, 1024);
// expected-warning@-1 {{Memory set function overflows the destination buffer}}
free(buf);
}
void testStackArraySameSize() {
char buf[1];
memset(buf, 0, sizeof(buf)); // no-warning
}
void testHeapSymbolSameSize() {
char *buf = (char *)malloc(1);
memset(buf, 0, 1); // no-warning
free(buf);
}