File tree Expand file tree Collapse file tree 2 files changed +12
-0
lines changed Expand file tree Collapse file tree 2 files changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,10 @@ static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_c
60
60
secp256k1_callback_call (error_callback , "invalid scratch space" );
61
61
return 0 ;
62
62
}
63
+ /* Ensure that multiplication will not wrap around */
64
+ if (ALIGNMENT > 1 && objects > SIZE_MAX /(ALIGNMENT - 1 )) {
65
+ return 0 ;
66
+ }
63
67
if (scratch -> max_size - scratch -> alloc_size <= objects * (ALIGNMENT - 1 )) {
64
68
return 0 ;
65
69
}
Original file line number Diff line number Diff line change @@ -400,6 +400,14 @@ void run_scratch_tests(void) {
400
400
secp256k1_scratch_space_destroy (none , scratch );
401
401
CHECK (ecount == 5 );
402
402
403
+ /* Test that large integers do not wrap around in a bad way */
404
+ scratch = secp256k1_scratch_space_create (none , 1000 );
405
+ /* Try max allocation with a large number of objects. Only makes sense if
406
+ * ALIGNMENT is greater than 1 because otherwise the objects take no extra
407
+ * space. */
408
+ CHECK (ALIGNMENT <= 1 || !secp256k1_scratch_max_allocation (& none -> error_callback , scratch , (SIZE_MAX / (ALIGNMENT - 1 )) + 1 ));
409
+ secp256k1_scratch_space_destroy (none , scratch );
410
+
403
411
/* cleanup */
404
412
secp256k1_scratch_space_destroy (none , NULL ); /* no-op */
405
413
secp256k1_context_destroy (none );
You can’t perform that action at this time.
0 commit comments