Replaced sprintf with snprintf everywhere.
authorJohn Millaway <john43@users.sourceforge.net>
Wed, 22 Mar 2006 12:49:41 +0000 (12:49 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Wed, 22 Mar 2006 12:49:41 +0000 (12:49 +0000)
buf.c
filter.c
gen.c
main.c
misc.c
nfa.c
parse.y
regex.c

diff --git a/buf.c b/buf.c
index fb3bbd1e0e342998767e4dc3476b8f03916f7749..33a5a9f0b2b3a7f7fb617420112082098f7efc10 100644 (file)
--- 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;
 }
index e60e0830d5ab3b2f5cccaaecc3b1ef1c8acb9c8b..54fa50cd23a0aff430706921394c36af5228043d 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -362,7 +362,7 @@ int filter_fix_linedirs (struct filter *chain)
                                       "<stdout>") == 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 f5318fd809787b2bc3c1316d0e3fe4d2f04b87b0..99e9c8b0511ff922254107979f03b26f0aa9183b 100644 (file)
--- 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 0ead73aa8aa68164283661f2022f7cbb5495d732..a1283cbdb2cd4c6d7669afc841275ad05ba56fd9 100644 (file)
--- 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 b4dfb10cc042a117b059fc795910d56647491e2f..1009dea948b194a49888038e129891ac01c3b7ac 100644 (file)
--- 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 09fedcf65a494c106fcdae3be80600f5cce76c37..dbd15578b77ceb9d8de82a33baa04ca8f15f88c3 100644 (file)
--- 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 c40d75b7f0ef20db1b1613dd480bbe700a2202cf..0f56e0479c3aba80f09e3d997594ebb254d4aec6 100644 (file)
--- 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 d124b4bd312057e5d85cb2dee102125bda885b12..553397174f10672e9873c5a09ade0f82a4aa83b0 100644 (file)
--- 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);