buffer: use nftnl_expr_snprintf() from nftnl_buf_expr()
Pablo Neira Ayuso
maintainerPATCH 1/3 buffer: use nftnl_expr_snprintf() from nftnl_buf_expr()
This helper function deals with no ->snprintf() indirection in expression and safe buffer nul-termination, use it.
Fixes 059b9bf6fb31 ("src: Use nftnl_buf to export XML/JSON rules")
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/buffer.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
src/buffer.c +3 -2
@@ -14,6 +14,7 @@#include <string.h> #include <buffer.h> #include <libnftnl/common.h> +#include <libnftnl/expr.h> #include "internal.h" int nftnl_buf_update (struct nftnl_buf *b,int ret)
@@ -206,8 +207,8 @@ int nftnl_buf_expr(struct nftnl_buf *b, int type, uint32_t flags,case NFTNL_OUTPUT_JSON :nftnl_buf_put (b,"{" );nftnl_buf_str (b, type, expr->ops->name,TYPE ); - ret = expr->ops->snprintf (b->buf + b->off, b->len, type, flags, - expr); + ret =nftnl_expr_snprintf (b->buf + b->off, b->len, expr, type, + flags);if (ret >0 )nftnl_buf_update (b, ret);else
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/3 expr: no need to nul-terminate buffer from expression ->snprintf indirection
->snprintf() is always called via nftnl_expr_snprintf() wrapper, which is already dealing with this corner case for us.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/expr/bitwise.c | 3 --- src/expr/byteorder.c | 3 --- src/expr/cmp.c | 3 --- src/expr/counter.c | 3 --- src/expr/ct.c | 3 --- src/expr/dup.c | 3 --- src/expr/dynset.c | 3 --- src/expr/exthdr.c | 3 --- src/expr/fib.c | 3 --- src/expr/fwd.c | 3 --- src/expr/hash.c | 3 --- src/expr/immediate.c | 3 --- src/expr/limit.c | 3 --- src/expr/log.c | 3 --- src/expr/lookup.c | 3 --- src/expr/masq.c | 3 --- src/expr/match.c | 3 --- src/expr/meta.c | 3 --- src/expr/nat.c | 3 --- src/expr/numgen.c | 3 --- src/expr/objref.c | 3 --- src/expr/payload.c | 3 --- src/expr/queue.c | 3 --- src/expr/quota.c | 3 --- src/expr/range.c | 3 --- src/expr/redir.c | 3 --- src/expr/reject.c | 3 --- src/expr/rt.c | 3 --- src/expr/target.c | 3 --- 29 files changed, 87 deletions(-)
src/expr/bitwise.c +0 -3
@@ -257,9 +257,6 @@ static int nftnl_expr_bitwise_snprintf(char *buf, size_t size, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_bitwise_snprintf_default (buf, size, e);
src/expr/byteorder.c +0 -3
@@ -272,9 +272,6 @@ static int nftnl_expr_byteorder_snprintf(char *buf, size_t size, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_byteorder_snprintf_default (buf, size, e);
src/expr/cmp.c +0 -3
@@ -246,9 +246,6 @@ static int nftnl_expr_cmp_snprintf(char *buf, size_t size, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_cmp_snprintf_default (buf, size, e);
src/expr/counter.c +0 -3
@@ -164,9 +164,6 @@ static int nftnl_expr_counter_snprintf(char *buf, size_t len, uint32_t type,uint32_t flags, const struct nftnl_expr *e) { - if (len) - buf[0 ] ='\0' ; - switch (type) { case NFTNL_OUTPUT_DEFAULT: return nftnl_expr_counter_snprintf_default(buf, len, e);
src/expr/ct.c +0 -3
@@ -317,9 +317,6 @@ static int nftnl_expr_ct_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_ct_snprintf_default (buf, len, e);
src/expr/dup.c +0 -3
@@ -170,9 +170,6 @@ static int nftnl_expr_dup_snprintf_default(char *buf, size_t len,static int nftnl_expr_dup_snprintf (char *buf,size_t len,uint32_t type,uint32_t flags,const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_dup_snprintf_default (buf, len, e, flags);
src/expr/dynset.c +0 -3
@@ -314,9 +314,6 @@ static int nftnl_expr_dynset_snprintf(char *buf, size_t size, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_dynset_snprintf_default (buf, size, e);
src/expr/exthdr.c +0 -3
@@ -339,9 +339,6 @@ static int nftnl_expr_exthdr_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_exthdr_snprintf_default (buf, len, e);
src/expr/fib.c +0 -3
@@ -236,9 +236,6 @@ static int nftnl_expr_fib_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_fib_snprintf_default (buf, len, e);
src/expr/fwd.c +0 -3
@@ -147,9 +147,6 @@ static int nftnl_expr_fwd_snprintf_default(char *buf, size_t len,static int nftnl_expr_fwd_snprintf (char *buf,size_t len,uint32_t type,uint32_t flags,const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_fwd_snprintf_default (buf, len, e, flags);
src/expr/hash.c +0 -3
@@ -288,9 +288,6 @@ static int nftnl_expr_hash_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_hash_snprintf_default (buf, len, e);
src/expr/immediate.c +0 -3
@@ -262,9 +262,6 @@ static int nftnl_expr_immediate_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_immediate_snprintf_default (buf, len, e, flags);
src/expr/limit.c +0 -3
@@ -243,9 +243,6 @@ static int nftnl_expr_limit_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_limit_snprintf_default (buf, len, e);
src/expr/log.c +0 -3
@@ -301,9 +301,6 @@ static int nftnl_expr_log_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_log_snprintf_default (buf, len, e);
src/expr/lookup.c +0 -3
@@ -242,9 +242,6 @@ static int nftnl_expr_lookup_snprintf(char *buf, size_t size, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_lookup_snprintf_default (buf, size, e);
src/expr/masq.c +0 -3
@@ -190,9 +190,6 @@ static int nftnl_expr_masq_snprintf_default(char *buf, size_t len,static int nftnl_expr_masq_snprintf (char *buf,size_t len,uint32_t type,uint32_t flags,const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_masq_snprintf_default (buf, len, e);
src/expr/match.c +0 -3
@@ -200,9 +200,6 @@ nftnl_expr_match_snprintf(char *buf, size_t len, uint32_t type, {struct nftnl_expr_match *match =nftnl_expr_data (e); -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT : return snprintf(buf, len, "name %s rev %u ",
src/expr/meta.c +0 -3
@@ -252,9 +252,6 @@ static int nftnl_expr_meta_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_meta_snprintf_default (buf, len, e);
src/expr/nat.c +0 -3
@@ -339,9 +339,6 @@ static int nftnl_expr_nat_snprintf(char *buf, size_t size, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_nat_snprintf_default (buf, size, e);
src/expr/numgen.c +0 -3
@@ -224,9 +224,6 @@ static int nftnl_expr_ng_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_ng_snprintf_default (buf, len, e);
src/expr/objref.c +0 -3
@@ -236,9 +236,6 @@ static int nftnl_expr_objref_snprintf(char *buf, size_t len, uint32_t type,uint32_t flags, const struct nftnl_expr *e) { - if (len) - buf[0 ] ='\0' ; - switch (type) { case NFTNL_OUTPUT_DEFAULT: return nftnl_expr_objref_snprintf_default(buf, len, e);
src/expr/payload.c +0 -3
@@ -290,9 +290,6 @@ nftnl_expr_payload_snprintf(char *buf, size_t len, uint32_t type, {struct nftnl_expr_payload *payload =nftnl_expr_data (e); -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :if (payload->sreg)
src/expr/queue.c +0 -3
@@ -235,9 +235,6 @@ static int nftnl_expr_queue_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_queue_snprintf_default (buf, len, e);
src/expr/quota.c +0 -3
@@ -183,9 +183,6 @@ static int nftnl_expr_quota_snprintf(char *buf, size_t len, uint32_t type,uint32_t flags, const struct nftnl_expr *e) { - if (len) - buf[0 ] ='\0' ; - switch (type) { case NFTNL_OUTPUT_DEFAULT: return nftnl_expr_quota_snprintf_default(buf, len, e);
src/expr/range.c +0 -3
@@ -263,9 +263,6 @@ static int nftnl_expr_range_snprintf_default(char *buf, size_t size,static int nftnl_expr_range_snprintf (char *buf,size_t size,uint32_t type,uint32_t flags,const struct nftnl_expr *e) { -if (size) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_range_snprintf_default (buf, size, e);
src/expr/redir.c +0 -3
@@ -204,9 +204,6 @@ static int nftnl_expr_redir_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_redir_snprintf_default (buf, len, e);
src/expr/reject.c +0 -3
@@ -164,9 +164,6 @@ static int nftnl_expr_reject_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_reject_snprintf_default (buf, len, e);
src/expr/rt.c +0 -3
@@ -203,9 +203,6 @@ static int nftnl_expr_rt_snprintf(char *buf, size_t len, uint32_t type, uint32_t flags, const struct nftnl_expr *e) { -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT :return nftnl_expr_rt_snprintf_default (buf, len, e);
src/expr/target.c +0 -3
@@ -200,9 +200,6 @@ nftnl_expr_target_snprintf(char *buf, size_t len, uint32_t type, {struct nftnl_expr_target *target =nftnl_expr_data (e); -if (len) - buf[0 ] ='\0' ; -switch (type) {case NFTNL_OUTPUT_DEFAULT : return snprintf(buf, len, "name %s rev %u ",
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/3 src: no need to nul-terminate buffer for nftnl_fprintf() calls
In reply to Pablo Neira AyusoThis function bails out with -1 if we cannot print, and this buffer is internally allocated. No need for this overly deffensive initialization.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/chain.c | 3 --- src/gen.c | 3 --- src/object.c | 3 --- src/rule.c | 3 --- src/set.c | 3 --- src/set_elem.c | 3 --- src/table.c | 3 --- 7 files changed, 21 deletions(-)
src/chain.c +0 -3
@@ -867,9 +867,6 @@ EXPORT_SYMBOL(nftnl_chain_snprintf);static int nftnl_chain_do_snprintf (char *buf,size_t size,const void *c,uint32_t cmd,uint32_t type,uint32_t flags) { -if (size) - buf[0 ] ='\0' ; -return nftnl_chain_snprintf (buf, size, c, type, flags); }
src/gen.c +0 -3
@@ -203,9 +203,6 @@ EXPORT_SYMBOL(nftnl_gen_snprintf);static int nftnl_gen_do_snprintf (char *buf,size_t size,const void *gen,uint32_t cmd,uint32_t type,uint32_t flags) { -if (size) - buf ='\0' ; -return nftnl_gen_snprintf (buf, size, gen, type, flags); }
src/object.c +0 -3
@@ -488,9 +488,6 @@ EXPORT_SYMBOL(nftnl_obj_snprintf);static int nftnl_obj_do_snprintf (char *buf,size_t size,const void *obj,uint32_t cmd,uint32_t type,uint32_t flags) { -if (size) - buf[0 ] ='\0' ; -return nftnl_obj_snprintf (buf, size, obj, type, flags); }
src/rule.c +0 -3
@@ -836,9 +836,6 @@ EXPORT_SYMBOL(nftnl_rule_snprintf);static int nftnl_rule_do_snprintf (char *buf,size_t size,const void *r,uint32_t cmd,uint32_t type,uint32_t flags) { -if (size) - buf[0 ] ='\0' ; -return nftnl_rule_snprintf (buf, size, r, type, flags); }
src/set.c +0 -3
@@ -939,9 +939,6 @@ EXPORT_SYMBOL(nftnl_set_snprintf);static int nftnl_set_do_snprintf (char *buf,size_t size,const void *s,uint32_t cmd,uint32_t type,uint32_t flags) { -if (size) - buf[0 ] ='\0' ; -return nftnl_set_snprintf (buf, size, s, type, flags); }
src/set_elem.c +0 -3
@@ -738,9 +738,6 @@ static int nftnl_set_elem_do_snprintf(char *buf, size_t size, const void *e, uint32_t cmd, uint32_t type, uint32_t flags) { - if (size) - buf[0 ] ='\0' ; - return nftnl_set_elem_snprintf(buf, size, e, type, flags); }
src/table.c +0 -3
@@ -417,9 +417,6 @@ EXPORT_SYMBOL(nftnl_table_snprintf);static int nftnl_table_do_snprintf (char *buf,size_t size,const void *t,uint32_t cmd,uint32_t type,uint32_t flags) { -if (size) - buf[0 ] ='\0' ; -return nftnl_table_snprintf (buf, size, t, type, flags); }
Signature · 1 line
2.1.4