Skip to content

Commit 53b4d5d

Browse files
committed
Add more tests pt 2
1 parent da72823 commit 53b4d5d

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=unix \
4+
// RUN: -analyzer-checker=debug.ExprInspection
5+
6+
#define NULL ((void*) 0)
7+
8+
enum bool {
9+
false,
10+
true
11+
};
12+
13+
typedef struct __pthread_attr pthread_attr_t;
14+
15+
typedef __typeof(sizeof(int)) size_t;
16+
17+
void free (void* ptr);
18+
19+
void *malloc(size_t sz);
20+
21+
void clang_analyzer_checkInlined(int);
22+
void clang_analyzer_warnIfReached();
23+
void clang_analyzer_printState();
24+
25+
typedef unsigned long int pthread_t;
26+
27+
28+
int pthread_create(pthread_t *restrict thread,
29+
const pthread_attr_t *restrict attr,
30+
void *(*start_routine)(void*), void *restrict arg);
31+
32+
33+
int pthread_join(pthread_t thread, void **retval);
34+
35+
36+
void *worker(void *);
37+
int foo(void);
38+
39+
void *worker(void *data)
40+
41+
{
42+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
43+
// clang_analyzer_printState();
44+
45+
int *pdata = (int *)data;
46+
// switch (*pdata) {
47+
// case 0:
48+
// case 2:
49+
// free(data);
50+
// break;
51+
// default:
52+
// break;
53+
// }
54+
55+
return NULL;
56+
}
57+
58+
59+
60+
int foo(void)
61+
{
62+
pthread_t th1, th2;
63+
64+
int *pdata1 = (int *)malloc(sizeof(int));
65+
int *pdata2 = (int *)malloc(sizeof(int));
66+
67+
68+
*pdata1 = 0;
69+
*pdata2 = 1;
70+
71+
// clang_analyzer_printState();
72+
pthread_create(&th1, NULL, worker, pdata1);
73+
pthread_create(&th2, NULL, worker, pdata2); // expected-warning {{Potential leak of memory pointed to by 'pdata1'}}
74+
75+
//
76+
// pthread_join(th1, NULL);
77+
// pthread_join(th2, NULL);
78+
79+
return 0; // expected-warning {{Potential leak of memory pointed to by 'pdata2'}}
80+
}
81+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=unix \
4+
// RUN: -analyzer-checker=debug.ExprInspection
5+
6+
#define NULL ((void*) 0)
7+
8+
enum bool {
9+
false,
10+
true
11+
};
12+
13+
typedef struct __pthread_attr pthread_attr_t;
14+
15+
typedef __typeof(sizeof(int)) size_t;
16+
17+
void free (void* ptr);
18+
19+
void *malloc(size_t sz);
20+
21+
void clang_analyzer_checkInlined(int);
22+
void clang_analyzer_warnIfReached();
23+
void clang_analyzer_printState();
24+
25+
typedef unsigned long int pthread_t;
26+
27+
28+
int pthread_create(pthread_t *restrict thread,
29+
const pthread_attr_t *restrict attr,
30+
void *(*start_routine)(void*), void *restrict arg);
31+
32+
33+
int pthread_join(pthread_t thread, void **retval);
34+
35+
36+
void *worker(void *);
37+
int foo(void);
38+
39+
void *worker(void *data)
40+
41+
{
42+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
43+
// clang_analyzer_printState();
44+
45+
int *pdata = (int *)data;
46+
switch (*pdata) {
47+
case 0:
48+
case 2:
49+
free(data);
50+
break;
51+
default:
52+
break;
53+
}
54+
55+
return NULL;
56+
}
57+
58+
59+
60+
int foo(void)
61+
{
62+
pthread_t th1, th2;
63+
64+
int *pdata1 = (int *)malloc(sizeof(int));
65+
// int *pdata2 = (int *)malloc(sizeof(int));
66+
67+
68+
*pdata1 = 1;
69+
// *pdata2 = 1;
70+
71+
// clang_analyzer_printState();
72+
pthread_create(&th1, NULL, worker, pdata1);
73+
74+
75+
return 0; // expected-warning {{Potential leak of memory pointed to by 'pdata1'}}
76+
}
77+

0 commit comments

Comments
 (0)