Skip to content

Commit 2e1b9e0

Browse files
tests: Abort if malloc() fails during context cloning tests
Found by the clang static analyzer. This is the worst true positive that it found. I feel somewhat proud.
1 parent 3e5cfc5 commit 2e1b9e0

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/tests.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ void run_context_tests(int use_prealloc) {
182182
ecount2 = 10;
183183
secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount);
184184
secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount2);
185-
secp256k1_context_set_error_callback(sign, counting_illegal_callback_fn, NULL);
186-
CHECK(vrfy->error_callback.fn != sign->error_callback.fn);
185+
/* set error callback (to a function that still aborts in case malloc() fails in secp256k1_context_clone() below) */
186+
secp256k1_context_set_error_callback(sign, secp256k1_default_illegal_callback_fn, NULL);
187+
CHECK(sign->error_callback.fn != vrfy->error_callback.fn);
188+
CHECK(sign->error_callback.fn == secp256k1_default_illegal_callback_fn);
187189

188190
/* check if sizes for cloning are consistent */
189191
CHECK(secp256k1_context_preallocated_clone_size(none) == secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
@@ -239,7 +241,8 @@ void run_context_tests(int use_prealloc) {
239241
}
240242

241243
/* Verify that the error callback makes it across the clone. */
242-
CHECK(vrfy->error_callback.fn != sign->error_callback.fn);
244+
CHECK(sign->error_callback.fn != vrfy->error_callback.fn);
245+
CHECK(sign->error_callback.fn == secp256k1_default_illegal_callback_fn);
243246
/* And that it resets back to default. */
244247
secp256k1_context_set_error_callback(sign, NULL, NULL);
245248
CHECK(vrfy->error_callback.fn == sign->error_callback.fn);

0 commit comments

Comments
 (0)