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;
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;
{
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;
}
{
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;
}
"<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);
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);
}
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 ();
else
suffix = "c";
- sprintf (outfile_path, outfile_template,
+ snprintf (outfile_path, sizeof(outfile_path), outfile_template,
prefix, suffix);
outfilename = outfile_path;
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)
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)
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);
}
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;
}
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. */
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);
}
{
char errmsg[MAXLINE];
- (void) sprintf (errmsg, msg, arg);
+ snprintf (errmsg, sizeof(errmsg), msg, arg);
flexerror (errmsg);
}
{
char errmsg[MAXLINE];
- (void) sprintf (errmsg, msg, arg);
+ snprintf (errmsg, sizeof(errmsg), msg, arg);
flexerror (errmsg);
}
*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
#endif
default:
- (void) sprintf (rform, "\\%.3o", (unsigned int) c);
+ snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c);
return rform;
}
}
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);
}
("*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);
}
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 );
}
{
char errmsg[MAXLINE];
- (void) sprintf( errmsg, msg, arg );
+ (void) snprintf( errmsg, sizeof(errmsg), msg, arg );
synerr( errmsg );
}
{
char warn_msg[MAXLINE];
- (void) sprintf( warn_msg, msg, arg );
+ snprintf( warn_msg, sizeof(warn_msg), msg, arg );
warn( warn_msg );
}
{
char errmsg[MAXLINE];
- (void) sprintf( errmsg, msg, arg );
+ snprintf( errmsg, sizeof(errmsg), msg, arg );
pinpoint_message( errmsg );
}
if ( ! nowarn )
{
- sprintf( warning, "warning, %s", str );
+ snprintf( warning, sizeof(warning), "warning, %s", str );
line_pinpoint( warning, line );
}
}
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);