Skip to content

Commit

Permalink
examples/multi_process: fix build on Ubuntu 20.04
Browse files Browse the repository at this point in the history
[ upstream commit 27b549c ]

Two warnings are reported by gcc 9.3.0 on Ubuntu 20.04.

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 formatting function from rte_ether.h.

The other warning is about possible string truncation when getting the
RX queue name:

In file included from init.c:36:
init.c: In function ‘init’:
../shared/common.h:38:28: warning: ‘%u’ directive output may be truncated
  writing between 1 and 10 bytes into a region of size 8
  [-Wformat-truncation=]
   38 | #define MP_CLIENT_RXQ_NAME "MProc_Client_%u_RX"
      |                            ^~~~~~~~~~~~~~~~~~~~
../shared/common.h:52:35: note: in expansion of macro ‘MP_CLIENT_RXQ_NAME’
   52 |  snprintf(buffer, sizeof(buffer), MP_CLIENT_RXQ_NAME, id);
      |                                   ^~~~~~~~~~~~~~~~~~

This is a false positive, as the value of the "id" is limited to 255,
being stored in the app as a uint8_t value, removing the possibility of
the %u being replaced by anything other then 3 characters max (rather than
up to 10 as thought by the compiler). Therefore, the warning can be easily
removed by changing the type of the "id" parameter to the local function
from "unsigned" to "uint8_t" also, ensuring the compiler is aware of the
range limit.

Fixes: af75078 ("first public release")

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Radu Nicolau <radu.nicolau@intel.com>
  • Loading branch information
bruce-richardson authored and bluca committed Nov 5, 2020
1 parent d14d8f2 commit 200a950
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions examples/multi_process/client_server_mp/mp_server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];
}
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_process/client_server_mp/shared/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct port_info {
* Given the rx queue name template above, get the queue name
*/
static inline const char *
get_rx_queue_name(unsigned id)
get_rx_queue_name(uint8_t id)
{
/* buffer for return value. Size calculated by %u being replaced
* by maximum 3 digits (plus an extra byte for safety) */
Expand Down

0 comments on commit 200a950

Please sign in to comment.