Skip to content

Commit d071aa5

Browse files
Merge #1815: refactor: remove unnecessary malloc result casts
97b3c47 refactor: remove unnecessary `malloc` result casts (Sebastian Falbesoner) Pull request description: While working on benchmark code for #1765, I noticed that in some instances we explicitly cast `malloc` results in the codebase. It seems that there is no good reason to do this in C, and it's even considered bad practice, see e.g. https://stackoverflow.com/a/605858. This commit touches mostly test code, the only two functions used in production are `secp256k1_context_{create,clone}`. Instances were found manually via `$ git grep "malloc("`. ACKs for top commit: real-or-random: Weak Concept ACK && Code Review ACK 97b3c47 w0xlt: ACK 97b3c47 Tree-SHA512: 74aa9f47eb52b7f2a6fcb69deb6aef0c0daa136c5deedfba1228218ef178c722212d8e9936fd2946d2035df932637ca4df49c98ddde488c6b009a74c4d5df316
2 parents 99ab4a1 + 97b3c47 commit d071aa5

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/modules/schnorrsig/bench_impl.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ static void run_schnorrsig_bench(int iters, int argc, char** argv) {
5151
int d = argc == 1;
5252

5353
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
54-
data.keypairs = (const secp256k1_keypair **)malloc(iters * sizeof(secp256k1_keypair *));
55-
data.pk = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
56-
data.msgs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
57-
data.sigs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
54+
data.keypairs = malloc(iters * sizeof(secp256k1_keypair *));
55+
data.pk = malloc(iters * sizeof(unsigned char *));
56+
data.msgs = malloc(iters * sizeof(unsigned char *));
57+
data.sigs = malloc(iters * sizeof(unsigned char *));
5858

5959
CHECK(MSGLEN >= 4);
6060
for (i = 0; i < iters; i++) {
6161
unsigned char sk[32];
62-
unsigned char *msg = (unsigned char *)malloc(MSGLEN);
63-
unsigned char *sig = (unsigned char *)malloc(64);
64-
secp256k1_keypair *keypair = (secp256k1_keypair *)malloc(sizeof(*keypair));
65-
unsigned char *pk_char = (unsigned char *)malloc(32);
62+
unsigned char *msg = malloc(MSGLEN);
63+
unsigned char *sig = malloc(64);
64+
secp256k1_keypair *keypair = malloc(sizeof(*keypair));
65+
unsigned char *pk_char = malloc(32);
6666
secp256k1_xonly_pubkey pk;
6767
msg[0] = sk[0] = i;
6868
msg[1] = sk[1] = i >> 8;

src/secp256k1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigne
140140

141141
secp256k1_context* secp256k1_context_create(unsigned int flags) {
142142
size_t const prealloc_size = secp256k1_context_preallocated_size(flags);
143-
secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size);
143+
secp256k1_context* ctx = checked_malloc(&default_error_callback, prealloc_size);
144144
if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) {
145145
free(ctx);
146146
return NULL;
@@ -168,7 +168,7 @@ secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) {
168168
ARG_CHECK(secp256k1_context_is_proper(ctx));
169169

170170
prealloc_size = secp256k1_context_preallocated_clone_size(ctx);
171-
ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size);
171+
ret = checked_malloc(&ctx->error_callback, prealloc_size);
172172
ret = secp256k1_context_preallocated_clone(ctx, ret);
173173
return ret;
174174
}

src/tests.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,8 +3676,8 @@ static void test_ge(void) {
36763676
* negation, and then those two again but with randomized Z coordinate.
36773677
* - The same is then done for lambda*p1 and lambda^2*p1.
36783678
*/
3679-
secp256k1_ge *ge = (secp256k1_ge *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * (1 + 4 * runs));
3680-
secp256k1_gej *gej = (secp256k1_gej *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_gej) * (1 + 4 * runs));
3679+
secp256k1_ge *ge = checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * (1 + 4 * runs));
3680+
secp256k1_gej *gej = checked_malloc(&CTX->error_callback, sizeof(secp256k1_gej) * (1 + 4 * runs));
36813681
secp256k1_fe zf, r;
36823682
secp256k1_fe zfi2, zfi3;
36833683

@@ -3811,7 +3811,7 @@ static void test_ge(void) {
38113811
/* Test adding all points together in random order equals infinity. */
38123812
{
38133813
secp256k1_gej sum = SECP256K1_GEJ_CONST_INFINITY;
3814-
secp256k1_gej *gej_shuffled = (secp256k1_gej *)checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_gej));
3814+
secp256k1_gej *gej_shuffled = checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_gej));
38153815
for (i = 0; i < 4 * runs + 1; i++) {
38163816
gej_shuffled[i] = gej[i];
38173817
}
@@ -3832,8 +3832,8 @@ static void test_ge(void) {
38323832

38333833
/* Test batch gej -> ge conversion without known z ratios. */
38343834
{
3835-
secp256k1_ge *ge_set_all_var = (secp256k1_ge *)checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
3836-
secp256k1_ge *ge_set_all = (secp256k1_ge *)checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
3835+
secp256k1_ge *ge_set_all_var = checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
3836+
secp256k1_ge *ge_set_all = checked_malloc(&CTX->error_callback, (4 * runs + 1) * sizeof(secp256k1_ge));
38373837
secp256k1_ge_set_all_gej_var(&ge_set_all_var[0], &gej[0], 4 * runs + 1);
38383838
for (i = 0; i < 4 * runs + 1; i++) {
38393839
secp256k1_fe s;
@@ -5175,8 +5175,8 @@ static void test_ecmult_multi_batch_size_helper(void) {
51755175
static void test_ecmult_multi_batching(void) {
51765176
static const int n_points = 2*ECMULT_PIPPENGER_THRESHOLD;
51775177
secp256k1_scalar scG;
5178-
secp256k1_scalar *sc = (secp256k1_scalar *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_scalar) * n_points);
5179-
secp256k1_ge *pt = (secp256k1_ge *)checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * n_points);
5178+
secp256k1_scalar *sc = checked_malloc(&CTX->error_callback, sizeof(secp256k1_scalar) * n_points);
5179+
secp256k1_ge *pt = checked_malloc(&CTX->error_callback, sizeof(secp256k1_ge) * n_points);
51805180
secp256k1_gej r;
51815181
secp256k1_gej r2;
51825182
ecmult_multi_data data;

0 commit comments

Comments
 (0)