Skip to content

Commit 5db782e

Browse files
Allow usage of external default callbacks
1 parent 6095a86 commit 5db782e

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

configure.ac

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ AC_ARG_ENABLE(module_recovery,
134134
[enable_module_recovery=$enableval],
135135
[enable_module_recovery=no])
136136

137+
AC_ARG_ENABLE(external_default_callbacks,
138+
AS_HELP_STRING([--enable-external-default-callbacks],[enable external default callback functions (default is no)]),
139+
[use_external_default_callbacks=$enableval],
140+
[use_external_default_callbacks=no])
141+
137142
AC_ARG_ENABLE(jni,
138143
AS_HELP_STRING([--enable-jni],[enable libsecp256k1_jni [default=no]]),
139144
[use_jni=$enableval],
@@ -493,6 +498,10 @@ if test x"$use_external_asm" = x"yes"; then
493498
AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used])
494499
fi
495500

501+
if test x"$use_external_default_callbacks" = x"yes"; then
502+
AC_DEFINE(USE_EXTERNAL_DEFAULT_CALLBACKS, 1, [Define this symbol if an external implementation of the default callbacks is used])
503+
fi
504+
496505
if test x"$enable_experimental" = x"yes"; then
497506
AC_MSG_NOTICE([******])
498507
AC_MSG_NOTICE([WARNING: experimental build])
@@ -535,22 +544,23 @@ AC_OUTPUT
535544

536545
echo
537546
echo "Build Options:"
538-
echo " with endomorphism = $use_endomorphism"
539-
echo " with ecmult precomp = $set_precomp"
540-
echo " with jni = $use_jni"
541-
echo " with benchmarks = $use_benchmark"
542-
echo " with coverage = $enable_coverage"
543-
echo " module ecdh = $enable_module_ecdh"
544-
echo " module recovery = $enable_module_recovery"
547+
echo " with endomorphism = $use_endomorphism"
548+
echo " with ecmult precomp = $set_precomp"
549+
echo " with external callbacks = $use_external_default_callbacks"
550+
echo " with jni = $use_jni"
551+
echo " with benchmarks = $use_benchmark"
552+
echo " with coverage = $enable_coverage"
553+
echo " module ecdh = $enable_module_ecdh"
554+
echo " module recovery = $enable_module_recovery"
545555
echo
546-
echo " asm = $set_asm"
547-
echo " bignum = $set_bignum"
548-
echo " field = $set_field"
549-
echo " scalar = $set_scalar"
550-
echo " ecmult window size = $set_ecmult_window"
556+
echo " asm = $set_asm"
557+
echo " bignum = $set_bignum"
558+
echo " field = $set_field"
559+
echo " scalar = $set_scalar"
560+
echo " ecmult window size = $set_ecmult_window"
551561
echo
552-
echo " CC = $CC"
553-
echo " CFLAGS = $CFLAGS"
554-
echo " CPPFLAGS = $CPPFLAGS"
555-
echo " LDFLAGS = $LDFLAGS"
562+
echo " CC = $CC"
563+
echo " CFLAGS = $CFLAGS"
564+
echo " CPPFLAGS = $CPPFLAGS"
565+
echo " LDFLAGS = $LDFLAGS"
556566
echo

include/secp256k1.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,28 @@ SECP256K1_API void secp256k1_context_destroy(
247247
* to cause a crash, though its return value and output arguments are
248248
* undefined.
249249
*
250+
* When this function has not been called (or called with fn==NULL), then the
251+
* default handler will be used. The library provides a default handler which
252+
* writes the message to stderr and calls abort. This default handler can be
253+
* replaced at link time if the preprocessor macro
254+
* USE_EXTERNAL_DEFAULT_CALLBACKS is defined, which is the case if the build
255+
* has been configured with --enable-external-default-callbacks. Then the
256+
* following two symbols must be provided to link against:
257+
* - void secp256k1_default_illegal_callback_fn(const char* message, void* data);
258+
* - void secp256k1_default_error_callback_fn(const char* message, void* data);
259+
* The library can call these default handlers even before a proper callback data
260+
* pointer could have been set using secp256k1_context_set_illegal_callback or
261+
* secp256k1_context_set_illegal_callback, e.g., when the creation of a context
262+
* fails. In this case, the corresponding default handler will be called with
263+
* the data pointer argument set to NULL.
264+
*
250265
* Args: ctx: an existing context object (cannot be NULL)
251266
* In: fun: a pointer to a function to call when an illegal argument is
252-
* passed to the API, taking a message and an opaque pointer
253-
* (NULL restores a default handler that calls abort).
267+
* passed to the API, taking a message and an opaque pointer.
268+
* (NULL restores the default handler.)
254269
* data: the opaque pointer to pass to fun above.
270+
*
271+
* See also secp256k1_context_set_error_callback.
255272
*/
256273
SECP256K1_API void secp256k1_context_set_illegal_callback(
257274
secp256k1_context* ctx,
@@ -271,9 +288,12 @@ SECP256K1_API void secp256k1_context_set_illegal_callback(
271288
*
272289
* Args: ctx: an existing context object (cannot be NULL)
273290
* In: fun: a pointer to a function to call when an internal error occurs,
274-
* taking a message and an opaque pointer (NULL restores a default
275-
* handler that calls abort).
291+
* taking a message and an opaque pointer (NULL restores the
292+
* default handler, see secp256k1_context_set_illegal_callback
293+
* for details).
276294
* data: the opaque pointer to pass to fun above.
295+
*
296+
* See also secp256k1_context_set_illegal_callback.
277297
*/
278298
SECP256K1_API void secp256k1_context_set_error_callback(
279299
secp256k1_context* ctx,

src/secp256k1.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,32 @@
3333
} \
3434
} while(0)
3535

36+
#ifndef USE_EXTERNAL_DEFAULT_CALLBACKS
3637
static void default_illegal_callback_fn(const char* str, void* data) {
3738
(void)data;
3839
fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
3940
abort();
4041
}
41-
42-
static const secp256k1_callback default_illegal_callback = {
43-
default_illegal_callback_fn,
44-
NULL
45-
};
46-
4742
static void default_error_callback_fn(const char* str, void* data) {
4843
(void)data;
4944
fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
5045
abort();
5146
}
47+
#else
48+
void default_illegal_callback_fn(const char* str, void* data);
49+
void default_error_callback_fn(const char* str, void* data);
50+
#endif
51+
52+
static const secp256k1_callback default_illegal_callback = {
53+
default_illegal_callback_fn,
54+
NULL
55+
};
5256

5357
static const secp256k1_callback default_error_callback = {
5458
default_error_callback_fn,
5559
NULL
5660
};
5761

58-
5962
struct secp256k1_context_struct {
6063
secp256k1_ecmult_context ecmult_ctx;
6164
secp256k1_ecmult_gen_context ecmult_gen_ctx;

0 commit comments

Comments
 (0)