From 26bb2525c89ecda0b0bc7b597ec8d1b792fc8662 Mon Sep 17 00:00:00 2001 From: Manoj Srivastava Date: Wed, 9 Apr 2014 00:15:48 -0700 Subject: Imported Upstream version 2.5.39 --- buf.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'buf.c') diff --git a/buf.c b/buf.c index 33a5a9f..e5deb4e 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); @@ -88,11 +90,20 @@ 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 (tsz = strlen (fmt) + strlen (filename) + (int)(1 + log10(lineno>=0?lineno:-lineno)) + 1); - snprintf (t, tsz, fmt, lineno, filename); + char *dst, *src, *t; + + t = flex_alloc (strlen ("#line \"\"\n") + /* constant parts */ + 2 * strlen (filename) + /* filename with possibly all backslashes escaped */ + (int) (1 + log10 (abs (lineno))) + /* line number */ + 1); /* NUL */ + if (!t) + flexfatal (_("Allocation of buffer for line directive failed")); + for (dst = t + sprintf (t, "#line %d \"", lineno), src = filename; *src; *dst++ = *src++) + if (*src == '\\') /* escape backslashes */ + *dst++ = '\\'; + *dst++ = '"'; + *dst++ = '\n'; + *dst = '\0'; buf = buf_strappend (buf, t); flex_free (t); return buf; @@ -162,6 +173,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 +193,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); -- cgit v1.2.3