From f0c46e78306ad9c6ede3155c0fab73c00e57ae3c Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Wed, 12 Jan 2022 17:01:37 -0600 Subject: [PATCH] asn1: Use calloc() in generated C code --- lib/asn1/gen.c | 2 +- lib/asn1/gen_copy.c | 8 ++++---- lib/asn1/gen_encode.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/asn1/gen.c b/lib/asn1/gen.c index 69cc79da1..8dd337a46 100644 --- a/lib/asn1/gen.c +++ b/lib/asn1/gen.c @@ -258,7 +258,7 @@ init_generate (const char *filename, const char *base) fputs("#define ASN1_MALLOC_ENCODE(T, B, BL, S, L, R) \\\n" " do { \\\n" " (BL) = length_##T((S)); \\\n" - " (B) = malloc((BL)); \\\n" + " (B) = calloc(1, (BL)); \\\n" " if((B) == NULL) { \\\n" " *(L) = 0; \\\n" " (R) = ENOMEM; \\\n" diff --git a/lib/asn1/gen_copy.c b/lib/asn1/gen_copy.c index 133a4bbae..c1d963209 100644 --- a/lib/asn1/gen_copy.c +++ b/lib/asn1/gen_copy.c @@ -125,7 +125,7 @@ copy_type (const char *from, const char *to, const Type *t, int preserve) errx(1, "malloc"); if(m->optional){ fprintf(codefile, "if(%s) {\n", fs); - fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts); + fprintf(codefile, "%s = calloc(1, sizeof(*%s));\n", ts, ts); fprintf(codefile, "if(%s == NULL) goto fail;\n", ts); used_fail++; } @@ -161,7 +161,7 @@ copy_type (const char *from, const char *to, const Type *t, int preserve) char *f = NULL, *T = NULL; fprintf (codefile, "if(((%s)->val = " - "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n", + "calloc(1, (%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n", to, from, to, from); fprintf (codefile, "goto fail;\n"); used_fail++; @@ -256,7 +256,7 @@ generate_type_copy (const Symbol *s) /* Decorated with field of external type w/ copy function */ if (deco.ptr) { fprintf(codefile, "if (from->%s) {\n", deco.field_name); - fprintf(codefile, "(to)->%s = malloc(sizeof(*(to)->%s));\n", + fprintf(codefile, "(to)->%s = calloc(1, sizeof(*(to)->%s));\n", deco.field_name, deco.field_name); fprintf(codefile, "if (%s((from)->%s, (to)->%s)) goto fail;\n", deco.copy_function_name, deco.field_name, deco.field_name); @@ -268,7 +268,7 @@ generate_type_copy (const Symbol *s) } else if (deco.opt) { /* Decorated with optional field of ASN.1 type */ fprintf(codefile, "if (from->%s) {\n", deco.field_name); - fprintf(codefile, "(to)->%s = malloc(sizeof(*(to)->%s));\n", deco.field_name, deco.field_name); + fprintf(codefile, "(to)->%s = calloc(1, sizeof(*(to)->%s));\n", deco.field_name, deco.field_name); fprintf(codefile, "if (copy_%s((from)->%s, (to)->%s)) goto fail;\n", deco.field_type, deco.field_name, deco.field_name); fprintf(codefile, "}\n"); } else { diff --git a/lib/asn1/gen_encode.c b/lib/asn1/gen_encode.c index de917d408..168a5a5dd 100644 --- a/lib/asn1/gen_encode.c +++ b/lib/asn1/gen_encode.c @@ -313,7 +313,7 @@ encode_type (const char *name, const Type *t, const char *tmpstr) name); fprintf(codefile, - "val = malloc(sizeof(val[0]) * (%s)->len);\n" + "val = calloc(1, sizeof(val[0]) * (%s)->len);\n" "if (val == NULL && (%s)->len != 0) return ENOMEM;\n", name, name); @@ -465,7 +465,7 @@ encode_type (const char *name, const Type *t, const char *tmpstr) "size_t l2_%s, lensave_%s = len;\n" "len = length_%s(%s);\n" /* Allocate a temp buffer for the encoder */ - "if ((p = pfree_%s = malloc(len)) == NULL) return ENOMEM;\n" + "if ((p = pfree_%s = calloc(1, len)) == NULL) return ENOMEM;\n" /* Make p point to the last byte of the allocated buf */ "p += len - 1;\n", tmpstr, tmpstr, tmpstr, tmpstr,