Skip to content

Commit 893926d

Browse files
author
Craig Ringer
committed
clang scan-build stack escape checker example 1
for pg mailing list thread https://www.postgresql.org/message-id/CAMsr+YE8tveiaGPuNzw+5fuo0yeZf7LePVLpx9MxUrFHMBG0gQ@mail.gmail.com
1 parent adca75a commit 893926d

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
guard.so
2+
return_stack_escape

c/clang_return_stack_checks/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CC ?= clang
2+
CFLAGS ?= -Wall -Wextra
3+
4+
all: return_stack_escape
5+
6+
guard.so: guard.c guard.h
7+
$(CC) -shared -fPIC $< -o $@
8+
9+
return_stack_escape: return_stack_escape.c guard.so
10+
$(CC) $< ./guard.so -o $@
11+
12+
clean:
13+
rm -f guard.so return_stack_escape

c/clang_return_stack_checks/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Examples for thread https://www.postgresql.org/message-id/CAMsr+YE8tveiaGPuNzw+5fuo0yeZf7LePVLpx9MxUrFHMBG0gQ@mail.gmail.com

c/clang_return_stack_checks/guard.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "guard.h"
2+
3+
struct guard * guard_ptr = 0;

c/clang_return_stack_checks/guard.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdint.h>
2+
#include <assert.h>
3+
4+
struct guard
5+
{
6+
int8_t guard_set;
7+
};
8+
9+
extern struct guard * guard_ptr;
10+
11+
static void
12+
set_guard(struct guard * const g)
13+
{
14+
assert(!g->guard_set);
15+
assert(!guard_ptr);
16+
g->guard_set = 1;
17+
guard_ptr = g;
18+
}
19+
20+
static inline void
21+
clear_guard(struct guard * const g)
22+
{
23+
assert(g->guard_set);
24+
assert(guard_ptr);
25+
g->guard_set = 0;
26+
guard_ptr = 0;
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <error.h>
4+
5+
#include "guard.h"
6+
7+
int
8+
foo(int do_fail)
9+
{
10+
struct guard g = {0};
11+
set_guard(&g);
12+
13+
if (do_fail)
14+
return do_fail;
15+
16+
clear_guard(&g);
17+
18+
return do_fail;
19+
}
20+
21+
int
22+
main(int argc, char * argv[])
23+
{
24+
int do_fail;
25+
int ret;
26+
char * endpos;
27+
28+
if (argc != 2)
29+
error(2, 0, "usage: %s 0|1", argv[0]);
30+
31+
do_fail = strtol(argv[1], &endpos, 10);
32+
if (*endpos != '\0')
33+
error(2, 0, "couldn't parse \"%s\" as an integer", argv[1]);
34+
35+
ret = foo(do_fail);
36+
37+
if (guard_ptr)
38+
printf("guard value: %hhd\n", guard_ptr->guard_set);
39+
else
40+
printf("guard value: no guard pointer\n");
41+
42+
return ret;
43+
}

0 commit comments

Comments
 (0)