]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib/bpf: Fix bytecode-file parsing
authorPhil Sutter <phil@nwl.cc>
Tue, 29 Aug 2017 15:09:45 +0000 (17:09 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Mon, 4 Sep 2017 19:06:50 +0000 (12:06 -0700)
The signedness of char type is implementation dependent, and there are
architectures on which it is unsigned by default. In that case, the
check whether fgetc() returned EOF failed because the return value was
assigned an (unsigned) char variable prior to comparison with EOF (which
is defined to -1). Fix this by using int as type for 'c' variable, which
also matches the declaration of fgetc().

While being at it, fix the parser logic to correctly handle multiple
empty lines and consecutive whitespace and tab characters to further
improve the parser's robustness. Note that this will still detect double
separator characters, so doesn't soften up the parser too much.

Fixes: 3da3ebfca85b8 ("bpf: Make bytecode-file reading a little more robust")
Cc: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
lib/bpf.c

index c180934acc7dc3d422c88e777bbc448df69ab8d6..5fd4928c7e6e48e070458664f30b0f40a0ebf4b0 100644 (file)
--- a/lib/bpf.c
+++ b/lib/bpf.c
@@ -208,8 +208,9 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
 
        if (from_file) {
                size_t tmp_len, op_len = sizeof("65535 255 255 4294967295,");
-               char *tmp_string, *pos, c, c_prev = ' ';
+               char *tmp_string, *pos, c_prev = ' ';
                FILE *fp;
+               int c;
 
                tmp_len = sizeof("4096,") + BPF_MAXINSNS * op_len;
                tmp_string = pos = calloc(1, tmp_len);
@@ -228,18 +229,20 @@ static int bpf_parse_string(char *arg, bool from_file, __u16 *bpf_len,
                        case '\n':
                                if (c_prev != ',')
                                        *(pos++) = ',';
+                               c_prev = ',';
                                break;
                        case ' ':
                        case '\t':
                                if (c_prev != ' ')
                                        *(pos++) = c;
+                               c_prev = ' ';
                                break;
                        default:
                                *(pos++) = c;
+                               c_prev = c;
                        }
                        if (pos - tmp_string == tmp_len)
                                break;
-                       c_prev = c;
                }
 
                if (!feof(fp)) {