@@ -44,11 +44,31 @@ static int all_bytes_equal(const void* s, unsigned char value, size_t n) {
44
44
return 1 ;
45
45
}
46
46
47
+ /* TODO Use CHECK_ILLEGAL(_VOID) everywhere and get rid of the uncounting callback */
48
+ /* CHECK that expr_or_stmt calls the illegal callback of ctx exactly once
49
+ *
50
+ * For checking functions that use ARG_CHECK_VOID */
51
+ #define CHECK_ILLEGAL_VOID (ctx , expr_or_stmt ) do { \
52
+ int32_t _calls_to_illegal_callback = 0; \
53
+ secp256k1_callback _saved_illegal_cb = ctx->illegal_callback; \
54
+ secp256k1_context_set_illegal_callback(ctx, \
55
+ counting_illegal_callback_fn, &_calls_to_illegal_callback); \
56
+ { expr_or_stmt; } \
57
+ ctx->illegal_callback = _saved_illegal_cb; \
58
+ CHECK(_calls_to_illegal_callback == 1); \
59
+ } while(0);
60
+
61
+ /* CHECK that expr calls the illegal callback of ctx exactly once and that expr == 0
62
+ *
63
+ * For checking functions that use ARG_CHECK */
64
+ #define CHECK_ILLEGAL (ctx , expr ) CHECK_ILLEGAL_VOID(ctx, CHECK((expr) == 0))
65
+
47
66
static void counting_illegal_callback_fn (const char * str , void * data ) {
48
67
/* Dummy callback function that just counts. */
49
68
int32_t * p ;
50
69
(void )str ;
51
70
p = data ;
71
+ CHECK (* p != INT32_MAX );
52
72
(* p )++ ;
53
73
}
54
74
@@ -57,6 +77,7 @@ static void uncounting_illegal_callback_fn(const char* str, void* data) {
57
77
int32_t * p ;
58
78
(void )str ;
59
79
p = data ;
80
+ CHECK (* p != INT32_MIN );
60
81
(* p )-- ;
61
82
}
62
83
@@ -246,39 +267,28 @@ static void run_static_context_tests(int use_prealloc) {
246
267
CHECK (secp256k1_context_no_precomp == secp256k1_context_static );
247
268
248
269
{
249
- int ecount = 0 ;
250
270
unsigned char seed [32 ] = {0x17 };
251
- secp256k1_context_set_illegal_callback (STATIC_CTX , counting_illegal_callback_fn , & ecount );
252
271
253
272
/* Randomizing secp256k1_context_static is not supported. */
254
- CHECK (secp256k1_context_randomize (STATIC_CTX , seed ) == 0 );
255
- CHECK (ecount == 1 );
256
- CHECK (secp256k1_context_randomize (STATIC_CTX , NULL ) == 0 );
257
- CHECK (ecount == 2 );
258
- ecount = 0 ;
273
+ CHECK_ILLEGAL (STATIC_CTX , secp256k1_context_randomize (STATIC_CTX , seed ));
274
+ CHECK_ILLEGAL (STATIC_CTX , secp256k1_context_randomize (STATIC_CTX , NULL ));
259
275
260
276
/* Destroying or cloning secp256k1_context_static is not supported. */
261
277
if (use_prealloc ) {
262
- CHECK (secp256k1_context_preallocated_clone_size (STATIC_CTX ) == 0 );
263
- CHECK (ecount == 1 );
278
+ CHECK_ILLEGAL (STATIC_CTX , secp256k1_context_preallocated_clone_size (STATIC_CTX ));
264
279
{
265
280
secp256k1_context * my_static_ctx = malloc (sizeof (* STATIC_CTX ));
266
281
CHECK (my_static_ctx != NULL );
267
282
memset (my_static_ctx , 0x2a , sizeof (* my_static_ctx ));
268
- CHECK ( secp256k1_context_preallocated_clone (STATIC_CTX , my_static_ctx ) == NULL );
283
+ CHECK_ILLEGAL ( STATIC_CTX , secp256k1_context_preallocated_clone (STATIC_CTX , my_static_ctx ));
269
284
CHECK (all_bytes_equal (my_static_ctx , 0x2a , sizeof (* my_static_ctx )));
270
- CHECK (ecount == 2 );
271
285
free (my_static_ctx );
272
286
}
273
- secp256k1_context_preallocated_destroy (STATIC_CTX );
274
- CHECK (ecount == 3 );
287
+ CHECK_ILLEGAL_VOID (STATIC_CTX , secp256k1_context_preallocated_destroy (STATIC_CTX ));
275
288
} else {
276
- CHECK (secp256k1_context_clone (STATIC_CTX ) == NULL );
277
- CHECK (ecount == 1 );
278
- secp256k1_context_destroy (STATIC_CTX );
279
- CHECK (ecount == 2 );
289
+ CHECK_ILLEGAL (STATIC_CTX , secp256k1_context_clone (STATIC_CTX ));
290
+ CHECK_ILLEGAL_VOID (STATIC_CTX , secp256k1_context_destroy (STATIC_CTX ));
280
291
}
281
- secp256k1_context_set_illegal_callback (STATIC_CTX , NULL , NULL );
282
292
}
283
293
284
294
{
0 commit comments