Skip to content

Commit ba12dd0

Browse files
Check arguments of _preallocated functions
1 parent 5feadde commit ba12dd0

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/secp256k1.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ size_t secp256k1_context_preallocated_size(unsigned int flags) {
8484

8585
size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
8686
size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context));
87+
VERIFY_CHECK(ctx != NULL);
8788
if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) {
8889
ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE;
8990
}
@@ -95,9 +96,12 @@ size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) {
9596

9697
secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) {
9798
void* const base = prealloc;
98-
size_t prealloc_size = secp256k1_context_preallocated_size(flags);
99-
secp256k1_context* ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
99+
size_t prealloc_size;
100+
secp256k1_context* ret;
100101

102+
VERIFY_CHECK(prealloc != NULL);
103+
prealloc_size = secp256k1_context_preallocated_size(flags);
104+
ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size);
101105
ret->illegal_callback = default_illegal_callback;
102106
ret->error_callback = default_error_callback;
103107

@@ -132,17 +136,26 @@ secp256k1_context* secp256k1_context_create(unsigned int flags) {
132136
}
133137

134138
secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) {
135-
size_t prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
136-
secp256k1_context* ret = (secp256k1_context*)prealloc;
139+
size_t prealloc_size;
140+
secp256k1_context* ret;
141+
VERIFY_CHECK(ctx != NULL);
142+
ARG_CHECK(prealloc != NULL);
143+
144+
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
145+
ret = (secp256k1_context*)prealloc;
137146
memcpy(ret, ctx, prealloc_size);
138147
secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx);
139148
secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx);
140149
return ret;
141150
}
142151

143152
secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
144-
size_t prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
145-
secp256k1_context* ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
153+
secp256k1_context* ret;
154+
size_t prealloc_size;
155+
156+
VERIFY_CHECK(ctx != NULL);
157+
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
158+
ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
146159
ret = secp256k1_context_preallocated_clone(ctx, ret);
147160
return ret;
148161
}

0 commit comments

Comments
 (0)