Skip to content

Commit

Permalink
hinic: avoid gcc -Wrestrict warning
Browse files Browse the repository at this point in the history
With extra warnings enabled, gcc complains that snprintf should not
take the same buffer as source and destination:

drivers/net/ethernet/huawei/hinic/hinic_ethtool.c: In function 'hinic_set_settings_to_hw':
drivers/net/ethernet/huawei/hinic/hinic_ethtool.c:480:9: error: 'snprintf' argument 4 overlaps destination object 'set_link_str' [-Werror=restrict]
  480 |   err = snprintf(set_link_str, SET_LINK_STR_MAX_LEN,
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  481 |           "%sspeed %d ", set_link_str, speed);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/ethernet/huawei/hinic/hinic_ethtool.c:464:7: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  464 |  char set_link_str[SET_LINK_STR_MAX_LEN] = {0};

Rewrite this to avoid the nested sprintf and instead use separate
buffers, which is simpler.

Cc: Rasmus Villemoes <[email protected]>
Signed-off-by: Arnd Bergmann <[email protected]>
Reviewed-by: Rasmus Villemoes <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
arndb authored and davem330 committed Mar 25, 2021
1 parent 5a55861 commit 84c7f6c
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions drivers/net/ethernet/huawei/hinic/hinic_ethtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "hinic_rx.h"
#include "hinic_dev.h"

#define SET_LINK_STR_MAX_LEN 128
#define SET_LINK_STR_MAX_LEN 16

#define GET_SUPPORTED_MODE 0
#define GET_ADVERTISED_MODE 1
Expand Down Expand Up @@ -462,24 +462,19 @@ static int hinic_set_settings_to_hw(struct hinic_dev *nic_dev,
{
struct hinic_link_ksettings_info settings = {0};
char set_link_str[SET_LINK_STR_MAX_LEN] = {0};
const char *autoneg_str;
struct net_device *netdev = nic_dev->netdev;
enum nic_speed_level speed_level = 0;
int err;

err = snprintf(set_link_str, SET_LINK_STR_MAX_LEN, "%s",
(set_settings & HILINK_LINK_SET_AUTONEG) ?
(autoneg ? "autong enable " : "autong disable ") : "");
if (err < 0 || err >= SET_LINK_STR_MAX_LEN) {
netif_err(nic_dev, drv, netdev, "Failed to snprintf link state, function return(%d) and dest_len(%d)\n",
err, SET_LINK_STR_MAX_LEN);
return -EFAULT;
}
autoneg_str = (set_settings & HILINK_LINK_SET_AUTONEG) ?
(autoneg ? "autong enable " : "autong disable ") : "";

if (set_settings & HILINK_LINK_SET_SPEED) {
speed_level = hinic_ethtool_to_hw_speed_level(speed);
err = snprintf(set_link_str, SET_LINK_STR_MAX_LEN,
"%sspeed %d ", set_link_str, speed);
if (err <= 0 || err >= SET_LINK_STR_MAX_LEN) {
"speed %d ", speed);
if (err >= SET_LINK_STR_MAX_LEN) {
netif_err(nic_dev, drv, netdev, "Failed to snprintf link speed, function return(%d) and dest_len(%d)\n",
err, SET_LINK_STR_MAX_LEN);
return -EFAULT;
Expand All @@ -494,11 +489,11 @@ static int hinic_set_settings_to_hw(struct hinic_dev *nic_dev,
err = hinic_set_link_settings(nic_dev->hwdev, &settings);
if (err != HINIC_MGMT_CMD_UNSUPPORTED) {
if (err)
netif_err(nic_dev, drv, netdev, "Set %s failed\n",
set_link_str);
netif_err(nic_dev, drv, netdev, "Set %s%sfailed\n",
autoneg_str, set_link_str);
else
netif_info(nic_dev, drv, netdev, "Set %s successfully\n",
set_link_str);
netif_info(nic_dev, drv, netdev, "Set %s%ssuccessfully\n",
autoneg_str, set_link_str);

return err;
}
Expand Down

0 comments on commit 84c7f6c

Please sign in to comment.