@@ -52,25 +52,32 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
52
52
return 1 ;
53
53
}
54
54
55
- /* CHECK that expr_or_stmt calls the illegal callback of ctx exactly once
56
- *
57
- * For checking functions that use ARG_CHECK_VOID */
58
- #define CHECK_ILLEGAL_VOID (ctx , expr_or_stmt ) do { \
59
- int32_t _calls_to_illegal_callback = 0; \
60
- secp256k1_callback _saved_illegal_cb = ctx->illegal_callback; \
61
- secp256k1_context_set_illegal_callback(ctx, \
62
- counting_illegal_callback_fn, &_calls_to_illegal_callback); \
55
+ #define CHECK_COUNTING_CALLBACK_VOID (ctx , expr_or_stmt , callback , callback_setter ) do { \
56
+ int32_t _calls_to_callback = 0; \
57
+ secp256k1_callback _saved_callback = ctx->callback; \
58
+ callback_setter(ctx, counting_callback_fn, &_calls_to_callback); \
63
59
{ expr_or_stmt; } \
64
- ctx->illegal_callback = _saved_illegal_cb ; \
65
- CHECK(_calls_to_illegal_callback == 1); \
60
+ ctx->callback = _saved_callback ; \
61
+ CHECK(_calls_to_callback == 1); \
66
62
} while(0);
67
63
68
- /* CHECK that expr calls the illegal callback of ctx exactly once and that expr == 0
64
+ /* CHECK that expr_or_stmt calls the error or illegal callback of ctx exactly once
65
+ *
66
+ * Useful for checking functions that return void (e.g., API functions that use ARG_CHECK_VOID) */
67
+ #define CHECK_ERROR_VOID (ctx , expr_or_stmt ) \
68
+ CHECK_COUNTING_CALLBACK_VOID(ctx, expr_or_stmt, error_callback, secp256k1_context_set_error_callback)
69
+ #define CHECK_ILLEGAL_VOID (ctx , expr_or_stmt ) \
70
+ CHECK_COUNTING_CALLBACK_VOID(ctx, expr_or_stmt, illegal_callback, secp256k1_context_set_illegal_callback)
71
+
72
+ /* CHECK that
73
+ * - expr calls the illegal callback of ctx exactly once and,
74
+ * - expr == 0 (or equivalently, expr == NULL)
69
75
*
70
- * For checking functions that use ARG_CHECK */
76
+ * Useful for checking functions that return an integer or a pointer. */
71
77
#define CHECK_ILLEGAL (ctx , expr ) CHECK_ILLEGAL_VOID(ctx, CHECK((expr) == 0))
78
+ #define CHECK_ERROR (ctx , expr ) CHECK_ERROR_VOID(ctx, CHECK((expr) == 0))
72
79
73
- static void counting_illegal_callback_fn (const char * str , void * data ) {
80
+ static void counting_callback_fn (const char * str , void * data ) {
74
81
/* Dummy callback function that just counts. */
75
82
int32_t * p ;
76
83
(void )str ;
@@ -334,8 +341,8 @@ static void run_static_context_tests(int use_prealloc) {
334
341
{
335
342
/* Verify that setting and resetting illegal callback works */
336
343
int32_t dummy = 0 ;
337
- secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & dummy );
338
- CHECK (STATIC_CTX -> illegal_callback .fn == counting_illegal_callback_fn );
344
+ secp256k1_context_set_illegal_callback (STATIC_CTX , counting_callback_fn , & dummy );
345
+ CHECK (STATIC_CTX -> illegal_callback .fn == counting_callback_fn );
339
346
CHECK (STATIC_CTX -> illegal_callback .data == & dummy );
340
347
secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
341
348
CHECK (STATIC_CTX -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
@@ -426,8 +433,8 @@ static void run_proper_context_tests(int use_prealloc) {
426
433
CHECK (context_eq (my_ctx , my_ctx_fresh ));
427
434
428
435
/* Verify that setting and resetting illegal callback works */
429
- secp256k1_context_set_illegal_callback (my_ctx , counting_illegal_callback_fn , & dummy );
430
- CHECK (my_ctx -> illegal_callback .fn == counting_illegal_callback_fn );
436
+ secp256k1_context_set_illegal_callback (my_ctx , counting_callback_fn , & dummy );
437
+ CHECK (my_ctx -> illegal_callback .fn == counting_callback_fn );
431
438
CHECK (my_ctx -> illegal_callback .data == & dummy );
432
439
secp256k1_context_set_illegal_callback (my_ctx , NULL , NULL );
433
440
CHECK (my_ctx -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
@@ -468,18 +475,14 @@ static void run_proper_context_tests(int use_prealloc) {
468
475
static void run_scratch_tests (void ) {
469
476
const size_t adj_alloc = ((500 + ALIGNMENT - 1 ) / ALIGNMENT ) * ALIGNMENT ;
470
477
471
- int32_t ecount = 0 ;
472
478
size_t checkpoint ;
473
479
size_t checkpoint_2 ;
474
480
secp256k1_scratch_space * scratch ;
475
481
secp256k1_scratch_space local_scratch ;
476
482
477
- secp256k1_context_set_error_callback (CTX , counting_illegal_callback_fn , & ecount );
478
-
479
483
/* Test public API */
480
484
scratch = secp256k1_scratch_space_create (CTX , 1000 );
481
485
CHECK (scratch != NULL );
482
- CHECK (ecount == 0 );
483
486
484
487
/* Test internal API */
485
488
CHECK (secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ) == 1000 );
@@ -512,22 +515,16 @@ static void run_scratch_tests(void) {
512
515
/* try to apply a bad checkpoint */
513
516
checkpoint_2 = secp256k1_scratch_checkpoint (& CTX -> error_callback , scratch );
514
517
secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , checkpoint );
515
- CHECK (ecount == 0 );
516
- secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , checkpoint_2 ); /* checkpoint_2 is after checkpoint */
517
- CHECK (ecount == 1 );
518
- secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , (size_t ) -1 ); /* this is just wildly invalid */
519
- CHECK (ecount == 2 );
518
+ CHECK_ERROR_VOID (CTX , secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , checkpoint_2 )); /* checkpoint_2 is after checkpoint */
519
+ CHECK_ERROR_VOID (CTX , secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , (size_t ) -1 )); /* this is just wildly invalid */
520
520
521
521
/* try to use badly initialized scratch space */
522
522
secp256k1_scratch_space_destroy (CTX , scratch );
523
523
memset (& local_scratch , 0 , sizeof (local_scratch ));
524
524
scratch = & local_scratch ;
525
- CHECK (!secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ));
526
- CHECK (ecount == 3 );
527
- CHECK (secp256k1_scratch_alloc (& CTX -> error_callback , scratch , 500 ) == NULL );
528
- CHECK (ecount == 4 );
529
- secp256k1_scratch_space_destroy (CTX , scratch );
530
- CHECK (ecount == 5 );
525
+ CHECK_ERROR (CTX , secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ));
526
+ CHECK_ERROR (CTX , secp256k1_scratch_alloc (& CTX -> error_callback , scratch , 500 ));
527
+ CHECK_ERROR_VOID (CTX , secp256k1_scratch_space_destroy (CTX , scratch ));
531
528
532
529
/* Test that large integers do not wrap around in a bad way */
533
530
scratch = secp256k1_scratch_space_create (CTX , 1000 );
@@ -543,8 +540,6 @@ static void run_scratch_tests(void) {
543
540
544
541
/* cleanup */
545
542
secp256k1_scratch_space_destroy (CTX , NULL ); /* no-op */
546
-
547
- secp256k1_context_set_error_callback (CTX , NULL , NULL );
548
543
}
549
544
550
545
static void run_ctz_tests (void ) {
@@ -5759,7 +5754,6 @@ static void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, in
5759
5754
}
5760
5755
}
5761
5756
}
5762
- secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
5763
5757
}
5764
5758
5765
5759
static void run_ec_pubkey_parse_test (void ) {
@@ -6267,7 +6261,6 @@ static void run_eckey_edge_case_test(void) {
6267
6261
CHECK (secp256k1_ec_pubkey_combine (CTX , & pubkey , pubkeys , 2 ) == 1 );
6268
6262
SECP256K1_CHECKMEM_CHECK (& pubkey , sizeof (secp256k1_pubkey ));
6269
6263
CHECK (secp256k1_memcmp_var (& pubkey , zeros , sizeof (secp256k1_pubkey )) > 0 );
6270
- secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
6271
6264
}
6272
6265
6273
6266
static void run_eckey_negate_test (void ) {
@@ -6632,7 +6625,7 @@ static void run_pubkey_comparison(void) {
6632
6625
CHECK_ILLEGAL_VOID (CTX , CHECK (secp256k1_ec_pubkey_cmp (CTX , & pk_tmp , & pk2 ) < 0 ));
6633
6626
{
6634
6627
int32_t ecount = 0 ;
6635
- secp256k1_context_set_illegal_callback (CTX , counting_illegal_callback_fn , & ecount );
6628
+ secp256k1_context_set_illegal_callback (CTX , counting_callback_fn , & ecount );
6636
6629
CHECK (secp256k1_ec_pubkey_cmp (CTX , & pk_tmp , & pk_tmp ) == 0 );
6637
6630
CHECK (ecount == 2 );
6638
6631
secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
0 commit comments