Skip to content

Commit 49dd695

Browse files
authored
Merge pull request #165 from MingxiuWang/master
add new nullptr test cases
2 parents 03d3e6d + 3ef33c7 commit 49dd695

25 files changed

+618
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
extern void SAFE_LOAD(void *ptr);
6+
extern void UNSAFE_LOAD(void *ptr);
7+
8+
void test_safe_strcat() {
9+
char *a = (char *)malloc(20);
10+
char *b = (char *)malloc(20);
11+
if (a && b) {
12+
strcpy(a, "Hello");
13+
strcpy(b, " World");
14+
15+
// Safe usage
16+
SAFE_LOAD(a); // Access before strcat
17+
SAFE_LOAD(b);
18+
19+
strcat(a, b); // Concatenate strings
20+
}
21+
free(a);
22+
free(b);
23+
}
24+
25+
void test_unsafe_strcat() {
26+
char *a = NULL;
27+
char *b = (char *)malloc(20);
28+
if (b) {
29+
strcpy(b, " World");
30+
31+
// Unsafe usage
32+
UNSAFE_LOAD(a); // a is NULL — this is unsafe
33+
SAFE_LOAD(b); // b is valid
34+
35+
strcat(a, b); // Undefined behavior (null dereference)
36+
}
37+
free(b);
38+
}
39+
40+
int main() {
41+
test_safe_strcat();
42+
test_unsafe_strcat(); // This will likely crash — intended for static analysis tools like SVF
43+
return 0;
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
extern void SAFE_LOAD(void *ptr);
6+
extern void UNSAFE_LOAD(void *ptr);
7+
8+
void test_safe_memcpy() {
9+
int *a = (int *)malloc(5 * sizeof(int));
10+
int *b = (int *)malloc(5 * sizeof(int));
11+
if (a && b) {
12+
for (int i = 0; i < 5; ++i) {
13+
b[i] = i;
14+
}
15+
16+
SAFE_LOAD(a);
17+
SAFE_LOAD(b);
18+
19+
memcpy(a, b, 5 * sizeof(int)); // Safe copy of 5 integers
20+
}
21+
free(a);
22+
free(b);
23+
}
24+
25+
void test_unsafe_memcpy() {
26+
int *a = NULL;
27+
int *b = (int *)malloc(5 * sizeof(int));
28+
if (b) {
29+
for (int i = 0; i < 5; ++i) {
30+
b[i] = i;
31+
}
32+
33+
UNSAFE_LOAD(a); // a is NULL — unsafe
34+
SAFE_LOAD(b); // b is valid
35+
36+
memcpy(a, b, 5 * sizeof(int)); // Undefined behavior
37+
}
38+
free(b);
39+
}
40+
41+
int main() {
42+
test_safe_memcpy();
43+
test_unsafe_memcpy(); // Likely crash
44+
return 0;
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
5+
extern void SAFE_LOAD(void *ptr); // This pointer is guaranteed safe
6+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
7+
8+
int main() {
9+
char* buf = (char*)malloc(10 * sizeof(char)); // Dynamically allocate memory
10+
char* buf_copy = (char*)malloc(10 * sizeof(char)); // Dynamically allocate memory for copy
11+
12+
memcpy(buf, "Hello", 5); // Copy "Hello" into buf (valid pointer)
13+
memcpy(buf_copy, buf, 5); // Copy the content of buf into buf_copy
14+
15+
SAFE_LOAD(buf); // Safe load: buf is valid
16+
SAFE_LOAD(buf_copy); // Safe load: buf_copy is valid
17+
18+
free(buf); // Free allocated memory for buf
19+
UNSAFE_LOAD(buf); // Unsafe load: buf is now invalid
20+
21+
free(buf_copy); // Free allocated memory for buf_copy
22+
UNSAFE_LOAD(buf_copy); // Unsafe load: buf_copy is now invalid
23+
24+
return 0;
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
extern void SAFE_LOAD(void *ptr); // This pointer is guaranteed safe
5+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
6+
7+
int main() {
8+
int* arr = (int*)malloc(5 * sizeof(int)); // Dynamically allocate memory for 5 integers
9+
int* arr_copy = (int*)malloc(5 * sizeof(int)); // Dynamically allocate memory for the copy
10+
11+
for (int i = 0; i < 5; i++) {
12+
arr[i] = i; // Initialize the array
13+
}
14+
15+
memcpy(arr_copy, arr, 5 * sizeof(int)); // Copy the content of arr to arr_copy
16+
17+
SAFE_LOAD(arr); // Safe load: arr is valid
18+
SAFE_LOAD(arr_copy); // Safe load: arr_copy is valid
19+
20+
free(arr); // Free allocated memory for arr
21+
UNSAFE_LOAD(arr); // Unsafe load: arr is now invalid
22+
23+
free(arr_copy); // Free allocated memory for arr_copy
24+
UNSAFE_LOAD(arr_copy); // Unsafe load: arr_copy is now invalid
25+
26+
return 0;
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
5+
6+
int main() {
7+
char* p = NULL; // NULL pointer
8+
char* p_copy = malloc(10 * sizeof(char)); // Dynamically allocate memory for copy
9+
10+
memcpy(p_copy, "Hello", 5); // Copy "Hello" into p_copy (valid pointer)
11+
memcpy(p, p_copy, 5); // Unsafe operation: p is NULL, this is invalid
12+
13+
free(p_copy); // Free allocated memory for p_copy
14+
UNSAFE_LOAD(p_copy); // Unsafe load: p_copy is now invalid
15+
16+
free(p); // Freeing a NULL pointer is safe, but p is still unsafe
17+
UNSAFE_LOAD(p); // Unsafe load: p is still NULL after free
18+
19+
return 0;
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
extern void SAFE_LOAD(void *ptr); // This pointer is guaranteed safe
5+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
6+
7+
int main() {
8+
char c;
9+
char* p = &c; // Pointer to local variable
10+
char* p_copy = (char*)malloc(sizeof(char)); // Dynamically allocate memory for copy
11+
12+
memcpy(p_copy, "A", 1); // Copy 'A' into p_copy (valid pointer)
13+
14+
SAFE_LOAD(p); // Safe load: p points to a valid local variable
15+
SAFE_LOAD(p_copy); // Safe load: p_copy is valid
16+
17+
free(p_copy); // Free allocated memory for p_copy
18+
UNSAFE_LOAD(p_copy); // Unsafe load: p_copy is now invalid
19+
20+
return 0;
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
extern void SAFE_LOAD(void *ptr); // This pointer is guaranteed safe
5+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
6+
7+
int main() {
8+
void* p = malloc(20); // Dynamically allocate 20 bytes of memory
9+
void* p_copy = malloc(20); // Dynamically allocate memory for copy
10+
11+
memcpy(p, "Hello", 5); // Copy "Hello" into p
12+
memcpy(p_copy, p, 5); // Copy the content of p into p_copy
13+
14+
SAFE_LOAD(p); // Safe load: p is valid
15+
SAFE_LOAD(p_copy); // Safe load: p_copy is valid
16+
17+
free(p); // Free allocated memory for p
18+
UNSAFE_LOAD(p); // Unsafe load: p is now invalid
19+
20+
free(p_copy); // Free allocated memory for p_copy
21+
UNSAFE_LOAD(p_copy); // Unsafe load: p_copy is now invalid
22+
23+
return 0;
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
extern void SAFE_LOAD(void *ptr);
6+
extern void UNSAFE_LOAD(void *ptr);
7+
8+
void test_safe_memset() {
9+
int *a = (int *)malloc(5 * sizeof(int));
10+
if (a) {
11+
SAFE_LOAD(a);
12+
memset(a, 0, 5 * sizeof(int)); // Safe memset of 5 integers
13+
}
14+
free(a);
15+
}
16+
17+
void test_unsafe_memset() {
18+
int *a = NULL;
19+
20+
UNSAFE_LOAD(a); // a is NULL — unsafe
21+
22+
memset(a, 0, 5 * sizeof(int)); // Undefined behavior
23+
}
24+
25+
int main() {
26+
test_safe_memset();
27+
test_unsafe_memset(); // Likely crash
28+
return 0;
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
extern void SAFE_LOAD(void *ptr); // This pointer is guaranteed safe
5+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
6+
7+
int main() {
8+
char* buf = (char*)malloc(10 * sizeof(char)); // Dynamically allocate memory
9+
memset(buf, 0, 10); // Set all 10 bytes to 0 (valid pointer)
10+
11+
SAFE_LOAD(buf); // Safe load: buf is guaranteed to be valid
12+
13+
free(buf); // Free allocated memory
14+
15+
UNSAFE_LOAD(buf); // Unsafe load: buf is now invalid, as it points to freed memory
16+
17+
return 0;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
4+
extern void SAFE_LOAD(void *ptr); // This pointer is guaranteed safe
5+
extern void UNSAFE_LOAD(void *ptr); // This pointer is guaranteed unsafe
6+
7+
int main() {
8+
int* arr = (int*)malloc(5 * sizeof(int)); // Dynamically allocate memory for 5 integers
9+
memset(arr, 0, 5 * sizeof(int)); // Set all 5 integers to 0 (valid pointer)
10+
11+
SAFE_LOAD(arr); // Safe load: arr is guaranteed to be valid
12+
13+
free(arr); // Free allocated memory
14+
15+
UNSAFE_LOAD(arr); // Unsafe load: arr is now invalid, as it points to freed memory
16+
17+
return 0;
18+
}

0 commit comments

Comments
 (0)