Skip to content

Commit b198061

Browse files
tests: Use global copy of secp256k1_context_static instead of clone
1 parent 2a39ac1 commit b198061

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/modules/extrakeys/tests_impl.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ void test_keypair(void) {
336336
secp256k1_xonly_pubkey xonly_pk, xonly_pk_tmp;
337337
int pk_parity, pk_parity_tmp;
338338
int ecount;
339-
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
340339

341340
set_counting_callbacks(ctx, &ecount);
342341
set_counting_callbacks(sttc, &ecount);
@@ -440,7 +439,9 @@ void test_keypair(void) {
440439
memset(&keypair, 0, sizeof(keypair));
441440
CHECK(secp256k1_keypair_sec(ctx, sk_tmp, &keypair) == 1);
442441
CHECK(secp256k1_memcmp_var(zeros96, sk_tmp, sizeof(sk_tmp)) == 0);
443-
secp256k1_context_destroy(sttc);
442+
443+
secp256k1_context_set_error_callback(sttc, NULL, NULL);
444+
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
444445
}
445446

446447
void test_keypair_add(void) {

src/modules/recovery/tests_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned c
3030

3131
void test_ecdsa_recovery_api(void) {
3232
/* Setup contexts that just count errors */
33-
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
3433
secp256k1_pubkey pubkey;
3534
secp256k1_pubkey recpubkey;
3635
secp256k1_ecdsa_signature normal_sig;
@@ -124,7 +123,8 @@ void test_ecdsa_recovery_api(void) {
124123
CHECK(ecount == 7);
125124

126125
/* cleanup */
127-
secp256k1_context_destroy(sttc);
126+
secp256k1_context_set_error_callback(sttc, NULL, NULL);
127+
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
128128
}
129129

130130
void test_ecdsa_recovery_end_to_end(void) {

src/modules/schnorrsig/tests_impl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ void test_schnorrsig_api(void) {
128128
secp256k1_schnorrsig_extraparams invalid_extraparams = {{ 0 }, NULL, NULL};
129129

130130
/** setup **/
131-
secp256k1_context *sttc = secp256k1_context_clone(secp256k1_context_static);
132-
int ecount;
131+
int ecount = 0;
133132

134133
secp256k1_context_set_error_callback(ctx, counting_illegal_callback_fn, &ecount);
135134
secp256k1_context_set_illegal_callback(ctx, counting_illegal_callback_fn, &ecount);
@@ -198,7 +197,8 @@ void test_schnorrsig_api(void) {
198197
CHECK(secp256k1_schnorrsig_verify(ctx, sig, msg, sizeof(msg), &zero_pk) == 0);
199198
CHECK(ecount == 4);
200199

201-
secp256k1_context_destroy(sttc);
200+
secp256k1_context_set_error_callback(sttc, NULL, NULL);
201+
secp256k1_context_set_illegal_callback(sttc, NULL, NULL);
202202
}
203203

204204
/* Checks that hash initialized by secp256k1_schnorrsig_sha256_tagged has the

src/tests.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
static int count = 64;
3131
static secp256k1_context *ctx = NULL;
32+
static secp256k1_context *sttc = NULL;
3233

3334
static void counting_illegal_callback_fn(const char* str, void* data) {
3435
/* Dummy callback function that just counts. */
@@ -180,9 +181,7 @@ void run_context_tests(int use_prealloc) {
180181
unsigned char ctmp[32];
181182
int32_t ecount;
182183
int32_t ecount2;
183-
secp256k1_context *sttc;
184184
void *ctx_prealloc = NULL;
185-
void *sttc_prealloc = NULL;
186185

187186
secp256k1_gej pubj;
188187
secp256k1_ge pub;
@@ -196,11 +195,7 @@ void run_context_tests(int use_prealloc) {
196195
ctx_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
197196
CHECK(ctx_prealloc != NULL);
198197
ctx = secp256k1_context_preallocated_create(ctx_prealloc, SECP256K1_CONTEXT_NONE);
199-
sttc_prealloc = malloc(secp256k1_context_preallocated_clone_size(secp256k1_context_static));
200-
CHECK(sttc_prealloc != NULL);
201-
sttc = secp256k1_context_preallocated_clone(secp256k1_context_static, sttc_prealloc);
202198
} else {
203-
sttc = secp256k1_context_clone(secp256k1_context_static);
204199
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
205200
}
206201

@@ -312,12 +307,9 @@ void run_context_tests(int use_prealloc) {
312307
/* cleanup */
313308
if (use_prealloc) {
314309
secp256k1_context_preallocated_destroy(ctx);
315-
secp256k1_context_preallocated_destroy(sttc);
316310
free(ctx_prealloc);
317-
free(sttc_prealloc);
318311
} else {
319312
secp256k1_context_destroy(ctx);
320-
secp256k1_context_destroy(sttc);
321313
}
322314
/* Defined as no-op. */
323315
secp256k1_context_destroy(NULL);
@@ -7357,6 +7349,15 @@ int main(int argc, char **argv) {
73577349
secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
73587350

73597351
/* initialize */
7352+
/* Make a writable copy of secp256k1_context_static in order to test the effect of API functions
7353+
that write to the context. The API does not support cloning the static context, so we use
7354+
memcpy instead. The user is not supposed to copy a context but we should still ensure that
7355+
the API functions handle copies of the static context gracefully. */
7356+
sttc = malloc(sizeof(*secp256k1_context_static));
7357+
CHECK(sttc != NULL);
7358+
memcpy(sttc, secp256k1_context_static, sizeof(secp256k1_context));
7359+
CHECK(!secp256k1_context_is_proper(sttc));
7360+
73607361
run_selftest_tests();
73617362
run_context_tests(0);
73627363
run_context_tests(1);
@@ -7463,6 +7464,7 @@ int main(int argc, char **argv) {
74637464
secp256k1_testrand_finish();
74647465

74657466
/* shutdown */
7467+
free(sttc);
74667468
secp256k1_context_destroy(ctx);
74677469

74687470
printf("no problems found\n");

0 commit comments

Comments
 (0)