From a6b8c36a85314758aae83a910b4e3cfea8560376 Mon Sep 17 00:00:00 2001 From: John Millaway Date: Wed, 22 Mar 2006 12:49:41 +0000 Subject: [PATCH] Replaced sprintf with snprintf everywhere. --- buf.c | 20 ++++++++++++-------- filter.c | 2 +- gen.c | 6 +++--- main.c | 19 +++++++++---------- misc.c | 14 +++++++------- nfa.c | 8 ++++---- parse.y | 10 +++++----- regex.c | 2 +- 8 files changed, 42 insertions(+), 39 deletions(-) diff --git a/buf.c b/buf.c index fb3bbd1..33a5a9f 100644 --- a/buf.c +++ b/buf.c @@ -71,9 +71,10 @@ struct Buf *buf_print_strings(struct Buf * buf, FILE* out) struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) { char *t; + size_t tsz; - t = flex_alloc (strlen (fmt) + strlen (s) + 1); - sprintf (t, fmt, s); + t = flex_alloc (tsz = strlen (fmt) + strlen (s) + 1); + snprintf (t, tsz, fmt, s); buf = buf_strappend (buf, t); flex_free (t); return buf; @@ -88,9 +89,10 @@ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno) { char *t, *fmt = "#line %d \"%s\"\n"; + size_t tsz; - t = flex_alloc (strlen (fmt) + strlen (filename) + (int)(1 + log10(lineno>=0?lineno:-lineno)) + 1); - sprintf (t, fmt, lineno, filename); + t = flex_alloc (tsz = strlen (fmt) + strlen (filename) + (int)(1 + log10(lineno>=0?lineno:-lineno)) + 1); + snprintf (t, tsz, fmt, lineno, filename); buf = buf_strappend (buf, t); flex_free (t); return buf; @@ -156,11 +158,12 @@ struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val) { const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n"; char * str; + size_t strsz; val = val?val:""; - str = (char*)flex_alloc(strlen(fmt) + strlen(def) + strlen(val) + 2); + str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2); - sprintf(str, fmt, def, val); + snprintf(str, strsz, fmt, def, val); buf_append(buf, &str, 1); return buf; } @@ -174,10 +177,11 @@ struct Buf *buf_m4_undefine (struct Buf *buf, const char* def) { const char * fmt = "m4_undefine( [[%s]])m4_dnl\n"; char * str; + size_t strsz; - str = (char*)flex_alloc(strlen(fmt) + strlen(def) + 2); + str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + 2); - sprintf(str, fmt, def); + snprintf(str, strsz, fmt, def); buf_append(buf, &str, 1); return buf; } diff --git a/filter.c b/filter.c index e60e083..54fa50c 100644 --- a/filter.c +++ b/filter.c @@ -362,7 +362,7 @@ int filter_fix_linedirs (struct filter *chain) "") == 0) { /* Adjust the line directives. */ in_gen = true; - sprintf (buf, "#line %d \"%s\"\n", + snprintf (buf, readsz, "#line %d \"%s\"\n", lineno + 1, fname); free (fname); diff --git a/gen.c b/gen.c index f5318fd..99e9c8b 100644 --- a/gen.c +++ b/gen.c @@ -869,11 +869,11 @@ void gen_next_state (worry_about_NULs) if (worry_about_NULs && !nultrans) { if (useecs) - (void) sprintf (char_map, + snprintf (char_map, sizeof(char_map), "(*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : %d)", NUL_ec); else - (void) sprintf (char_map, + snprintf (char_map, sizeof(char_map), "(*yy_cp ? YY_SC_TO_UI(*yy_cp) : %d)", NUL_ec); } @@ -980,7 +980,7 @@ void gen_NUL_trans () else { char NUL_ec_str[20]; - (void) sprintf (NUL_ec_str, "%d", NUL_ec); + snprintf (NUL_ec_str, sizeof(NUL_ec_str), "%d", NUL_ec); gen_next_compressed_state (NUL_ec_str); do_indent (); diff --git a/main.c b/main.c index 0ead73a..a1283cb 100644 --- a/main.c +++ b/main.c @@ -344,7 +344,7 @@ void check_options () else suffix = "c"; - sprintf (outfile_path, outfile_template, + snprintf (outfile_path, sizeof(outfile_path), outfile_template, prefix, suffix); outfilename = outfile_path; @@ -393,11 +393,9 @@ void check_options () buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL); if (!tablesfilename) { - nbytes = strlen (prefix) + - strlen (tablesfile_template) + 2; - tablesfilename = pname = - (char *) calloc (nbytes, 1); - sprintf (pname, tablesfile_template, prefix); + nbytes = strlen (prefix) + strlen (tablesfile_template) + 2; + tablesfilename = pname = (char *) calloc (nbytes, 1); + snprintf (pname, nbytes, tablesfile_template, prefix); } if ((tablesout = fopen (tablesfilename, "w")) == NULL) @@ -410,7 +408,7 @@ void check_options () nbytes = strlen (prefix) + strlen ("tables") + 2; tablesname = (char *) calloc (nbytes, 1); - sprintf (tablesname, "%stables", prefix); + snprintf (tablesname, nbytes, "%stables", prefix); yytbl_hdr_init (&hdr, flex_version, tablesname); if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0) @@ -450,9 +448,10 @@ void check_options () buf_init(&tmpbuf, sizeof(char)); for (i = 1; i <= lastsc; i++) { char *str, *fmt = "#define %s %d\n"; + size_t strsz; - str = (char*)flex_alloc(strlen(fmt) + strlen(scname[i]) + (int)(1 + log10(i)) + 2); - sprintf(str, fmt, scname[i], i - 1); + str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(scname[i]) + (int)(1 + log10(i)) + 2); + snprintf(str, strsz, fmt, scname[i], i - 1); buf_strappend(&tmpbuf, str); free(str); } @@ -1778,7 +1777,7 @@ void usage () FILE *f = stdout; if (!did_outfilename) { - sprintf (outfile_path, outfile_template, + snprintf (outfile_path, sizeof(outfile_path), outfile_template, prefix, C_plus_plus ? "cc" : "c"); outfilename = outfile_path; } diff --git a/misc.c b/misc.c index b4dfb10..1009dea 100644 --- a/misc.c +++ b/misc.c @@ -102,7 +102,7 @@ void action_define (defname, value) return; } - sprintf (buf, "#define %s %d\n", defname, value); + snprintf (buf, sizeof(buf), "#define %s %d\n", defname, value); add_action (buf); /* track #defines so we can undef them when we're done. */ @@ -128,7 +128,7 @@ void action_m4_define (const char *defname, const char * value) return; } - sprintf (buf, "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:""); + snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:""); add_action (buf); } @@ -446,7 +446,7 @@ void lerrif (msg, arg) { char errmsg[MAXLINE]; - (void) sprintf (errmsg, msg, arg); + snprintf (errmsg, sizeof(errmsg), msg, arg); flexerror (errmsg); } @@ -458,7 +458,7 @@ void lerrsf (msg, arg) { char errmsg[MAXLINE]; - (void) sprintf (errmsg, msg, arg); + snprintf (errmsg, sizeof(errmsg), msg, arg); flexerror (errmsg); } @@ -495,13 +495,13 @@ void line_directive_out (output_file, do_infile) *s2 = '\0'; if (do_infile) - sprintf (directive, line_fmt, linenum, filename); + snprintf (directive, sizeof(directive), line_fmt, linenum, filename); else { if (output_file == stdout) /* Account for the line directive itself. */ ++out_linenum; - sprintf (directive, line_fmt, out_linenum, filename); + snprintf (directive, sizeof(directive), line_fmt, out_linenum, filename); } /* If output_file is nil then we should put the directive in @@ -843,7 +843,7 @@ char *readable_form (c) #endif default: - (void) sprintf (rform, "\\%.3o", (unsigned int) c); + snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c); return rform; } } diff --git a/nfa.c b/nfa.c index 09fedcf..dbd1557 100644 --- a/nfa.c +++ b/nfa.c @@ -222,10 +222,10 @@ void finish_rule (mach, variable_trail_rule, headcnt, trailcnt, if (pcont_act && rule_has_nl[num_rules - 1]) rule_has_nl[num_rules] = true; - sprintf (action_text, "case %d:\n", num_rules); + snprintf (action_text, sizeof(action_text), "case %d:\n", num_rules); add_action (action_text); if (rule_has_nl[num_rules]) { - sprintf (action_text, "/* rule %d can match eol */\n", + snprintf (action_text, sizeof(action_text), "/* rule %d can match eol */\n", num_rules); add_action (action_text); } @@ -257,13 +257,13 @@ void finish_rule (mach, variable_trail_rule, headcnt, trailcnt, ("*yy_cp = YY_G(yy_hold_char); /* undo effects of setting up yytext */\n"); if (headcnt > 0) { - sprintf (action_text, "%s = %s + %d;\n", + snprintf (action_text, sizeof(action_text), "%s = %s + %d;\n", scanner_cp, scanner_bp, headcnt); add_action (action_text); } else { - sprintf (action_text, "%s -= %d;\n", + snprintf (action_text, sizeof(action_text), "%s -= %d;\n", scanner_cp, trailcnt); add_action (action_text); } diff --git a/parse.y b/parse.y index c40d75b..0f56e04 100644 --- a/parse.y +++ b/parse.y @@ -930,7 +930,7 @@ void build_eof_action() else { sceof[scon_stk[i]] = true; - sprintf( action_text, "case YY_STATE_EOF(%s):\n", + snprintf( action_text, sizeof(action_text), "case YY_STATE_EOF(%s):\n", scname[scon_stk[i]] ); add_action( action_text ); } @@ -955,7 +955,7 @@ const char *msg, arg[]; { char errmsg[MAXLINE]; - (void) sprintf( errmsg, msg, arg ); + (void) snprintf( errmsg, sizeof(errmsg), msg, arg ); synerr( errmsg ); } @@ -977,7 +977,7 @@ const char *msg, arg[]; { char warn_msg[MAXLINE]; - (void) sprintf( warn_msg, msg, arg ); + snprintf( warn_msg, sizeof(warn_msg), msg, arg ); warn( warn_msg ); } @@ -999,7 +999,7 @@ const char *msg, arg[]; { char errmsg[MAXLINE]; - (void) sprintf( errmsg, msg, arg ); + snprintf( errmsg, sizeof(errmsg), msg, arg ); pinpoint_message( errmsg ); } @@ -1023,7 +1023,7 @@ int line; if ( ! nowarn ) { - sprintf( warning, "warning, %s", str ); + snprintf( warning, sizeof(warning), "warning, %s", str ); line_pinpoint( warning, line ); } } diff --git a/regex.c b/regex.c index d124b4b..5533971 100644 --- a/regex.c +++ b/regex.c @@ -59,7 +59,7 @@ void flex_regcomp(regex_t *preg, const char *regex, int cflags) errbuf = (char*)flex_alloc(errbuf_sz *sizeof(char)); regerror (err, preg, errbuf, errbuf_sz); - sprintf (errbuf, "regcomp failed: %s\n", errbuf); + snprintf (errbuf, errbuf_sz, "regcomp failed: %s\n", errbuf); flexfatal (errbuf); free(errbuf); -- 2.40.0