]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
erec: Avoid passing negative offset to fseek()
authorPhil Sutter <phil@nwl.cc>
Thu, 1 Mar 2018 14:00:28 +0000 (15:00 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 2 Mar 2018 10:46:56 +0000 (11:46 +0100)
If the initial call to ftell() fails, variable orig_offset is set to -1.
Avoid passing this to fseek() later on.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/erec.c

index 80806ffeee949fb7c2a6a4da5e330f587a713c51..8de249de8b78d095debb36e3f0734ca5a053049f 100644 (file)
@@ -121,7 +121,7 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
        char buf[1024] = {};
        char *pbuf = NULL;
        unsigned int i, end;
-       int l, ret;
+       int l;
        off_t orig_offset = 0;
        FILE *f = octx->output_fp;
 
@@ -136,12 +136,12 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
                break;
        case INDESC_FILE:
                orig_offset = ftell(indesc->fp);
-               fseek(indesc->fp, loc->line_offset, SEEK_SET);
-               ret = fread(buf, 1, sizeof(buf) - 1, indesc->fp);
-               if (ret > 0)
+               if (orig_offset >= 0 &&
+                   !fseek(indesc->fp, loc->line_offset, SEEK_SET) &&
+                   fread(buf, 1, sizeof(buf) - 1, indesc->fp) > 0 &&
+                   !fseek(indesc->fp, orig_offset, SEEK_SET))
                        *strchrnul(buf, '\n') = '\0';
                line = buf;
-               fseek(indesc->fp, orig_offset, SEEK_SET);
                break;
        case INDESC_INTERNAL:
        case INDESC_NETLINK: