]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
libnftables: Ensure output_fp is never NULL
authorPhil Sutter <phil@nwl.cc>
Mon, 20 Nov 2017 15:54:04 +0000 (16:54 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 22 Nov 2017 12:18:34 +0000 (13:18 +0100)
Initialize output_fp to 'stdout' upon context creation and check output
stream validity in nft_ctx_set_output(). This allows to drop checks in
nft_{gmp_,}print() and do_command_export(). While doing so for the
latter, simplify it a bit by using nft_print() which takes care of
flushing the output stream.

If applications desire to drop all output, they are supposed to open
/dev/null and assign that.

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

index e8fa6742f7d174ff2f3a2ed5d693b07f36f4aa7b..c86d89477e778ae6aa483d1026fa71966bc20b40 100644 (file)
@@ -167,6 +167,7 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
        ctx->parser_max_errors  = 10;
        init_list_head(&ctx->cache.list);
        ctx->flags = flags;
+       ctx->output.output_fp = stdout;
 
        if (flags == NFT_CTX_DEFAULT)
                nft_ctx_netlink_init(ctx);
@@ -190,6 +191,9 @@ FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp)
 {
        FILE *old = ctx->output.output_fp;
 
+       if (!fp || ferror(fp))
+               return NULL;
+
        ctx->output.output_fp = fp;
 
        return old;
@@ -333,9 +337,6 @@ int nft_print(struct output_ctx *octx, const char *fmt, ...)
        int ret;
        va_list arg;
 
-       if (!octx->output_fp)
-               return -1;
-
        va_start(arg, fmt);
        ret = vfprintf(octx->output_fp, fmt, arg);
        va_end(arg);
@@ -349,9 +350,6 @@ int nft_gmp_print(struct output_ctx *octx, const char *fmt, ...)
        int ret;
        va_list arg;
 
-       if (!octx->output_fp)
-               return -1;
-
        va_start(arg, fmt);
        ret = gmp_vfprintf(octx->output_fp, fmt, arg);
        va_end(arg);
index ff7878c94ccb323074e172c999ea645f061869fc..353b87bc66631a000ddb2989053c57a975b8479e 100644 (file)
@@ -173,7 +173,6 @@ int main(int argc, char * const *argv)
        int i, val, rc;
 
        nft = nft_ctx_new(NFT_CTX_DEFAULT);
-       nft_ctx_set_output(nft, stdout);
 
        while (1) {
                val = getopt_long(argc, argv, OPTSTRING, options, NULL);
index eb91be4636e214e1042e3dab2851e0cffc4ac2a0..37d99c22004713c7134e76aa46cc4ef734d7891c 100644 (file)
@@ -1153,9 +1153,6 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)
        struct nftnl_ruleset *rs;
        FILE *fp = ctx->octx->output_fp;
 
-       if (!fp)
-               return 0;
-
        do {
                rs = netlink_dump_ruleset(ctx, &cmd->handle, &cmd->location);
                if (rs == NULL && errno != EINTR)
@@ -1163,8 +1160,7 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)
        } while (rs == NULL);
 
        nftnl_ruleset_fprintf(fp, rs, cmd->export->format, 0);
-       fprintf(fp, "\n");
-       fflush(fp);
+       nft_print(ctx->octx, "\n");
 
        nftnl_ruleset_free(rs);
        return 0;