@@ -52,25 +52,32 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
5252 return 1 ;
5353}
5454
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); \
6359 { 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); \
6662} while(0);
6763
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)
6975 *
70- * For checking functions that use ARG_CHECK */
76+ * Useful for checking functions that return an integer or a pointer. */
7177#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))
7279
73- static void counting_illegal_callback_fn (const char * str , void * data ) {
80+ static void counting_callback_fn (const char * str , void * data ) {
7481 /* Dummy callback function that just counts. */
7582 int32_t * p ;
7683 (void )str ;
@@ -301,8 +308,8 @@ static void run_static_context_tests(int use_prealloc) {
301308 {
302309 /* Verify that setting and resetting illegal callback works */
303310 int32_t dummy = 0 ;
304- secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & dummy );
305- CHECK (STATIC_CTX -> illegal_callback .fn == counting_illegal_callback_fn );
311+ secp256k1_context_set_illegal_callback (STATIC_CTX , counting_callback_fn , & dummy );
312+ CHECK (STATIC_CTX -> illegal_callback .fn == counting_callback_fn );
306313 CHECK (STATIC_CTX -> illegal_callback .data == & dummy );
307314 secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
308315 CHECK (STATIC_CTX -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
@@ -393,8 +400,8 @@ static void run_proper_context_tests(int use_prealloc) {
393400 CHECK (context_eq (my_ctx , my_ctx_fresh ));
394401
395402 /* Verify that setting and resetting illegal callback works */
396- secp256k1_context_set_illegal_callback (my_ctx , counting_illegal_callback_fn , & dummy );
397- CHECK (my_ctx -> illegal_callback .fn == counting_illegal_callback_fn );
403+ secp256k1_context_set_illegal_callback (my_ctx , counting_callback_fn , & dummy );
404+ CHECK (my_ctx -> illegal_callback .fn == counting_callback_fn );
398405 CHECK (my_ctx -> illegal_callback .data == & dummy );
399406 secp256k1_context_set_illegal_callback (my_ctx , NULL , NULL );
400407 CHECK (my_ctx -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
@@ -435,18 +442,14 @@ static void run_proper_context_tests(int use_prealloc) {
435442static void run_scratch_tests (void ) {
436443 const size_t adj_alloc = ((500 + ALIGNMENT - 1 ) / ALIGNMENT ) * ALIGNMENT ;
437444
438- int32_t ecount = 0 ;
439445 size_t checkpoint ;
440446 size_t checkpoint_2 ;
441447 secp256k1_scratch_space * scratch ;
442448 secp256k1_scratch_space local_scratch ;
443449
444- secp256k1_context_set_error_callback (CTX , counting_illegal_callback_fn , & ecount );
445-
446450 /* Test public API */
447451 scratch = secp256k1_scratch_space_create (CTX , 1000 );
448452 CHECK (scratch != NULL );
449- CHECK (ecount == 0 );
450453
451454 /* Test internal API */
452455 CHECK (secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ) == 1000 );
@@ -479,22 +482,16 @@ static void run_scratch_tests(void) {
479482 /* try to apply a bad checkpoint */
480483 checkpoint_2 = secp256k1_scratch_checkpoint (& CTX -> error_callback , scratch );
481484 secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , checkpoint );
482- CHECK (ecount == 0 );
483- secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , checkpoint_2 ); /* checkpoint_2 is after checkpoint */
484- CHECK (ecount == 1 );
485- secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , (size_t ) -1 ); /* this is just wildly invalid */
486- CHECK (ecount == 2 );
485+ CHECK_ERROR_VOID (CTX , secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , checkpoint_2 )); /* checkpoint_2 is after checkpoint */
486+ CHECK_ERROR_VOID (CTX , secp256k1_scratch_apply_checkpoint (& CTX -> error_callback , scratch , (size_t ) -1 )); /* this is just wildly invalid */
487487
488488 /* try to use badly initialized scratch space */
489489 secp256k1_scratch_space_destroy (CTX , scratch );
490490 memset (& local_scratch , 0 , sizeof (local_scratch ));
491491 scratch = & local_scratch ;
492- CHECK (!secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ));
493- CHECK (ecount == 3 );
494- CHECK (secp256k1_scratch_alloc (& CTX -> error_callback , scratch , 500 ) == NULL );
495- CHECK (ecount == 4 );
496- secp256k1_scratch_space_destroy (CTX , scratch );
497- CHECK (ecount == 5 );
492+ CHECK_ERROR (CTX , secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ));
493+ CHECK_ERROR (CTX , secp256k1_scratch_alloc (& CTX -> error_callback , scratch , 500 ));
494+ CHECK_ERROR_VOID (CTX , secp256k1_scratch_space_destroy (CTX , scratch ));
498495
499496 /* Test that large integers do not wrap around in a bad way */
500497 scratch = secp256k1_scratch_space_create (CTX , 1000 );
@@ -510,8 +507,6 @@ static void run_scratch_tests(void) {
510507
511508 /* cleanup */
512509 secp256k1_scratch_space_destroy (CTX , NULL ); /* no-op */
513-
514- secp256k1_context_set_error_callback (CTX , NULL , NULL );
515510}
516511
517512static void run_ctz_tests (void ) {
@@ -5733,7 +5728,6 @@ static void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, in
57335728 }
57345729 }
57355730 }
5736- secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
57375731}
57385732
57395733static void run_ec_pubkey_parse_test (void ) {
@@ -6241,7 +6235,6 @@ static void run_eckey_edge_case_test(void) {
62416235 CHECK (secp256k1_ec_pubkey_combine (CTX , & pubkey , pubkeys , 2 ) == 1 );
62426236 SECP256K1_CHECKMEM_CHECK (& pubkey , sizeof (secp256k1_pubkey ));
62436237 CHECK (secp256k1_memcmp_var (& pubkey , zeros , sizeof (secp256k1_pubkey )) > 0 );
6244- secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
62456238}
62466239
62476240static void run_eckey_negate_test (void ) {
@@ -6606,7 +6599,7 @@ static void run_pubkey_comparison(void) {
66066599 CHECK_ILLEGAL_VOID (CTX , CHECK (secp256k1_ec_pubkey_cmp (CTX , & pk_tmp , & pk2 ) < 0 ));
66076600 {
66086601 int32_t ecount = 0 ;
6609- secp256k1_context_set_illegal_callback (CTX , counting_illegal_callback_fn , & ecount );
6602+ secp256k1_context_set_illegal_callback (CTX , counting_callback_fn , & ecount );
66106603 CHECK (secp256k1_ec_pubkey_cmp (CTX , & pk_tmp , & pk_tmp ) == 0 );
66116604 CHECK (ecount == 2 );
66126605 secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
0 commit comments