catch error from as.*printf

This commit is contained in:
Love Hornquist Astrand
2010-05-30 13:28:09 -07:00
parent 9773655027
commit 351e0d0914
5 changed files with 51 additions and 41 deletions

View File

@@ -46,7 +46,7 @@ static int
doit(const char *fn) doit(const char *fn)
{ {
char buf[2048]; char buf[2048];
char *fnout; char *fnout = NULL;
const char *bname; const char *bname;
unsigned long line = 0; unsigned long line = 0;
FILE *f, *fout; FILE *f, *fout;
@@ -62,8 +62,7 @@ doit(const char *fn)
else else
bname = fn; bname = fn;
asprintf(&fnout, "%s.out", bname); if (asprintf(&fnout, "%s.out", bname) < 0 || fnout == NULL)
if (fnout == NULL)
errx(1, "malloc"); errx(1, "malloc");
fout = fopen(fnout, "w"); fout = fopen(fnout, "w");

View File

@@ -1007,7 +1007,8 @@ static void fix_labels1(struct memhead *members, const char *prefix)
if(members == NULL) if(members == NULL)
return; return;
ASN1_TAILQ_FOREACH(m, members, members) { ASN1_TAILQ_FOREACH(m, members, members) {
asprintf(&m->label, "%s_%s", prefix, m->gen_name); if (asprintf(&m->label, "%s_%s", prefix, m->gen_name) < 0)
errx(1, "malloc");
if (m->label == NULL) if (m->label == NULL)
errx(1, "malloc"); errx(1, "malloc");
if(m->type != NULL) if(m->type != NULL)
@@ -1024,9 +1025,8 @@ static void fix_labels2(Type *t, const char *prefix)
static void static void
fix_labels(Symbol *s) fix_labels(Symbol *s)
{ {
char *p; char *p = NULL;
asprintf(&p, "choice_%s", s->gen_name); if (asprintf(&p, "choice_%s", s->gen_name) < 0 || p == NULL)
if (p == NULL)
errx(1, "malloc"); errx(1, "malloc");
fix_labels2(s->type, p); fix_labels2(s->type, p);
free(p); free(p);

View File

@@ -77,7 +77,8 @@ test_integer (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "integer %d", values[i]); if (asprintf (&tests[i].name, "integer %d", values[i]) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }
@@ -200,7 +201,8 @@ test_unsigned (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "unsigned %u", values[i]); if (asprintf (&tests[i].name, "unsigned %u", values[i]) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }
@@ -241,7 +243,8 @@ test_octet_string (void)
int ret; int ret;
tests[0].val = &s1; tests[0].val = &s1;
asprintf (&tests[0].name, "a octet string"); if (asprintf (&tests[0].name, "a octet string") < 0)
errx(1, "malloc");
if (tests[0].name == NULL) if (tests[0].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
@@ -282,11 +285,13 @@ test_bmp_string (void)
int ret; int ret;
tests[0].val = &s1; tests[0].val = &s1;
asprintf (&tests[0].name, "a bmp string"); if (asprintf (&tests[0].name, "a bmp string") < 0)
errx(1, "malloc");
if (tests[0].name == NULL) if (tests[0].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
tests[1].val = &s2; tests[1].val = &s2;
asprintf (&tests[1].name, "second bmp string"); if (asprintf (&tests[1].name, "second bmp string") < 0)
errx(1, "malloc");
if (tests[1].name == NULL) if (tests[1].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
@@ -328,11 +333,13 @@ test_universal_string (void)
int ret; int ret;
tests[0].val = &s1; tests[0].val = &s1;
asprintf (&tests[0].name, "a universal string"); if (asprintf (&tests[0].name, "a universal string") < 0)
errx(1, "malloc");
if (tests[0].name == NULL) if (tests[0].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
tests[1].val = &s2; tests[1].val = &s2;
asprintf (&tests[1].name, "second universal string"); if (asprintf (&tests[1].name, "second universal string") < 0)
errx(1, "malloc");
if (tests[1].name == NULL) if (tests[1].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
@@ -368,7 +375,8 @@ test_general_string (void)
int ret, ntests = sizeof(tests) / sizeof(*tests); int ret, ntests = sizeof(tests) / sizeof(*tests);
tests[0].val = &s1; tests[0].val = &s1;
asprintf (&tests[0].name, "the string \"%s\"", s1); if (asprintf (&tests[0].name, "the string \"%s\"", s1) < 0)
errx(1, "malloc");
if (tests[0].name == NULL) if (tests[0].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
@@ -405,7 +413,8 @@ test_generalized_time (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "time %d", (int)values[i]); if (asprintf (&tests[i].name, "time %d", (int)values[i]) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }
@@ -453,7 +462,8 @@ test_oid (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "oid %d", i); if (asprintf (&tests[i].name, "oid %d", i) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }
@@ -490,7 +500,8 @@ test_bit_string (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "bit_string %d", i); if (asprintf (&tests[i].name, "bit_string %d", i) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }
@@ -542,7 +553,8 @@ test_heim_integer (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "heim_integer %d", i); if (asprintf (&tests[i].name, "heim_integer %d", i) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }
@@ -592,7 +604,8 @@ test_boolean (void)
for (i = 0; i < ntests; ++i) { for (i = 0; i < ntests; ++i) {
tests[i].val = &values[i]; tests[i].val = &values[i];
asprintf (&tests[i].name, "heim_boolean %d", i); if (asprintf (&tests[i].name, "heim_boolean %d", i) < 0)
errx(1, "malloc");
if (tests[i].name == NULL) if (tests[i].name == NULL)
errx(1, "malloc"); errx(1, "malloc");
} }

View File

@@ -126,23 +126,20 @@ init_generate (const char *filename, const char *base)
} }
/* public header file */ /* public header file */
asprintf(&header, "%s.h", headerbase); if (asprintf(&header, "%s.h", headerbase) < 0 || header == NULL)
if (header == NULL)
errx(1, "malloc"); errx(1, "malloc");
asprintf(&fn, "%s.hx", headerbase); if (asprintf(&fn, "%s.hx", headerbase) < 0 || fn == NULL)
if (fn == NULL)
errx(1, "malloc"); errx(1, "malloc");
headerfile = fopen (fn, "w"); headerfile = fopen (fn, "w");
if (headerfile == NULL) if (headerfile == NULL)
err (1, "open %s", fn); err (1, "open %s", fn);
free(fn); free(fn);
fn = NULL;
/* private header file */ /* private header file */
asprintf(&privheader, "%s-priv.h", headerbase); if (asprintf(&privheader, "%s-priv.h", headerbase) < 0 || privheader == NULL)
if (privheader == NULL)
errx(1, "malloc"); errx(1, "malloc");
asprintf(&fn, "%s-priv.hx", headerbase); if (asprintf(&fn, "%s-priv.hx", headerbase) < 0 || fn == NULL)
if (fn == NULL)
errx(1, "malloc"); errx(1, "malloc");
privheaderfile = fopen (fn, "w"); privheaderfile = fopen (fn, "w");
if (privheaderfile == NULL) if (privheaderfile == NULL)
@@ -150,10 +147,8 @@ init_generate (const char *filename, const char *base)
free(fn); free(fn);
/* template file */ /* template file */
asprintf(&template, "%s-template.c", headerbase); if (asprintf(&template, "%s-template.c", headerbase) < 0 || template == NULL)
if (template == NULL)
errx(1, "malloc"); errx(1, "malloc");
fprintf (headerfile, fprintf (headerfile,
"/* Generated from %s */\n" "/* Generated from %s */\n"
"/* Do not edit */\n\n", "/* Do not edit */\n\n",

View File

@@ -110,14 +110,16 @@ copy_type (const char *from, const char *to, const Type *t, int preserve)
if(t->type == TChoice) if(t->type == TChoice)
fprintf(codefile, "case %s:\n", m->label); fprintf(codefile, "case %s:\n", m->label);
asprintf (&fs, "%s(%s)->%s%s", if (asprintf (&fs, "%s(%s)->%s%s",
m->optional ? "" : "&", from, m->optional ? "" : "&", from,
t->type == TChoice ? "u." : "", m->gen_name); t->type == TChoice ? "u." : "", m->gen_name) < 0)
errx(1, "malloc");
if (fs == NULL) if (fs == NULL)
errx(1, "malloc"); errx(1, "malloc");
asprintf (&ts, "%s(%s)->%s%s", if (asprintf (&ts, "%s(%s)->%s%s",
m->optional ? "" : "&", to, m->optional ? "" : "&", to,
t->type == TChoice ? "u." : "", m->gen_name); t->type == TChoice ? "u." : "", m->gen_name) < 0)
errx(1, "malloc");
if (ts == NULL) if (ts == NULL)
errx(1, "malloc"); errx(1, "malloc");
if(m->optional){ if(m->optional){
@@ -155,8 +157,7 @@ copy_type (const char *from, const char *to, const Type *t, int preserve)
} }
case TSetOf: case TSetOf:
case TSequenceOf: { case TSequenceOf: {
char *f; char *f = NULL, *T = NULL;
char *T;
fprintf (codefile, "if(((%s)->val = " fprintf (codefile, "if(((%s)->val = "
"malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n", "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
@@ -166,10 +167,12 @@ copy_type (const char *from, const char *to, const Type *t, int preserve)
fprintf(codefile, fprintf(codefile,
"for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n", "for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
to, to, from, to); to, to, from, to);
asprintf(&f, "&(%s)->val[(%s)->len]", from, to); if (asprintf(&f, "&(%s)->val[(%s)->len]", from, to) < 0)
errx(1, "malloc");
if (f == NULL) if (f == NULL)
errx(1, "malloc"); errx(1, "malloc");
asprintf(&T, "&(%s)->val[(%s)->len]", to, to); if (asprintf(&T, "&(%s)->val[(%s)->len]", to, to) < 0)
errx(1, "malloc");
if (T == NULL) if (T == NULL)
errx(1, "malloc"); errx(1, "malloc");
copy_type(f, T, t->subtype, FALSE); copy_type(f, T, t->subtype, FALSE);