Skip to content

Commit

Permalink
abi: add set and get error calls to the operations struct
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgriffiths committed Oct 2, 2023
1 parent 7718ab8 commit 517fc70
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
13 changes: 10 additions & 3 deletions include/wally_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ typedef int (*wally_ec_nonce_t)(
typedef struct secp256k1_context_struct *(*secp_context_t)(
void);

/** The type of an overridable function to get the last extended error code */
typedef int (*wally_get_error_t)(void);

/** The type of an overridable function to set the last extended error code */
typedef int (*wally_set_error_t)(
int error_code);

/** Structure holding function pointers for overridable wally operations */
struct wally_operations {
uintptr_t struct_size; /* Must be initialised to sizeof(wally_operations) */
Expand All @@ -358,9 +365,9 @@ struct wally_operations {
wally_bzero_t bzero_fn;
wally_ec_nonce_t ec_nonce_fn;
secp_context_t secp_context_fn;
void *reserved_1; /* reserved_ pointers are reserved for future use */
void *reserved_2;
void *reserved_3;
wally_get_error_t get_error_fn;
wally_set_error_t set_error_fn;
void *reserved_3; /* reserved_ pointers are reserved for future use */
void *reserved_4;
};

Expand Down
29 changes: 26 additions & 3 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

int wally_get_build_version(uint32_t *value)
{
Expand Down Expand Up @@ -378,15 +380,25 @@ struct secp256k1_context_struct *wally_internal_secp_context(void)
return global_ctx;
}

int wally_internal_get_error(void) {
return global_error;
}

int wally_internal_set_error(int error_code)
{
global_error = error_code;
return error_code;
}

static struct wally_operations _ops = {
sizeof(struct wally_operations),
wally_internal_malloc,
wally_internal_free,
wally_internal_bzero,
wally_internal_ec_nonce_fn,
wally_internal_secp_context,
NULL,
NULL,
wally_internal_get_error,
wally_internal_set_error,
NULL,
NULL
};
Expand Down Expand Up @@ -428,6 +440,15 @@ char *wally_strdup(const char *str)
return wally_strdup_n(str, strlen(str));
}

int wally_get_error(void) {
return _ops.get_error_fn();
}

int wally_set_error(int error_code)
{
return _ops.set_error_fn(error_code);
}

const struct wally_operations *wally_ops(void)
{
return &_ops;
Expand All @@ -447,7 +468,7 @@ int wally_set_operations(const struct wally_operations *ops)
return WALLY_EINVAL; /* Null or invalid version of ops */
/* Reserved pointers must be null so they can be enabled in the
* future without breaking back compatibility */
if (ops->reserved_1 || ops->reserved_2 || ops->reserved_3 || ops->reserved_4)
if (ops->reserved_3 || ops->reserved_4)
return WALLY_EINVAL;

#define COPY_FN_PTR(name) if (ops->name) _ops.name = ops->name
Expand All @@ -456,6 +477,8 @@ int wally_set_operations(const struct wally_operations *ops)
COPY_FN_PTR (bzero_fn);
COPY_FN_PTR (ec_nonce_fn);
COPY_FN_PTR (secp_context_fn);
COPY_FN_PTR (get_error_fn);
COPY_FN_PTR (set_error_fn);
#undef COPY_FN_PTR
return WALLY_OK;
}
Expand Down

0 comments on commit 517fc70

Please sign in to comment.