asn1: Use calloc() in generated C code

This commit is contained in:
Nicolas Williams
2022-01-12 17:01:37 -06:00
parent 1fe3d293e1
commit f0c46e7830
3 changed files with 7 additions and 7 deletions

View File

@@ -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"

View File

@@ -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 {

View File

@@ -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,