From b775a05ba376c68615fe5134ce291889c035f844 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Sun, 11 Jul 2010 13:22:29 +0200 Subject: nfq: consistent nfq_snprintf_xml() return value with snprintf() With this patch, nfq_snprintf_xml() returns the number of characters printed. If the output was truncated, then the return value is the number of characters that would have been written if enough space had been available. This makes nfq_snprintf_xml() consistent with the behaviour of snprintf(). Signed-off-by: Pablo Neira Ayuso --- src/libnetfilter_queue.c | 119 ++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/src/libnetfilter_queue.c b/src/libnetfilter_queue.c index bb2771a..7da16be 100644 --- a/src/libnetfilter_queue.c +++ b/src/libnetfilter_queue.c @@ -1012,24 +1012,27 @@ int nfq_get_payload(struct nfq_data *nfad, unsigned char **data) return -1; } -#define SNPRINTF_FAILURE(size, len, offset) \ +#define SNPRINTF_FAILURE(ret, rem, offset, len) \ do { \ - if (size < 0 || (unsigned int) size >= len) \ - return size; \ - offset += size; \ - len -= size; \ + if (ret < 0) \ + return ret; \ + len += ret; \ + if (ret > rem) \ + ret = rem; \ + offset += ret; \ + rem -= ret; \ } while (0) -int nfq_snprintf_xml(char *buf, size_t len, struct nfq_data *tb, int flags) +int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags) { struct nfqnl_msg_packet_hdr *ph; struct nfqnl_msg_packet_hw *hwph; u_int32_t mark, ifi; - int size, offset = 0, ret; + int size, offset = 0, len = 0, ret; unsigned char *data; - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); if (flags & NFQ_XML_TIME) { time_t t; @@ -1039,128 +1042,128 @@ int nfq_snprintf_xml(char *buf, size_t len, struct nfq_data *tb, int flags) if (localtime_r(&t, &tm) == NULL) return -1; - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, + size = snprintf(buf + offset, rem, "%d", tm.tm_hour); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); size = snprintf(buf + offset, - len, "%02d", tm.tm_min); - SNPRINTF_FAILURE(size, len, offset); + rem, "%02d", tm.tm_min); + SNPRINTF_FAILURE(size, rem, offset, len); size = snprintf(buf + offset, - len, "%02d", tm.tm_sec); - SNPRINTF_FAILURE(size, len, offset); + rem, "%02d", tm.tm_sec); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, "%d", + size = snprintf(buf + offset, rem, "%d", tm.tm_wday + 1); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, "%d", tm.tm_mday); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, "%d", tm.tm_mday); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, "%d", + size = snprintf(buf + offset, rem, "%d", tm.tm_mon + 1); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, "%d", + size = snprintf(buf + offset, rem, "%d", 1900 + tm.tm_year); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); } ph = nfq_get_msg_packet_hdr(tb); if (ph) { - size = snprintf(buf + offset, len, + size = snprintf(buf + offset, rem, "%u%u", ph->hook, ntohl(ph->packet_id)); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); hwph = nfq_get_packet_hw(tb); if (hwph && (flags & NFQ_XML_HW)) { int i, hlen = ntohs(hwph->hw_addrlen); - size = snprintf(buf + offset, len, "0x%04x" + size = snprintf(buf + offset, rem, "0x%04x" "", ntohs(ph->hw_protocol)); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); for (i=0; ihw_protocol)); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); } - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); } else if (flags & NFQ_XML_HW) { - size = snprintf(buf + offset, len, "0x%04x" + size = snprintf(buf + offset, rem, "0x%04x" "", ntohs(ph->hw_protocol)); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); } } mark = nfq_get_nfmark(tb); if (mark && (flags & NFQ_XML_MARK)) { - size = snprintf(buf + offset, len, "%u", mark); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, "%u", mark); + SNPRINTF_FAILURE(size, rem, offset, len); } ifi = nfq_get_indev(tb); if (ifi && (flags & NFQ_XML_DEV)) { - size = snprintf(buf + offset, len, "%u", ifi); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, "%u", ifi); + SNPRINTF_FAILURE(size, rem, offset, len); } ifi = nfq_get_outdev(tb); if (ifi && (flags & NFQ_XML_DEV)) { - size = snprintf(buf + offset, len, "%u", ifi); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, "%u", ifi); + SNPRINTF_FAILURE(size, rem, offset, len); } ifi = nfq_get_physindev(tb); if (ifi && (flags & NFQ_XML_PHYSDEV)) { - size = snprintf(buf + offset, len, + size = snprintf(buf + offset, rem, "%u", ifi); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); } ifi = nfq_get_physoutdev(tb); if (ifi && (flags & NFQ_XML_PHYSDEV)) { - size = snprintf(buf + offset, len, + size = snprintf(buf + offset, rem, "%u", ifi); - SNPRINTF_FAILURE(size, len, offset); + SNPRINTF_FAILURE(size, rem, offset, len); } ret = nfq_get_payload(tb, &data); if (ret >= 0 && (flags & NFQ_XML_PAYLOAD)) { int i; - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); for (i=0; i"); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); } - size = snprintf(buf + offset, len, ""); - SNPRINTF_FAILURE(size, len, offset); + size = snprintf(buf + offset, rem, ""); + SNPRINTF_FAILURE(size, rem, offset, len); - return size; + return len; } /** -- cgit v1.2.3