Skip to content

Commit eacad90

Browse files
committed
Merge #1171: Change ARG_CHECK_NO_RETURN to ARG_CHECK_VOID which returns (void)
a49e094 docs: Fix typo (Tim Ruffing) 2551cda tests: Fix code formatting (Tim Ruffing) c635c1b Change ARG_CHECK_NO_RETURN to ARG_CHECK_VOID which returns (void) (Tim Ruffing) cf66f23 refactor: Add helper function secp256k1_context_is_proper() (Tim Ruffing) Pull request description: ACKs for top commit: sipa: utACK a49e094 jonasnick: ACK a49e094 Tree-SHA512: 0fd4ee88510f2de0de96378ae69ce6e610a446000bb78597026c5924803e1ce5a4f76303fc6446233a6129f9c42dce1b1549f93bef935131101e47b5a69cdf2f
2 parents 3f57b9f + a49e094 commit eacad90

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

include/secp256k1.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(
849849
* kind of elliptic curve point multiplication and thus does not benefit from
850850
* enhanced protection against side-channel leakage currently.
851851
*
852-
* It is safe call this function on a copy of secp256k1_context_static in writable
852+
* It is safe to call this function on a copy of secp256k1_context_static in writable
853853
* memory (e.g., obtained via secp256k1_context_clone). In that case, this
854854
* function is guaranteed to return 1, but the call will have no effect because
855855
* the static context (or a copy thereof) is not meant to be randomized.

src/secp256k1.c

+20-4
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@
5151
} \
5252
} while(0)
5353

54-
#define ARG_CHECK_NO_RETURN(cond) do { \
54+
#define ARG_CHECK_VOID(cond) do { \
5555
if (EXPECT(!(cond), 0)) { \
5656
secp256k1_callback_call(&ctx->illegal_callback, #cond); \
57+
return; \
5758
} \
5859
} while(0)
5960

@@ -75,6 +76,15 @@ static const secp256k1_context secp256k1_context_static_ = {
7576
const secp256k1_context *secp256k1_context_static = &secp256k1_context_static_;
7677
const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_static_;
7778

79+
/* Helper function that determines if a context is proper, i.e., is not the static context or a copy thereof.
80+
*
81+
* This is intended for "context" functions such as secp256k1_context_clone. Function which need specific
82+
* features of a context should still check for these features directly. For example, a function that needs
83+
* ecmult_gen should directly check for the existence of the ecmult_gen context. */
84+
static int secp256k1_context_is_proper(const secp256k1_context* ctx) {
85+
return secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx);
86+
}
87+
7888
void secp256k1_selftest(void) {
7989
if (!secp256k1_selftest_passes()) {
8090
secp256k1_callback_call(&default_error_callback, "self test failed");
@@ -157,7 +167,7 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
157167
}
158168

159169
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
160-
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
170+
ARG_CHECK_VOID(ctx != secp256k1_context_static);
161171
if (ctx != NULL) {
162172
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
163173
}
@@ -171,7 +181,10 @@ void secp256k1_context_destroy(secp256k1_context* ctx) {
171181
}
172182

173183
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
174-
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
184+
/* We compare pointers instead of checking secp256k1_context_is_proper() here
185+
because setting callbacks is allowed on *copies* of the static context:
186+
it's harmless and makes testing easier. */
187+
ARG_CHECK_VOID(ctx != secp256k1_context_static);
175188
if (fun == NULL) {
176189
fun = secp256k1_default_illegal_callback_fn;
177190
}
@@ -180,7 +193,10 @@ void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(
180193
}
181194

182195
void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
183-
ARG_CHECK_NO_RETURN(ctx != secp256k1_context_static);
196+
/* We compare pointers instead of checking secp256k1_context_is_proper() here
197+
because setting callbacks is allowed on *copies* of the static context:
198+
it's harmless and makes testing easier. */
199+
ARG_CHECK_VOID(ctx != secp256k1_context_static);
184200
if (fun == NULL) {
185201
fun = secp256k1_default_error_callback_fn;
186202
}

src/tests.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ int context_eq(const secp256k1_context *a, const secp256k1_context *b) {
152152
return a->declassify == b->declassify
153153
&& ecmult_gen_context_eq(&a->ecmult_gen_ctx, &b->ecmult_gen_ctx)
154154
&& a->illegal_callback.fn == b->illegal_callback.fn
155-
&& a->illegal_callback.data == b->illegal_callback.
156-
data
155+
&& a->illegal_callback.data == b->illegal_callback.data
157156
&& a->error_callback.fn == b->error_callback.fn
158157
&& a->error_callback.data == b->error_callback.data;
159158
}

0 commit comments

Comments
 (0)