]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
scanner: don't rely on fseek for input stream repositioning
authorFlorian Westphal <fw@strlen.de>
Sun, 21 Jul 2019 10:18:31 +0000 (12:18 +0200)
committerFlorian Westphal <fw@strlen.de>
Tue, 30 Jul 2019 13:46:36 +0000 (15:46 +0200)
It doesn't work when reading from a pipe, leading to parser
errors in case of 'cat foo | nft -f -', whereas 'nft -f < foo'
works fine.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1354
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/scanner.l

index 4ed5f9241381234277295a5671d66c0895e75104..c1adcbddbd7302949380a43598d15d79e8d1f761 100644 (file)
  */
 #define YY_INPUT(buf,result,max_size)                                          \
 {                                                                              \
-       long n = 0;                                                             \
+       result = 0;                                                             \
        errno = 0;                                                              \
-       while ((result = fread(buf, 1, max_size, yyin)) == 0 &&                 \
-               ferror(yyin)) {                                                 \
-               if (errno != EINTR) {                                           \
-                       YY_FATAL_ERROR("input in flex scanner failed");         \
-                       break;                                                  \
+                                                                               \
+       while (result < max_size) {                                             \
+               int chr = fgetc(yyin);                                          \
+                                                                               \
+               if (chr != EOF) {                                               \
+                       buf[result++] = chr;                                    \
+                       if (chr == '\n' || chr == ' ')                          \
+                               break;                                          \
+                       continue;                                               \
                }                                                               \
-               errno = 0;                                                      \
-               clearerr(yyin);                                                 \
-       }                                                                       \
-       if (result > 1 && !feof(yyin)) {                                        \
-               while (result > 1 &&                                            \
-                      (buf[result - 1] != '\n' &&  buf[result - 1] != ' '))    \
-                       result--, n++;                                          \
-               result--, n++;                                                  \
-               fseek(yyin, -n, SEEK_CUR);                                      \
+                                                                               \
+               if (ferror(yyin)) {                                             \
+                       if (errno != EINTR) {                                   \
+                               YY_FATAL_ERROR("input in flex scanner failed"); \
+                               break;                                          \
+                       }                                                       \
+                       errno = 0;                                              \
+                       clearerr(yyin);                                         \
+               }                                                               \
+               break;                                                          \
        }                                                                       \
 }