Skip to content

Commit f8573bb

Browse files
Use standard lib functionality only when available
1 parent c4e5832 commit f8573bb

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

include/secp256k1.h

+12
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ typedef int (*secp256k1_nonce_function)(
138138
/* Unreachable with a confirming compiler. Guess "yes" as a last resort. */
139139
# define SECP256K1_HAVE_STDIO_H 1
140140
# endif
141+
# if !SECP256K1_HAVE_STDIO_H && defined(SECP256K1_BUILD) && !defined(USE_EXTERNAL_DEFAULT_CALLBACKS)
142+
# pragma message( \
143+
"<stdio.h> appears unavailable, " \
144+
"disabling debugging output for fatal errors in libsecp256k1. " \
145+
"(#define SECP256K1_HAVE_STDIO_H 0 to suppress this message.)")
146+
# endif
141147
#endif
142148
#if !defined(SECP256K1_HAVE_STDLIB_H)
143149
# if defined(__has_include)
@@ -148,6 +154,12 @@ typedef int (*secp256k1_nonce_function)(
148154
/* Unreachable with a confirming compiler. Guess "yes" as a last resort. */
149155
# define SECP256K1_HAVE_STDLIB_H 1
150156
# endif
157+
# if !SECP256K1_HAVE_STDLIB_H && defined(SECP256K1_BUILD)
158+
# pragma message( \
159+
"<stdlib.h> appears unavailable, " \
160+
"disabling dynamic memory allocation in libsecp256k1. " \
161+
"(#define SECP256K1_HAVE_STDLIB_H 0 to suppress this message.)")
162+
# endif
151163
#endif
152164

153165
/* When this header is used at build-time the SECP256K1_BUILD define needs to be set

src/scratch_impl.h

+4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
#ifndef SECP256K1_SCRATCH_IMPL_H
88
#define SECP256K1_SCRATCH_IMPL_H
99

10+
#include <string.h>
11+
1012
#include "util.h"
1113
#include "scratch.h"
1214

15+
#if SECP256K1_HAVE_STDLIB_H
1316
static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* error_callback, size_t size) {
1417
const size_t base_alloc = ROUND_TO_ALIGN(sizeof(secp256k1_scratch));
1518
void *alloc = checked_malloc(error_callback, base_alloc + size);
@@ -34,6 +37,7 @@ static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback,
3437
free(scratch);
3538
}
3639
}
40+
#endif
3741

3842
static size_t secp256k1_scratch_checkpoint(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch) {
3943
if (secp256k1_memcmp_var(scratch->magic, "scratch", 8) != 0) {

src/secp256k1.c

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
137137
return ret;
138138
}
139139

140+
#if SECP256K1_HAVE_STDLIB_H
140141
secp256k1_context* secp256k1_context_create(unsigned int flags) {
141142
size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
142143
secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
@@ -147,6 +148,7 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) {
147148

148149
return ctx;
149150
}
151+
#endif
150152

151153
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
152154
secp256k1_context* ret;
@@ -159,6 +161,7 @@ secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context*
159161
return ret;
160162
}
161163

164+
#if SECP256K1_HAVE_STDLIB_H
162165
secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
163166
secp256k1_context* ret;
164167
size_t prealloc_size;
@@ -171,6 +174,7 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
171174
ret = secp256k1_context_preallocated_clone(ctx, ret);
172175
return ret;
173176
}
177+
#endif
174178

175179
void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
176180
ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
@@ -183,6 +187,7 @@ void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) {
183187
secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
184188
}
185189

190+
#if SECP256K1_HAVE_STDLIB_H
186191
void secp256k1_context_destroy(secp256k1_context* ctx) {
187192
ARG_CHECK_VOID(ctx == NULL || secp256k1_context_is_proper(ctx));
188193

@@ -194,6 +199,7 @@ void secp256k1_context_destroy(secp256k1_context* ctx) {
194199
secp256k1_context_preallocated_destroy(ctx);
195200
free(ctx);
196201
}
202+
#endif
197203

198204
void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) {
199205
/* We compare pointers instead of checking secp256k1_context_is_proper() here
@@ -219,6 +225,7 @@ void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(co
219225
ctx->error_callback.data = data;
220226
}
221227

228+
#if SECP256K1_HAVE_STDLIB_H
222229
secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) {
223230
VERIFY_CHECK(ctx != NULL);
224231
return secp256k1_scratch_create(&ctx->error_callback, max_size);
@@ -228,6 +235,7 @@ void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scr
228235
VERIFY_CHECK(ctx != NULL);
229236
secp256k1_scratch_destroy(&ctx->error_callback, scratch);
230237
}
238+
#endif
231239

232240
/* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour
233241
* of the software.

src/util.h

+25-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@
99

1010
#include "../include/secp256k1.h"
1111

12-
#include <stdlib.h>
13-
#include <stdint.h>
14-
#include <stdio.h>
1512
#include <limits.h>
13+
#include <stddef.h>
14+
#include <stdint.h>
15+
#if SECP256K1_HAVE_STDIO_H
16+
# include <stdio.h>
17+
#endif
18+
#if SECP256K1_HAVE_STDLIB_H
19+
# include <stdlib.h>
20+
#endif
1621

1722
#define STR_(x) #x
1823
#define STR(x) STR_(x)
1924
#define DEBUG_CONFIG_MSG(x) "DEBUG_CONFIG: " x
2025
#define DEBUG_CONFIG_DEF(x) DEBUG_CONFIG_MSG(#x "=" STR(x))
2126

27+
#if SECP256K1_HAVE_STDIO_H
2228
/* Debug helper for printing arrays of unsigned char. */
2329
#define PRINT_BUF(buf, len) do { \
2430
printf("%s[%lu] = ", #buf, (unsigned long)len); \
@@ -38,6 +44,7 @@ static void print_buf_plain(const unsigned char *buf, size_t len) {
3844
}
3945
printf("\n}\n");
4046
}
47+
#endif
4148

