DPDK patches and discussions
 help / color / mirror / Atom feed
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>,
	stable@dpdk.org, Stephen Hemminger <stephen@networkplumber.org>,
	Anatoly Burakov <anatoly.burakov@intel.com>
Subject: [dpdk-dev] [PATCH v2 3/4] examples/mp_server: fix snprintf overflow
Date: Fri, 21 Aug 2020 18:10:16 +0100	[thread overview]
Message-ID: <20200821171017.50531-4-bruce.richardson@intel.com> (raw)
In-Reply-To: <20200821171017.50531-1-bruce.richardson@intel.com>

When producing a printable mac address the buffer was appropriately sized
for holding the mac address exactly, but the actual snprintf included a
'\n' character at the end, which means that the snprintf technically is
getting truncated i.e. the \n would not be added due to lack of space.
This gets flagged as a problem by modern versions of gcc, e.g. on Ubuntu
20.04.

main.c:77:37: warning: ‘__builtin___snprintf_chk’ output truncated before the last format character [-Wformat-truncation=]
   77 |     "%02x:%02x:%02x:%02x:%02x:%02x\n",
      |                                     ^

Since the \n is getting stripped anyway, we can fix the issue by just
removing it. In the process we can switch to using the standard ethernet
address formating function from rte_ether.h.

Fixes: af75078fece3 ("first public release")
Cc: stable@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>

---
V2: switched code to use standard formatting function
---
 .../client_server_mp/mp_server/main.c           | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/examples/multi_process/client_server_mp/mp_server/main.c b/examples/multi_process/client_server_mp/mp_server/main.c
index 280dab8672..fb2aa49678 100644
--- a/examples/multi_process/client_server_mp/mp_server/main.c
+++ b/examples/multi_process/client_server_mp/mp_server/main.c
@@ -59,12 +59,17 @@ static struct client_rx_buf *cl_rx_buf;
 static const char *
 get_printable_mac_addr(uint16_t port)
 {
-	static const char err_address[] = "00:00:00:00:00:00";
-	static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
+	static const struct rte_ether_addr null_mac; /* static defaults to 0 */
+	static char err_address[32];
+	static char addresses[RTE_MAX_ETHPORTS][32];
 	int ret;
 
-	if (unlikely(port >= RTE_MAX_ETHPORTS))
+	if (unlikely(port >= RTE_MAX_ETHPORTS)) {
+		if (err_address[0] == '\0')
+			rte_ether_format_addr(err_address,
+					sizeof(err_address), &null_mac);
 		return err_address;
+	}
 	if (unlikely(addresses[port][0]=='\0')){
 		struct rte_ether_addr mac;
 		ret = rte_eth_macaddr_get(port, &mac);
@@ -73,10 +78,8 @@ get_printable_mac_addr(uint16_t port)
 			       port, rte_strerror(-ret));
 			return err_address;
 		}
-		snprintf(addresses[port], sizeof(addresses[port]),
-				"%02x:%02x:%02x:%02x:%02x:%02x\n",
-				mac.addr_bytes[0], mac.addr_bytes[1], mac.addr_bytes[2],
-				mac.addr_bytes[3], mac.addr_bytes[4], mac.addr_bytes[5]);
+		rte_ether_format_addr(addresses[port],
+				sizeof(addresses[port]), &mac);
 	}
 	return addresses[port];
 }
-- 
2.25.1


  parent reply	other threads:[~2020-08-21 17:11 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-14 11:00 [dpdk-dev] [PATCH 0/4] fixes for example app builds Bruce Richardson
2020-08-14 11:00 ` [dpdk-dev] [PATCH 1/4] power: make guest channel headers public Bruce Richardson
2020-08-14 11:05   ` Bruce Richardson
2020-08-14 11:00 ` [dpdk-dev] [PATCH 2/4] examples/vm_power_manager: fix string truncation warning Bruce Richardson
2020-08-14 11:00 ` [dpdk-dev] [PATCH 3/4] examples/mp_server: fix snprintf overflow Bruce Richardson
2020-08-14 15:01   ` Stephen Hemminger
2020-08-14 11:00 ` [dpdk-dev] [PATCH 4/4] examples/mp_server: clear string truncation warning Bruce Richardson
2020-08-21 17:10 ` [dpdk-dev] [PATCH v2 0/4] fixes for example app builds Bruce Richardson
2020-08-21 17:10   ` [dpdk-dev] [PATCH v2 1/4] power: make guest channel headers public Bruce Richardson
2020-08-26 10:08     ` David Hunt
2020-10-05 21:44     ` [dpdk-dev] [dpdk-stable] " Thomas Monjalon
2020-08-21 17:10   ` [dpdk-dev] [PATCH v2 2/4] examples/vm_power_manager: fix string truncation warning Bruce Richardson
2020-08-26 10:09     ` David Hunt
2020-08-21 17:10   ` Bruce Richardson [this message]
2020-10-09 11:22     ` [dpdk-dev] [PATCH v2 3/4] examples/mp_server: fix snprintf overflow Nicolau, Radu
2020-08-21 17:10   ` [dpdk-dev] [PATCH v2 4/4] examples/mp_server: clear string truncation warning Bruce Richardson
2020-10-09 11:21     ` Nicolau, Radu
2020-10-28 16:26 ` [dpdk-dev] [PATCH v3 0/3] fixes for example app builds Bruce Richardson
2020-10-28 16:27   ` [dpdk-dev] [PATCH v3 1/3] examples/vm_power_manager: fix string truncation warning Bruce Richardson
2020-10-28 16:27   ` [dpdk-dev] [PATCH v3 2/3] examples/mp_server: fix snprintf overflow Bruce Richardson
2020-10-28 16:27   ` [dpdk-dev] [PATCH v3 3/3] examples/mp_server: clear string truncation warning Bruce Richardson
2020-10-30 13:56   ` [dpdk-dev] [PATCH v3 0/3] fixes for example app builds David Marchand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200821171017.50531-4-bruce.richardson@intel.com \
    --to=bruce.richardson@intel.com \
    --cc=anatoly.burakov@intel.com \
    --cc=dev@dpdk.org \
    --cc=stable@dpdk.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).