@@ -199,7 +199,8 @@ static void *js_trace_malloc(JSMallocState *s, size_t size)
199
199
/* Do not allocate zero bytes: behavior is platform dependent */
200
200
assert (size != 0 );
201
201
202
- if (unlikely (s -> malloc_size + size > s -> malloc_limit ))
202
+ /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
203
+ if (unlikely (s -> malloc_size + size > s -> malloc_limit - 1 ))
203
204
return NULL ;
204
205
ptr = malloc (size );
205
206
js_trace_malloc_printf (s , "A %zd -> %p\n" , size , ptr );
@@ -238,7 +239,8 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
238
239
free (ptr );
239
240
return NULL ;
240
241
}
241
- if (s -> malloc_size + size - old_size > s -> malloc_limit )
242
+ /* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */
243
+ if (s -> malloc_size + size - old_size > s -> malloc_limit - 1 )
242
244
return NULL ;
243
245
244
246
js_trace_malloc_printf (s , "R %zd %p" , size , ptr );
@@ -295,10 +297,10 @@ int main(int argc, char **argv)
295
297
int module = -1 ;
296
298
int load_std = 0 ;
297
299
int dump_unhandled_promise_rejection = 0 ;
298
- size_t memory_limit = 0 ;
299
300
char * include_list [32 ];
300
301
int i , include_count = 0 ;
301
- size_t stack_size = 0 ;
302
+ int64_t memory_limit = -1 ;
303
+ int64_t stack_size = -1 ;
302
304
303
305
argv0 = (JSCFunctionListEntry )JS_PROP_STRING_DEF ("argv0" , argv [0 ],
304
306
JS_PROP_C_W_E );
@@ -403,7 +405,7 @@ int main(int argc, char **argv)
403
405
opt_arg = argv [optind ++ ];
404
406
}
405
407
// TODO(chqrlie): accept kmg suffixes
406
- memory_limit = ( size_t ) strtod ( opt_arg , NULL );
408
+ memory_limit = strtoull ( opt_arg , NULL , 0 );
407
409
break ;
408
410
}
409
411
if (!strcmp (longopt , "stack-size" )) {
@@ -415,7 +417,7 @@ int main(int argc, char **argv)
415
417
opt_arg = argv [optind ++ ];
416
418
}
417
419
// TODO(chqrlie): accept kmg suffixes
418
- stack_size = ( size_t ) strtod ( opt_arg , NULL );
420
+ stack_size = strtoull ( opt_arg , NULL , 0 );
419
421
break ;
420
422
}
421
423
if (opt ) {
@@ -437,10 +439,10 @@ int main(int argc, char **argv)
437
439
fprintf (stderr , "qjs: cannot allocate JS runtime\n" );
438
440
exit (2 );
439
441
}
440
- if (memory_limit ! = 0 )
441
- JS_SetMemoryLimit (rt , memory_limit );
442
- if (stack_size ! = 0 )
443
- JS_SetMaxStackSize (rt , stack_size );
442
+ if (memory_limit > = 0 )
443
+ JS_SetMemoryLimit (rt , ( size_t ) memory_limit );
444
+ if (stack_size > = 0 )
445
+ JS_SetMaxStackSize (rt , ( size_t ) stack_size );
444
446
if (dump_flags != 0 )
445
447
JS_SetDumpFlags (rt , dump_flags );
446
448
js_std_set_worker_new_context_func (JS_NewCustomContext );
0 commit comments