4249
# if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
4350
# if SECP256K1_GNUC_PREREQ(2,7)
@@ -73,15 +80,27 @@ static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback *
7380
cb->fn(text, (void*)cb->data);
7481
}
7582

76-
#ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
83+
#if !SECP256K1_HAVE_STDLIB_H && !defined(USE_EXTERNAL_DEFAULT_CALLBACKS)
84+
# error "<stdlib.h> is not available. You need to use external default callbacks, see the documentation of secp256k1_context_set_illegal_callback."
85+
#endif
86+
87+
#if SECP256K1_HAVE_STDLIB_H
7788
static void secp256k1_default_illegal_callback_fn(const char* str, void* data) {
7889
(void)data;
90+
#if SECP256K1_HAVE_STDIO_H
7991
fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
92+
#else
93+
(void)str;
94+
#endif
8095
abort();
8196
}
8297
static void secp256k1_default_error_callback_fn(const char* str, void* data) {
8398
(void)data;
99+
#if SECP256K1_HAVE_STDIO_H
84100
fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
101+
#else
102+
(void)str;
103+
#endif
85104
abort();
86105
}
87106
#else
@@ -139,13 +158,15 @@ static const secp256k1_callback default_error_callback = {
139158
#define VERIFY_CHECK(cond)
140159
#endif
141160

161+
#if SECP256K1_HAVE_STDLIB_H
142162
static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
143163
void *ret = malloc(size);
144164
if (ret == NULL) {
145165
secp256k1_callback_call(cb, "Out of memory");
146166
}
147167
return ret;
148168
}
169+
#endif
149170

150171
#if defined(__BIGGEST_ALIGNMENT__)
151172
#define ALIGNMENT __BIGGEST_ALIGNMENT__

0 commit comments

Comments
 (0)