summaryrefslogtreecommitdiff
path: root/buf.c
diff options
context:
space:
mode:
authorManoj Srivastava <srivasta@golden-gryphon.com>2014-04-09 00:15:48 -0700
committerManoj Srivastava <srivasta@golden-gryphon.com>2014-04-09 00:15:48 -0700
commit26bb2525c89ecda0b0bc7b597ec8d1b792fc8662 (patch)
treeb2ecc519218e259a4fb5e5f0a72b7e00bc3a2b43 /buf.c
parentada7779d8b7a39d01dd70d733883db2383e61a6e (diff)
Imported Upstream version 2.5.39
Diffstat (limited to 'buf.c')
-rw-r--r--buf.c25
1 files changed, 20 insertions, 5 deletions
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);