Skip to content

Commit 517fc70

Browse files
committed
abi: add set and get error calls to the operations struct
Future error handling will support extended error codes via these functions which are user-overridable in order to support thread safety. Make the structure changes now without using them so we can avoid an ABI version bump once implemented.
1 parent 7718ab8 commit 517fc70

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

include/wally_core.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,13 @@ typedef int (*wally_ec_nonce_t)(
350350
typedef struct secp256k1_context_struct *(*secp_context_t)(
351351
void);
352352

353+
/** The type of an overridable function to get the last extended error code */
354+
typedef int (*wally_get_error_t)(void);
355+
356+
/** The type of an overridable function to set the last extended error code */
357+
typedef int (*wally_set_error_t)(
358+
int error_code);
359+
353360
/** Structure holding function pointers for overridable wally operations */
354361
struct wally_operations {
355362
uintptr_t struct_size; /* Must be initialised to sizeof(wally_operations) */
@@ -358,9 +365,9 @@ struct wally_operations {
358365
wally_bzero_t bzero_fn;
359366
wally_ec_nonce_t ec_nonce_fn;
360367
secp_context_t secp_context_fn;
361-
void *reserved_1; /* reserved_ pointers are reserved for future use */
362-
void *reserved_2;
363-
void *reserved_3;
368+
wally_get_error_t get_error_fn;
369+
wally_set_error_t set_error_fn;
370+
void *reserved_3; /* reserved_ pointers are reserved for future use */
364371
void *reserved_4;
365372
};
366373

src/internal.c

+26-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
/* Caller is responsible for thread safety */
1919
static secp256k1_context *global_ctx = NULL;
20+
/* Global extended error code. Not thread-safe unless caller-overridden */
21+
static int global_error = WALLY_OK;
2022

2123
int wally_get_build_version(uint32_t *value)
2224
{
@@ -378,15 +380,25 @@ struct secp256k1_context_struct *wally_internal_secp_context(void)
378380
return global_ctx;
379381
}
380382

383+
int wally_internal_get_error(void) {
384+
return global_error;
385+
}
386+
387+
int wally_internal_set_error(int error_code)
388+
{
389+
global_error = error_code;
390+
return error_code;
391+
}
392+
381393
static struct wally_operations _ops = {
382394
sizeof(struct wally_operations),
383395
wally_internal_malloc,
384396
wally_internal_free,
385397
wally_internal_bzero,
386398
wally_internal_ec_nonce_fn,
387399
wally_internal_secp_context,
388-
NULL,
389-
NULL,
400+
wally_internal_get_error,
401+
wally_internal_set_error,
390402
NULL,
391403
NULL
392404
};
@@ -428,6 +440,15 @@ char *wally_strdup(const char *str)
428440
return wally_strdup_n(str, strlen(str));
429441
}
430442

443+
int wally_get_error(void) {
444+
return _ops.get_error_fn();
445+
}
446+
447+
int wally_set_error(int error_code)
448+
{
449+
return _ops.set_error_fn(error_code);
450+
}
451+
431452
const struct wally_operations *wally_ops(void)
432453
{
433454
return &_ops;
@@ -447,7 +468,7 @@ int wally_set_operations(const struct wally_operations *ops)
447468
return WALLY_EINVAL; /* Null or invalid version of ops */
448469
/* Reserved pointers must be null so they can be enabled in the
449470
* future without breaking back compatibility */
450-
if (ops->reserved_1 || ops->reserved_2 || ops->reserved_3 || ops->reserved_4)
471+
if (ops->reserved_3 || ops->reserved_4)
451472
return WALLY_EINVAL;
452473

453474
#define COPY_FN_PTR(name) if (ops->name) _ops.name = ops->name
@@ -456,6 +477,8 @@ int wally_set_operations(const struct wally_operations *ops)
456477
COPY_FN_PTR (bzero_fn);
457478
COPY_FN_PTR (ec_nonce_fn);
458479
COPY_FN_PTR (secp_context_fn);
480+
COPY_FN_PTR (get_error_fn);
481+
COPY_FN_PTR (set_error_fn);
459482
#undef COPY_FN_PTR
460483
return WALLY_OK;
461484
}

0 commit comments

Comments
 (0)