forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-bug-push-buffer-maxsize.c
More file actions
94 lines (71 loc) · 2.56 KB
/
test-bug-push-buffer-maxsize.c
File metadata and controls
94 lines (71 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*===
*** test_1a (duk_safe_call)
fixed size buffer, maximum size_t (should fail)
==> rc=1, result='RangeError: buffer too long'
*** test_1b (duk_safe_call)
fixed size buffer, maximum size_t - 8 (should fail)
==> rc=1, result='RangeError: buffer too long'
*** test_2a (duk_safe_call)
dynamic size buffer, maximum size_t (should fail)
==> rc=1, result='RangeError: buffer too long'
*** test_2b (duk_safe_call)
dynamic size buffer, maximum size_t - 8 (should fail)
==> rc=1, result='RangeError: buffer too long'
===*/
/* Before Duktape 0.9.0, an attempt to allocate a buffer of maximum size_t
* (or anything so close that when the heap header size is added, the result
* overflows) causes a spurious successful allocation now. The allocation
* will in fact be too little to even contain the heap header but will appear
* to succeed.
*
* The proper behavior is to check for a maximum size before adding the header
* size to the requested size (this is done now).
*/
static duk_ret_t test_1a(duk_context *ctx, void *udata) {
void *buf;
(void) udata;
duk_set_top(ctx, 0);
printf("fixed size buffer, maximum size_t (should fail)\n");
buf = duk_push_buffer(ctx, DUK_SIZE_MAX, 0);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_1b(duk_context *ctx, void *udata) {
void *buf;
(void) udata;
duk_set_top(ctx, 0);
printf("fixed size buffer, maximum size_t - 8 (should fail)\n");
buf = duk_push_buffer(ctx, DUK_SIZE_MAX - 8, 0);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
static duk_ret_t test_2a(duk_context *ctx, void *udata) {
void *buf;
(void) udata;
duk_set_top(ctx, 0);
printf("dynamic size buffer, maximum size_t (should fail)\n");
buf = duk_push_buffer(ctx, DUK_SIZE_MAX, 1);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
/* This test actually succeeds even with the bug. */
static duk_ret_t test_2b(duk_context *ctx, void *udata) {
void *buf;
(void) udata;
duk_set_top(ctx, 0);
printf("dynamic size buffer, maximum size_t - 8 (should fail)\n");
buf = duk_push_buffer(ctx, DUK_SIZE_MAX - 8, 1);
printf("ptr is non-NULL: %d\n", (buf != NULL ? 1 : 0));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
/*printf("DUK_SIZE_MAX=%lf\n", (double) DUK_SIZE_MAX);*/
TEST_SAFE_CALL(test_1a);
TEST_SAFE_CALL(test_1b);
TEST_SAFE_CALL(test_2a);
TEST_SAFE_CALL(test_2b);
}