Closed
Description
The following code snippet very roughly resembles what happens in the libsecp test harness and illustrates the issue:
/* in the tests we have access to a global context */
static secp256k1_context *ctx = NULL;
void test_foo() {
int ecount;
context_set_error_callback(ctx, counting_illegal_callback_fn, &ecount);
/* do some tests with ecount */
}
void test_bar() {
/* we don't set the counting_illegal_callback here because we don't want to test that here */
some_function(ctx);
}
void main() {
test_foo();
test_bar();
}
The code is fine, until one day some_function(ctx)
results in the illegal callback being called.
Then we'd want the test to fail but instead what happens is that some stack region formerly known as ecount
is modified, which does not necessarily result in a crash.
One solution would be to never add the counting_illegal_callback
to the global context and instead create a local context for counting.