forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-push-sprintf.c
More file actions
43 lines (37 loc) · 868 Bytes
/
Copy pathtest-push-sprintf.c
File metadata and controls
43 lines (37 loc) · 868 Bytes
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
/*
* Try different format string lengths.
*/
/*===
length sum: 2147713027.000000
final top: 0
===*/
void test(duk_context *ctx) {
char buf[65536 + 1024];
duk_size_t fmt_len, i;
double len_sum = 0.0;
for (fmt_len = 0;
fmt_len <= 65536;
fmt_len ++) {
for (i = 0; i < fmt_len; i++) {
buf[i] = 'x';
}
buf[fmt_len + 0] = '%';
buf[fmt_len + 1] = 'd';
buf[fmt_len + 2] = '\0';
duk_push_sprintf(ctx, buf, 123);
len_sum += (double) duk_get_length(ctx, -1); /* trivial "checksum" */
duk_pop(ctx);
}
/* Length sequence is 3, 4, ..., 65539 ->
* (65539 + 3)/2 * (65539 - 3 + 1) = 2147713027
*
* >>> res = 0
* >>> for i in xrange(0, 65536+1):
* ... res += len( (('x' * i) + '%d') % 123 )
* ...
* >>> res
* 2147713027
*/
printf("length sum: %lf\n", len_sum);
printf("final top: %ld\n", (long) duk_get_top(ctx));
}