From 0ece5566df239cfc380d927efd0a94fc71dd9f3e Mon Sep 17 00:00:00 2001 From: wlestes Date: Fri, 3 Feb 2012 22:32:35 +0000 Subject: more better error messages; more better memory handling --- buf.c | 8 ++++++++ filter.c | 32 ++++++++++++++++++++++++-------- main.c | 2 ++ misc.c | 20 ++++++++++++++++++-- regex.c | 6 +++++- scanflags.c | 3 +++ 6 files changed, 60 insertions(+), 11 deletions(-) diff --git a/buf.c b/buf.c index 33a5a9f..c051295 100644 --- a/buf.c +++ b/buf.c @@ -74,6 +74,8 @@ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) size_t tsz; t = flex_alloc (tsz = strlen (fmt) + strlen (s) + 1); + if (!t) + flexfatal (_("Allocation of buffer to print string failed")); snprintf (t, tsz, fmt, s); buf = buf_strappend (buf, t); flex_free (t); @@ -92,6 +94,8 @@ struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno) size_t tsz; t = flex_alloc (tsz = strlen (fmt) + strlen (filename) + (int)(1 + log10(lineno>=0?lineno:-lineno)) + 1); + if (!t) + flexfatal (_("Allocation of buffer for line directive failed")); snprintf (t, tsz, fmt, lineno, filename); buf = buf_strappend (buf, t); flex_free (t); @@ -162,6 +166,8 @@ struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val) val = val?val:""; str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2); + if (!str) + flexfatal (_("Allocation of buffer for m4 def failed")); snprintf(str, strsz, fmt, def, val); buf_append(buf, &str, 1); @@ -180,6 +186,8 @@ struct Buf *buf_m4_undefine (struct Buf *buf, const char* def) size_t strsz; str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + 2); + if (!str) + flexfatal (_("Allocation of buffer for m4 undef failed")); snprintf(str, strsz, fmt, def); buf_append(buf, &str, 1); diff --git a/filter.c b/filter.c index dad7396..c82f7f8 100644 --- a/filter.c +++ b/filter.c @@ -48,6 +48,8 @@ struct filter *filter_create_ext (struct filter *chain, const char *cmd, /* allocate and initialize new filter */ f = (struct filter *) flex_alloc (sizeof (struct filter)); + if (!f) + flexerror (_("flex_alloc failed (f) in filter_create_ext")); memset (f, 0, sizeof (*f)); f->filter_func = NULL; f->extra = NULL; @@ -67,6 +69,8 @@ struct filter *filter_create_ext (struct filter *chain, const char *cmd, f->argv = (const char **) flex_alloc (sizeof (char *) * (max_args + 1)); + if (!f->argv) + flexerror (_("flex_alloc failed (f->argv) in filter_create_ext")); f->argv[f->argc++] = cmd; va_start (ap, cmd); @@ -104,6 +108,8 @@ struct filter *filter_create_int (struct filter *chain, /* allocate and initialize new filter */ f = (struct filter *) flex_alloc (sizeof (struct filter)); + if (!f) + flexerror (_("flex_alloc failed in filter_create_int")); memset (f, 0, sizeof (*f)); f->next = NULL; f->argc = 0; @@ -129,6 +135,10 @@ struct filter *filter_create_int (struct filter *chain, bool filter_apply_chain (struct filter * chain) { int pid, pipes[2]; + int r; + const int readsz = 512; + char *buf; + /* Tricky recursion, since we want to begin the chain * at the END. Why? Because we need all the forked processes @@ -145,6 +155,7 @@ bool filter_apply_chain (struct filter * chain) fflush (stdout); fflush (stderr); + if (pipe (pipes) == -1) flexerror (_("pipe failed")); @@ -178,7 +189,8 @@ clearerr(stdin); else { execvp (chain->argv[0], (char **const) (chain->argv)); - flexfatal (_("exec failed")); + lerrsf_fatal ( _("exec of %s failed"), + chain->argv[0]); } exit (1); @@ -280,6 +292,8 @@ int filter_tee_header (struct filter *chain) outfilename ? outfilename : ""); buf = (char *) flex_alloc (readsz); + if (!buf) + flexerror (_("flex_alloc failed in filter_tee_header")); while (fgets (buf, readsz, stdin)) { fputs (buf, to_c); if (write_header) @@ -297,13 +311,13 @@ int filter_tee_header (struct filter *chain) fputs ("m4_undefine( [[M4_YY_IN_HEADER]])m4_dnl\n", to_h); fflush (to_h); - if (ferror (to_h)) - lerrsf (_("error writing output file %s"), - (char *) chain->extra); + if (ferror (to_h)) + lerrsf (_("error writing output file %s"), + (char *) chain->extra); - else if (fclose (to_h)) - lerrsf (_("error closing output file %s"), - (char *) chain->extra); + else if (fclose (to_h)) + lerrsf (_("error closing output file %s"), + (char *) chain->extra); } fflush (to_c); @@ -339,6 +353,8 @@ int filter_fix_linedirs (struct filter *chain) return 0; buf = (char *) flex_alloc (readsz); + if (!buf) + flexerror (_("flex_alloc failed in filter_fix_linedirs")); while (fgets (buf, readsz, stdin)) { @@ -346,7 +362,7 @@ int filter_fix_linedirs (struct filter *chain) /* Check for #line directive. */ if (buf[0] == '#' - && regexec (®ex_linedir, buf, 3, m, 0) == 0) { + && regexec (®ex_linedir, buf, 3, m, 0) == 0) { int num; char *fname; diff --git a/main.c b/main.c index 0a9f256..493c636 100644 --- a/main.c +++ b/main.c @@ -455,6 +455,8 @@ void check_options () size_t strsz; str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(scname[i]) + (int)(1 + log10(i)) + 2); + if (!str) + flexfatal(_("allocation of macro definition failed")); snprintf(str, strsz, fmt, scname[i], i - 1); buf_strappend(&tmpbuf, str); free(str); diff --git a/misc.c b/misc.c index a65a50a..d819c61 100644 --- a/misc.c +++ b/misc.c @@ -61,6 +61,8 @@ static void sko_push(bool dc) if(!sko_stack){ sko_sz = 1; sko_stack = (struct sko_state*)flex_alloc(sizeof(struct sko_state)*sko_sz); + if (!sko_stack) + flexfatal(_("allocation of sko_stack failed")); sko_len = 0; } if(sko_len >= sko_sz){ @@ -454,15 +456,29 @@ void lerrif (msg, arg) /* lerrsf - report an error message formatted with one string argument */ void lerrsf (msg, arg) - const char *msg, arg[]; + const char *msg, arg[]; { char errmsg[MAXLINE]; - snprintf (errmsg, sizeof(errmsg), msg, arg); + snprintf (errmsg, sizeof(errmsg)-1, msg, arg); + errmsg[sizeof(errmsg)-1] = 0; /* ensure NULL termination */ flexerror (errmsg); } +/* lerrsf_fatal - as lerrsf, but call flexfatal */ + +void lerrsf_fatal (msg, arg) + const char *msg, arg[]; +{ + char errmsg[MAXLINE]; + + snprintf (errmsg, sizeof(errmsg)-1, msg, arg); + errmsg[sizeof(errmsg)-1] = 0; /* ensure NULL termination */ + flexfatal (errmsg); +} + + /* line_directive_out - spit out a "#line" statement */ void line_directive_out (output_file, do_infile) diff --git a/regex.c b/regex.c index 5533971..f5b9603 100644 --- a/regex.c +++ b/regex.c @@ -57,7 +57,9 @@ void flex_regcomp(regex_t *preg, const char *regex, int cflags) const int errbuf_sz = 200; char * errbuf=0; - errbuf = (char*)flex_alloc(errbuf_sz *sizeof(char)); + errbuf = (char*)flex_alloc(errbuf_sz *sizeof(char)); + if (!errbuf) + flexfatal(_("Unable to allocate buffer to report regcomp failed")); regerror (err, preg, errbuf, errbuf_sz); snprintf (errbuf, errbuf_sz, "regcomp failed: %s\n", errbuf); @@ -80,6 +82,8 @@ char *regmatch_dup (regmatch_t * m, const char *src) return NULL; len = m->rm_eo - m->rm_so; str = (char *) flex_alloc ((len + 1) * sizeof (char)); + if (!str) + flexfatal(_("Unable to allocate a copy of the match")); strncpy (str, src + m->rm_so, len); str[len] = 0; return str; diff --git a/scanflags.c b/scanflags.c index 20ff501..f75aa82 100644 --- a/scanflags.c +++ b/scanflags.c @@ -60,6 +60,9 @@ sf_init (void) { assert(_sf_stk == NULL); _sf_stk = (scanflags_t*) flex_alloc ( sizeof(scanflags_t) * (_sf_max = 32)); + if (!_sf_stk) + lerrsf_fatal(_("Unable to allocate %ld of stack"), + (long)sizeof(scanflags_t)); _sf_stk[_sf_top_ix] = 0; } -- cgit v1.2.1