@@ -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 ;
@@ -334,8 +341,8 @@ static void run_static_context_tests(int use_prealloc) {
334341 {
335342 /* Verify that setting and resetting illegal callback works */
336343 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 );
339346 CHECK (STATIC_CTX -> illegal_callback .data == & dummy );
340347 secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
341348 CHECK (STATIC_CTX -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
@@ -426,8 +433,8 @@ static void run_proper_context_tests(int use_prealloc) {
426433 CHECK (context_eq (my_ctx , my_ctx_fresh ));
427434
428435 /* 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 );
431438 CHECK (my_ctx -> illegal_callback .data == & dummy );
432439 secp256k1_context_set_illegal_callback (my_ctx , NULL , NULL );
433440 CHECK (my_ctx -> illegal_callback .fn == secp256k1_default_illegal_callback_fn );
@@ -468,18 +475,14 @@ static void run_proper_context_tests(int use_prealloc) {
468475static void run_scratch_tests (void ) {
469476 const size_t adj_alloc = ((500 + ALIGNMENT - 1 ) / ALIGNMENT ) * ALIGNMENT ;
470477
471- int32_t ecount = 0 ;
472478 size_t checkpoint ;
473479 size_t checkpoint_2 ;
474480 secp256k1_scratch_space * scratch ;
475481 secp256k1_scratch_space local_scratch ;
476482
477- secp256k1_context_set_error_callback (CTX , counting_illegal_callback_fn , & ecount );
478-
479483 /* Test public API */
480484 scratch = secp256k1_scratch_space_create (CTX , 1000 );
481485 CHECK (scratch != NULL );
482- CHECK (ecount == 0 );
483486
484487 /* Test internal API */
485488 CHECK (secp256k1_scratch_max_allocation (& CTX -> error_callback , scratch , 0 ) == 1000 );
@@ -512,22 +515,16 @@ static void run_scratch_tests(void) {
512515 /* try to apply a bad checkpoint */
513516 checkpoint_2 = secp256k1_scratch_checkpoint (& CTX -> error_callback , scratch );
514517 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 */
520520
521521 /* try to use badly initialized scratch space */
522522 secp256k1_scratch_space_destroy (CTX , scratch );
523523 memset (& local_scratch , 0 , sizeof (local_scratch ));
524524 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 ));
531528
532529 /* Test that large integers do not wrap around in a bad way */
533530 scratch = secp256k1_scratch_space_create (CTX , 1000 );
@@ -543,8 +540,6 @@ static void run_scratch_tests(void) {
543540
544541 /* cleanup */
545542 secp256k1_scratch_space_destroy (CTX , NULL ); /* no-op */
546-
547- secp256k1_context_set_error_callback (CTX , NULL , NULL );
548543}
549544
550545static void run_ctz_tests (void ) {
@@ -5759,7 +5754,6 @@ static void ec_pubkey_parse_pointtest(const unsigned char *input, int xvalid, in
57595754 }
57605755 }
57615756 }
5762- secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
57635757}
57645758
57655759static void run_ec_pubkey_parse_test (void ) {
@@ -6267,7 +6261,6 @@ static void run_eckey_edge_case_test(void) {
62676261 CHECK (secp256k1_ec_pubkey_combine (CTX , & pubkey , pubkeys , 2 ) == 1 );
62686262 SECP256K1_CHECKMEM_CHECK (& pubkey , sizeof (secp256k1_pubkey ));
62696263 CHECK (secp256k1_memcmp_var (& pubkey , zeros , sizeof (secp256k1_pubkey )) > 0 );
6270- secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
62716264}
62726265
62736266static void run_eckey_negate_test (void ) {
@@ -6632,7 +6625,7 @@ static void run_pubkey_comparison(void) {
66326625 CHECK_ILLEGAL_VOID (CTX , CHECK (secp256k1_ec_pubkey_cmp (CTX , & pk_tmp , & pk2 ) < 0 ));
66336626 {
66346627 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 );
66366629 CHECK (secp256k1_ec_pubkey_cmp (CTX , & pk_tmp , & pk_tmp ) == 0 );
66376630 CHECK (ecount == 2 );
66386631 secp256k1_context_set_illegal_callback (CTX , NULL , NULL );
0 commit comments