Skip to content

Commit 8079aed

Browse files
authored
Merge pull request #160 from eed3si9n/wip/buffer
Fixes buffer overrun
2 parents fce8f8c + 1fe7572 commit 8079aed

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

.github/workflows/fuzz.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Fuzz parser
2+
3+
# Run this workflow on changes to the external scanner
4+
on:
5+
workflow_dispatch:
6+
#push:
7+
# paths:
8+
# - src/scanner.c
9+
# - src/stack.h
10+
#pull_request:
11+
# paths:
12+
# - src/scanner.c
13+
# - src/stack.h
14+
15+
jobs:
16+
test:
17+
name: Parser fuzzing
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v3
21+
- uses: eed3si9n/tree-sitter-fuzz-action@v1
22+
with:
23+
language: scala
24+
external-scanner: src/scanner.c
25+
time: 60

src/stack.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#define LOG(...)
1111
#endif
1212

13-
#define STACK_SIZE 1024
13+
// Total payload size is 1024 bytes max
14+
#define STACK_SIZE 100
1415

1516
typedef struct ScannerStack {
16-
unsigned int stack[STACK_SIZE];
17+
int stack[STACK_SIZE];
1718
int top;
1819
int last_indentation_size;
1920
int last_newline_count;
@@ -63,7 +64,10 @@ void printStack(ScannerStack *stack, char *msg) {
6364
}
6465

6566
unsigned serialiseStack(ScannerStack *stack, char *buf) {
66-
unsigned elements = isEmptyStack(stack) ? 0 : stack->top;
67+
int elements = isEmptyStack(stack) ? 0 : stack->top;
68+
if (elements < 0) {
69+
elements = 0;
70+
}
6771
unsigned result_length = (elements + 3) * sizeof(int);
6872
int *placement = (int *)buf;
6973
memcpy(placement, stack->stack, elements * sizeof(int));

test/test-stack.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ int main() {
2626
assert(peekStack(stack) == -1);
2727
assert(isEmptyStack(stack));
2828

29-
char *buf = malloc(2048);
29+
char *buf = malloc(1024);
3030

31-
for (int i = 0; i < 250; i++) {
31+
for (int i = 0; i < 100; i++) {
3232
pushStack(stack, i);
3333
}
3434

35-
assert(serialiseStack(stack, buf) == sizeof(int) * 253);
35+
assert(serialiseStack(stack, buf) == sizeof(int) * 103);
3636

3737
ScannerStack *newStack = createStack();
3838

39-
deserialiseStack(newStack, buf, sizeof(int) * 253);
40-
assert(newStack -> top == 250);
41-
assert(popStack(newStack) == 249);
39+
deserialiseStack(newStack, buf, sizeof(int) * 103);
40+
assert(newStack -> top == 100);
41+
assert(popStack(newStack) == 99);
4242

4343
resetStack(newStack);
4444

0 commit comments

Comments
 (0)