summaryrefslogtreecommitdiff
path: root/buf.c
diff options
context:
space:
mode:
authorAnas Nashif <anas.nashif@intel.com>2012-11-04 18:18:17 -0800
committerAnas Nashif <anas.nashif@intel.com>2012-11-04 18:18:17 -0800
commit58a8c0edfa213572fd6ef7ba7705c4031d6f7928 (patch)
tree73012bd2124c90bd9db7dee490cb4e3e2f02e700 /buf.c
parent0960e23e38178233fc295c3d87760f6f8fac58bf (diff)
parentb8577c30fcc6e962e341f5ab3402197cd9bb1124 (diff)
downloadflex-58a8c0edfa213572fd6ef7ba7705c4031d6f7928.tar.gz
flex-58a8c0edfa213572fd6ef7ba7705c4031d6f7928.tar.bz2
flex-58a8c0edfa213572fd6ef7ba7705c4031d6f7928.zip
Merge commit 'upstream/2.5.37'
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);