Skip to content

Commit

Permalink
[test] Add unit tests for review status config files
Browse files Browse the repository at this point in the history
They break right now, but should test the expected behaviour.
  • Loading branch information
Szelethus committed Dec 8, 2023
1 parent 0242cff commit 483d6b6
Show file tree
Hide file tree
Showing 18 changed files with 686 additions and 0 deletions.
32 changes: 32 additions & 0 deletions analyzer/tests/projects/cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
OBJS = $(SRCS:.cpp=.o)

CXXFLAGS = -Wno-all -Wno-extra -std=c++11

SRCS = call_and_message.cpp \
divide_zero.cpp \
divide_zero_duplicate.cpp \
file_to_be_skipped.cpp \
new_delete.cpp \
null_dereference.cpp \
stack_address_escape.cpp \
skip_header.cpp \
path_begin1.cpp \
path_begin2.cpp \
statistical_checkers.cpp

.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@

all: $(OBJS) space path_begin_var1 path_begin_var2

space:
$(CXX) -c has\ a\ space.cpp -Wno-all -Wno-extra

path_begin_var1:
$(CXX) -c -DVAR=1 path_begin.cpp -Wno-all -Wno-extra

path_begin_var2:
$(CXX) -c -DVAR=2 path_begin.cpp -Wno-all -Wno-extra

clean:
rm -rf *.o
47 changes: 47 additions & 0 deletions analyzer/tests/projects/cpp/call_and_message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.CallAndMessage (C, C++, ObjC)
// Check for logical errors for function calls and Objective-C message
// expressions (e.g., uninitialized arguments, null function pointers).

struct S {
int x;
};

void f(struct S s);

void test1() {
struct S s;
// insert_suppress_here
f(s); // warn: passed-by-value arg contain uninitialized data
}

void test2() {
void (*foo)(void);
foo(); // warn: function pointer is uninitialized
}

void test3() {
void (*foo)(void);
foo = 0;
foo(); // warn: function pointer is null
}

class C {
public:
void f();
};

void test4() {
C *pc;
pc->f(); // warn: object pointer is uninitialized
}

void test5() {
C *pc = 0;
pc->f(); // warn: object pointer is null <- non breaking space here save the file in latin1 (ISO-8895-1)
}
19 changes: 19 additions & 0 deletions analyzer/tests/projects/cpp/divide_zero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.DivideZero (C, C++, ObjC)
// Check for division by zero.

void test1(int z) {
if (z == 0)
int x = 1 / z; // warn
}

void test2() {
int x = 1;
// insert_suppress_here
int y = x % 0; // warn
}
13 changes: 13 additions & 0 deletions analyzer/tests/projects/cpp/divide_zero_duplicate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.DivideZero (C, C++, ObjC)
// Check for division by zero.

void test1(int z) {
if (z == 0)
int x = 1 / z; // warn
}
12 changes: 12 additions & 0 deletions analyzer/tests/projects/cpp/file_to_be_skipped.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// This file is to be included in a skip list file.

void skipped_test(int z) {
if (z == 0)
int x = 1 / z; // warn
}
14 changes: 14 additions & 0 deletions analyzer/tests/projects/cpp/has a space.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.NullDereference (C, C++, ObjC)
// Check for dereferences of null pointers.

int main()
{
int *p = 0;
return *p + 4;
}
50 changes: 50 additions & 0 deletions analyzer/tests/projects/cpp/new_delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// cplusplus.NewDelete (C++)
// Check for double-free, use-after-free and offset problems involving C++
// delete.

void f(int *p);

void testUseMiddleArgAfterDelete(int *p) {
delete p;
f(p); // warn: use after free
}

class SomeClass {
public:
void f();
};

void test1() {
SomeClass *c = new SomeClass;
delete c;
c->f(); // warn: use after free
}

void test2() {
int *p = (int *)__builtin_alloca(sizeof(int));
delete p; // warn: deleting memory allocated by alloca
}

void test3() {
int *p = new int;
delete p;
delete p; // warn: attempt to free released °
}

void test4() {
int i;
delete &i; // warn: delete address of local
}

void test() {
int *p = new int[1];
delete[] (++p);
// warn: argument to 'delete[]' is offset by 4 bytes
// from the start of memory allocated by 'new[]'
}
30 changes: 30 additions & 0 deletions analyzer/tests/projects/cpp/null_dereference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.NullDereference (C, C++, ObjC)
// Check for dereferences of null pointers.

void test1(int *p) {
if (p)
return;

int x = p[0]; // warn
}

void test2(int *p) {
if (!p)
*p = 0; // warn
}

class C {
public:
int x;
};

void test3() {
C *pc = 0;
int k = pc->x; // warn
}
31 changes: 31 additions & 0 deletions analyzer/tests/projects/cpp/path_begin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------


int div_zero2(int z, int w) {

if (z == 0){
int x = 1 / z; // warn
return x;
}

if (w==0){
z=0;
int x = 1 / z; // warn
return x;
}

}

int f(int x){
return div_zero2(0, x);
}

int g(int x){
return div_zero2(x, 0);
}

14 changes: 14 additions & 0 deletions analyzer/tests/projects/cpp/path_begin1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

#include "path_end.h"

int f(int x){
if(x == 0){
x = div_zero(x);
return x;
}
}
17 changes: 17 additions & 0 deletions analyzer/tests/projects/cpp/path_begin2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

#include "path_end.h"

int g(int y){
return div_zero(y);
}

int test(int z){
if(z == 0){
return g(z);
}
}
13 changes: 13 additions & 0 deletions analyzer/tests/projects/cpp/path_end.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

int div_zero(int z) {
if (z == 0){
int x = 1 / z; // warn
return x;
}

}
5 changes: 5 additions & 0 deletions analyzer/tests/projects/cpp/project_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "cpp",
"clean_cmd": "make clean",
"build_cmd": "make"
}
11 changes: 11 additions & 0 deletions analyzer/tests/projects/cpp/skip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

int null_div(int z) {
int x = z / 0; // warn
return x;
}

18 changes: 18 additions & 0 deletions analyzer/tests/projects/cpp/skip_header.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

#include "skip.h"

int test() {
int x = null_div(42);
return x;
}

int test1(int i) {
int y = i / 0; // warn
return y;
}

25 changes: 25 additions & 0 deletions analyzer/tests/projects/cpp/stack_address_escape.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.StackAddressEscape (C)
// Check that addresses of stack memory do not escape the function.

char const *p;

void test1() {
char const str[] = "string";
p = str; // warn
}

void* test2() {
return __builtin_alloca(12); // warn
}

void test3() {
static int *x;
int y;
x = &y; // warn
}
Loading

0 comments on commit 483d6b6

Please sign in to comment.