forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-bug-push-string-maxsize.c
More file actions
62 lines (48 loc) · 1.69 KB
/
Copy pathtest-bug-push-string-maxsize.c
File metadata and controls
62 lines (48 loc) · 1.69 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
/*===
*** test_1a (duk_safe_call)
push string with maximum size_t (should fail)
==> rc=1, result='RangeError: string too long'
*** test_1b (duk_safe_call)
push string with maximum size_t - 8 (should fail)
==> rc=1, result='RangeError: string too long'
===*/
/* Same as test-bug-push-buffer-maxsize.c but for string pushing.
*
* There were actually two bugs in the implementation previously:
* (1) the size computation for the header plus string may overflow,
* and (2) the string size is passed as a duk_u32 internally which
* clamps incorrectly on 64-bit platforms.
*
* The attempt to push a string of SIZE_MAX (or close) should fail
* before the string data is actually read (there isn't enough data,
* of course, if that were to happen).
*
* The fix, now implemented, is to check for string maximum size
* explicitly.
*/
const char dummy[4] = { 'f', 'o', 'o', '\0' };
static duk_ret_t test_1a(duk_context *ctx, void *udata) {
const char *p;
(void) udata;
duk_set_top(ctx, 0);
printf("push string with maximum size_t (should fail)\n");
p = duk_push_lstring(ctx, dummy, DUK_SIZE_MAX);
printf("ptr is non-NULL: %d\n", (p != 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) {
const char *p;
(void) udata;
duk_set_top(ctx, 0);
printf("push string with maximum size_t - 8 (should fail)\n");
p = duk_push_lstring(ctx, dummy, DUK_SIZE_MAX - 8);
printf("ptr is non-NULL: %d\n", (p != 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);
}