2929
3030static int count = 64 ;
3131static secp256k1_context * ctx = NULL ;
32+ static secp256k1_context * sttc = NULL ;
3233
3334static 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