Skip to content

Commit 77d97d9

Browse files
committed
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.
1 parent 8a8863a commit 77d97d9

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/secp256k1.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,42 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
116116
}
117117

118118
secp256k1_context* secp256k1_context_create(unsigned int flags) {
119+
secp256k1_context* ctx = NULL;
120+
121+
#ifndef PREALLOC_INTERFACE_ONLY
119122
size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
120-
secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
123+
ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
121124
if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
122125
free(ctx);
123126
return NULL;
124127
}
125-
128+
#endif
126129
return ctx;
127130
}
128131

129132
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
130-
secp256k1_context* ret;
133+
secp256k1_context* ret = NULL;
134+
135+
#ifndef PREALLOC_INTERFACE_ONLY
131136
VERIFY_CHECK(ctx != NULL);
132137
ARG_CHECK(prealloc != NULL);
133138

134139
ret = (secp256k1_context*)prealloc;
135140
*ret = *ctx;
141+
#endif
136142
return ret;
137143
}
138144

139145
secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
140-
secp256k1_context* ret;
146+
secp256k1_context* ret = NULL;
141147
size_t prealloc_size;
142148

149+
#ifndef PREALLOC_INTERFACE_ONLY
143150
VERIFY_CHECK(ctx != NULL);
144151
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
145152
ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
146153
ret = secp256k1_context_preallocated_clone(ctx, ret);
154+
#endif
147155
return ret;
148156
}
149157

src/util.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "libsecp256k1-config.h"
1212
#endif
1313

14-
#include <stdlib.h>
1514
#include <stdint.h>
1615
#include <limits.h>
1716

@@ -23,6 +22,10 @@
2322
#include <stdio.h>
2423
#endif
2524

25+
#ifndef PREALLOC_INTERFACE_ONLY
26+
#include <stdlib.h>
27+
#endif
28+
2629
#define STR_(x) #x
2730
#define STR(x) STR_(x)
2831
#define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x

0 commit comments

Comments
 (0)