Skip to content

Commit

Permalink
Conditionally include stdlib.h
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed Nov 20, 2022
1 parent dca6a58 commit 303a201
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
#include "libsecp256k1-config.h"
#endif

#include <stdlib.h>
#include <stdint.h>
#include <limits.h>

#if defined(VERIFY) || !defined(USE_EXTERNAL_DEFAULT_CALLBACKS)
#include <stdio.h>
#endif

#ifndef PREALLOC_INTERFACE_ONLY
#include <stdlib.h>
#endif

#define STR_(x) #x
#define STR(x) STR_(x)
#define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x
Expand Down

0 comments on commit 303a201

Please sign in to comment.