scratch space: thread error_callback into all scratch space functions

Use it when checking magic bytes
This commit is contained in:
Andrew Poelstra
2019-03-13 23:30:51 +00:00
parent 0be1a4ae62
commit c2b028a281
9 changed files with 130 additions and 104 deletions

View File

@@ -21,10 +21,11 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
return ret;
}
static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
static void secp256k1_scratch_destroy(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
if (scratch != NULL) {
VERIFY_CHECK(scratch->frame == 0);
if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return;
}
memset(scratch->magic, 0, sizeof(scratch->magic));
@@ -33,10 +34,11 @@ static void secp256k1_scratch_destroy(secp256k1_scratch* scratch) {
}
}
static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch, size_t objects) {
static size_t secp256k1_scratch_max_allocation(const secp256k1_callback* error_callback, const secp256k1_scratch* scratch, size_t objects) {
size_t i = 0;
size_t allocated = 0;
if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return 0;
}
for (i = 0; i < scratch->frame; i++) {
@@ -48,14 +50,15 @@ static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch* scratch,
return scratch->max_size - allocated - objects * ALIGNMENT;
}
static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n, size_t objects) {
static int secp256k1_scratch_allocate_frame(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t n, size_t objects) {
VERIFY_CHECK(scratch->frame < SECP256K1_SCRATCH_MAX_FRAMES);
if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return 0;
}
if (n <= secp256k1_scratch_max_allocation(scratch, objects)) {
if (n <= secp256k1_scratch_max_allocation(error_callback, scratch, objects)) {
n += objects * ALIGNMENT;
scratch->current_frame = scratch->data;
scratch->data = (void *) ((char *) scratch->data + n);
@@ -68,23 +71,25 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
}
}
static void secp256k1_scratch_deallocate_frame(secp256k1_scratch* scratch) {
VERIFY_CHECK(scratch->frame > 0);
static void secp256k1_scratch_deallocate_frame(const secp256k1_callback* error_callback, secp256k1_scratch* scratch) {
if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return;
}
VERIFY_CHECK(scratch->frame > 0);
scratch->frame--;
scratch->data = (void *) ((char *) scratch->data - scratch->frame_size[scratch->frame]);
}
static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
static void *secp256k1_scratch_alloc(const secp256k1_callback* error_callback, secp256k1_scratch* scratch, size_t size) {
void *ret;
size_t frame = scratch->frame - 1;
size = ROUND_TO_ALIGN(size);
if (memcmp(scratch->magic, "scratch", 8) != 0) {
secp256k1_callback_call(error_callback, "invalid scratch space");
return NULL;
}