]> granicus.if.org Git - flex/commitdiff
escape backslashes in #line filenames in %top section; resolves #3212400; patch submi...
authorWill Estes <wlestes@users.sourceforge.net>
Fri, 23 Mar 2012 19:15:36 +0000 (19:15 +0000)
committerWill Estes <wlestes@users.sourceforge.net>
Fri, 23 Mar 2012 19:15:36 +0000 (19:15 +0000)
buf.c

diff --git a/buf.c b/buf.c
index c051295757ae6f6f7119898b62b4d0fdcba1d488..e5deb4e81c3a78e0ac3f93d4545f554c3f97a401 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -90,13 +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);
+    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"));
-    snprintf (t, tsz, fmt, lineno, filename);
+      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;