patches for DPDK stable branches
 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>,
	Radu Nicolau <radu.nicolau@intel.com>,
	Anatoly Burakov <anatoly.burakov@intel.com>
Subject: [dpdk-stable] [PATCH v3 2/3] examples/mp_server: fix snprintf overflow
Date: Wed, 28 Oct 2020 16:27:01 +0000
Message-ID: <20201028162702.969509-3-bruce.richardson@intel.com> (raw)
In-Reply-To: <20201028162702.969509-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>
Acked-by: Radu Nicolau <radu.nicolau@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 ec7f6b11f..b18e12dd4 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-10-28 16:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20200814110045.217724-1-bruce.richardson@intel.com>
2020-08-14 11:00 ` [dpdk-stable] [PATCH 1/4] power: make guest channel headers public Bruce Richardson
2020-08-14 11:05   ` Bruce Richardson
2020-08-14 11:00 ` [dpdk-stable] [PATCH 2/4] examples/vm_power_manager: fix string truncation warning Bruce Richardson
2020-08-14 11:00 ` [dpdk-stable] [PATCH 3/4] examples/mp_server: fix snprintf overflow Bruce Richardson
2020-08-14 15:01   ` [dpdk-stable] [dpdk-dev] " Stephen Hemminger
2020-08-14 11:00 ` [dpdk-stable] [PATCH 4/4] examples/mp_server: clear string truncation warning Bruce Richardson
     [not found] ` <20200821171017.50531-1-bruce.richardson@intel.com>
2020-08-21 17:10   ` [dpdk-stable] [PATCH v2 1/4] power: make guest channel headers public Bruce Richardson
2020-08-26 10:08     ` David Hunt
2020-10-05 21:44     ` Thomas Monjalon
2020-08-21 17:10   ` [dpdk-stable] [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   ` [dpdk-stable] [PATCH v2 3/4] examples/mp_server: fix snprintf overflow Bruce Richardson
2020-10-09 11:22     ` [dpdk-stable] [dpdk-dev] " Nicolau, Radu
2020-08-21 17:10   ` [dpdk-stable] [PATCH v2 4/4] examples/mp_server: clear string truncation warning Bruce Richardson
2020-10-09 11:21     ` [dpdk-stable] [dpdk-dev] " Nicolau, Radu
     [not found] ` <20201028162702.969509-1-bruce.richardson@intel.com>
2020-10-28 16:27   ` [dpdk-stable] [PATCH v3 1/3] examples/vm_power_manager: fix " Bruce Richardson
2020-10-28 16:27   ` Bruce Richardson [this message]
2020-10-28 16:27   ` [dpdk-stable] [PATCH v3 3/3] examples/mp_server: clear " Bruce Richardson

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=20201028162702.969509-3-bruce.richardson@intel.com \
    --to=bruce.richardson@intel.com \
    --cc=anatoly.burakov@intel.com \
    --cc=dev@dpdk.org \
    --cc=radu.nicolau@intel.com \
    --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

patches for DPDK stable branches

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.dpdk.org/stable/0 stable/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 stable stable/ https://inbox.dpdk.org/stable \
		stable@dpdk.org
	public-inbox-index stable

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.dpdk.org/inbox.dpdk.stable


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git