refactor: remove unnecessary `malloc` result casts
What changed, and why it matters
This commit is a straightforward code cleanup: it removes unnecessary type casts in front of memory allocation calls like malloc. In modern C, casting the result of malloc is not needed and is generally discouraged. The change does not alter program behavior, fix a bug, or address a security issue. It touches mostly test and benchmark code, with only two small changes in production functions that create or clone a cryptographic context.
No security action required. Treat as a normal refactoring commit during code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch removes explicit (type*) casts from malloc and checked_malloc calls across three files. In C, void* returned by malloc implicitly converts to any object pointer type, so these casts were redundant. The only production-code changes are in secp256k1_context_create and secp256k1_context_clone inside src/secp256k1.c; all other changes are in benchmarks and tests. There is no functional change, no memory-safety fix, and no change to allocation size calculations or error handling.
Changed components
src/secp256k1.c: secp256k1_context_createsrc/secp256k1.c: secp256k1_context_clonesrc/modules/schnorrsig/bench_impl.h: schnorrsig benchmark allocation codesrc/tests.c: various test allocation helpersInspect captured patch +17 / −17
diff --git a/src/modules/schnorrsig/bench_impl.h b/src/modules/schnorrsig/bench_impl.h
index 93a878e..069464d 100644
--- a/src/modules/schnorrsig/bench_impl.h
+++ b/src/modules/schnorrsig/bench_impl.h
@@ -51,18 +51,18 @@ static void run_schnorrsig_bench(int iters, int argc, char** argv) {
int d = argc == 1;
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
- data.keypairs = (const secp256k1_keypair **)malloc(iters * sizeof(secp256k1_keypair *));
- data.pk = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
- data.msgs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
- data.sigs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
+ data.keypairs = malloc(iters * sizeof(secp256k1_keypair *));
+ data.pk = malloc(iters * sizeof(unsigned char *));
+ data.msgs = malloc(iters * sizeof(unsigned char *));
+ data.sigs = malloc(iters * sizeof(unsigned char *));
CHECK(MSGLEN >= 4);
for (i = 0; i < iters; i++) {
unsigned char sk[32];
- unsigned char *msg = (unsigned char *)malloc(MSGLEN);
- unsigned char *sig = (unsigned char *)malloc(64);
- secp256k1_keypair *keypair = (secp256k1_keypair *)malloc(sizeof(*keypair));
- unsigned char *pk_char = (unsigned char *)malloc(32);
+ unsigned char *msg = malloc(MSGLEN);
+ unsigned char *sig = malloc(64);
+ secp256k1_keypair *keypair = malloc(sizeof(*keypair));
+ unsigned char *pk_char = malloc(32);
secp256k1_xonly_pubkey pk;
msg[0] = sk[0] = i;
msg[1] = sk[1] = i >> 8;
diff --git a/src/secp256k1.c b/src/secp256k1.c
index ddd9849..218ceaf 100644
--- a/src/secp256k1.c
+++ b/src/secp256k1.c
@@ -140,7 +140,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
secp256k1_context* secp256k1_context_create(unsigned int flags) {
size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
- secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
+ secp256k1_context* ctx = checked_malloc(&default_error_callback, prealloc_size);
if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
free(ctx);
return NULL;
@@ -168,7 +168,7 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
ARG_CHECK(secp256k1_context_is_proper(ctx));
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
- ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
+ ret = checked_malloc(&ctx->error_callback, prealloc_size);
ret = secp256k1_context_preallocated_clone(ctx, ret);
return ret;
}
diff --git a/src/tests.c b/src/tests.c
index e09f5c7..23f1dc0 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -3676,8 +3676,8 @@ static void test_ge(void) {
* negation, and then those two again but with randomized Z coordinate.
* - The same is then done for lambda*p1 and lambda^2*p1.
*/
- secp256k1_ge *ge = (secp256k1_ge *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * (1 + 4 * runs));
- secp256k1_gej *gej = (secp256k1_gej *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_gej) * (1 + 4 * runs));
+ secp256k1_ge *ge = checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * (1 + 4 * runs));
+ secp256k1_gej *gej = checked_malloc(&CTX->error_callback, sizeof(secp256k1_gej) * (1 + 4 * runs));
secp256k1_fe zf, r;
secp256k1_fe zfi2, zfi3;
@@ -3811,7 +3811,7 @@ static void test_ge(void) {
/* Test adding all points together in random order equals infinity. */
{
secp256k1_gej sum = SECP256K1_GEJ_CONST_INFINITY;
- secp256k1_gej *gej_shuffled = (secp256k1_gej *)checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_gej));
+ secp256k1_gej *gej_shuffled = checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_gej));
for (i = 0; i < 4 * runs + 1; i++) {
gej_shuffled[i] = gej[i];
}
@@ -3832,8 +3832,8 @@ static void test_ge(void) {
/* Test batch gej -> ge conversion without known z ratios. */
{
- secp256k1_ge *ge_set_all_var = (secp256k1_ge *)checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
- secp256k1_ge *ge_set_all = (secp256k1_ge *)checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
+ secp256k1_ge *ge_set_all_var = checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
+ secp256k1_ge *ge_set_all = checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
secp256k1_ge_set_all_gej_var(&ge_set_all_var[0], &gej[0], 4 * runs + 1);
for (i = 0; i < 4 * runs + 1; i++) {
secp256k1_fe s;
@@ -5175,8 +5175,8 @@ static void test_ecmult_multi_batch_size_helper(void) {
static void test_ecmult_multi_batching(void) {
static const int n_points = 2*ECMULT_PIPPENGER_THRESHOLD;
secp256k1_scalar scG;
- secp256k1_scalar *sc = (secp256k1_scalar *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_scalar) * n_points);
- secp256k1_ge *pt = (secp256k1_ge *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * n_points);
+ secp256k1_scalar *sc = checked_malloc(&CTX->error_callback, sizeof(secp256k1_scalar) * n_points);
+ secp256k1_ge *pt = checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * n_points);
secp256k1_gej r;
secp256k1_gej r2;
ecmult_multi_data data;
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.