From dca6a583cbc074740118e3715660b014cf79fe90 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 9 Nov 2022 09:07:45 +1100 Subject: [PATCH 1/2] Conditionally include stdio.h The header `stdio.h` is only needed in code ifdef guarded by `USE_EXTERNAL_DEFAULT_CALLBACKS` and also (indirectly) by `VERIFY`, we can therefore guard the include statement in the same manner. The reason for doing this as that wasm builds downstream have to patch in an empty `stdio.h` file in order to build because of the unconditional include. --- src/util.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util.h b/src/util.h index 0921e34f16..ad031182bb 100644 --- a/src/util.h +++ b/src/util.h @@ -13,9 +13,12 @@ #include #include -#include #include +#if defined(VERIFY) || !defined(USE_EXTERNAL_DEFAULT_CALLBACKS) +#include +#endif + #define STR_(x) #x #define STR(x) STR_(x) #define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x From 303a201bea069d9277e5f0d764670c3348ed2d8b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 9 Nov 2022 09:39:33 +1100 Subject: [PATCH 2/2] Conditionally include stdlib.h If we are using the preallocated interface only then stdlib.h is not required. Currently we unconditionally include it. Add ifdef guards around any code that uses malloc and friends and then conditionally include `stdlib.h` based on `PREALLOC_INTERFACE_ONLY`. The benefit of this patch is that downstream users who wish to build libsecp256k1 into WASM will not need to patch into their build an empty `stdlib.h` file. --- src/secp256k1.c | 16 ++++++++++++---- src/util.h | 5 ++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/secp256k1.c b/src/secp256k1.c index 96102d3651..1bf099a7cd 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -116,34 +116,42 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne } secp256k1_context* secp256k1_context_create(unsigned int flags) { + secp256k1_context* ctx = NULL; + +#ifndef PREALLOC_INTERFACE_ONLY size_t const prealloc_size = secp256k1_context_preallocated_size(flags); - secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size); + ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size); if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) { free(ctx); return NULL; } - +#endif return ctx; } secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) { - secp256k1_context* ret; + secp256k1_context* ret = NULL; + +#ifndef PREALLOC_INTERFACE_ONLY VERIFY_CHECK(ctx != NULL); ARG_CHECK(prealloc != NULL); ret = (secp256k1_context*)prealloc; *ret = *ctx; +#endif return ret; } secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) { - secp256k1_context* ret; + secp256k1_context* ret = NULL; size_t prealloc_size; +#ifndef PREALLOC_INTERFACE_ONLY VERIFY_CHECK(ctx != NULL); prealloc_size = secp256k1_context_preallocated_clone_size(ctx); ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size); ret = secp256k1_context_preallocated_clone(ctx, ret); +#endif return ret; } diff --git a/src/util.h b/src/util.h index ad031182bb..8be82a4329 100644 --- a/src/util.h +++ b/src/util.h @@ -11,7 +11,6 @@ #include "libsecp256k1-config.h" #endif -#include #include #include @@ -19,6 +18,10 @@ #include #endif +#ifndef PREALLOC_INTERFACE_ONLY +#include +#endif + #define STR_(x) #x #define STR(x) STR_(x) #define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x