Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp344932pxf; Wed, 24 Mar 2021 06:34:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzNfxbNcyK8DBkG7RjWhZqhlqLHdqTmUheajgl7psJaiqAzmf82bnMBMk5C7a88QfUV3gA X-Received: by 2002:a05:6402:10c9:: with SMTP id p9mr3535572edu.268.1616592878729; Wed, 24 Mar 2021 06:34:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616592878; cv=none; d=google.com; s=arc-20160816; b=IOF6D0SlWWgH2nH/SVmvtZ8jKwS+V7c7NyYNCrU94Lyc0vODSlTeZVPSPvRX4f34xc V7YgNfbbJ3TotKoeKsqSwFM5ui67+Y9944J+bYRir8pzVR2erLnKRv804oEIxMrNCERm yrPd9fdc8iH9nUL1HwYPGmh8rxf2+Qx53qt2zKkWsnj+L2Z2ZCSb8n+cRAWsnZPfg37P 3d2PvtV82dEEHi3Ta7c5ljQBRr0l+AGgMkA+OPvP8Z5UUXrqfgIaxXZzwRRGc+G6CvMb STz/S7hAjPYd0YjzNuMs7U8pbl8lkGZscMeXxHBtuU6NSfpyLo9JRK7KB8m4s2UUF36o j9SQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=PgVEYZSY9NfCWwR2RJxuZCY5immorWw9IvrU7FEyMZY=; b=Cn+zPxY8lTTzHE+msclg62gli+75q7B1aGrVyjlZ36lPjUfnrlCRNywsMKajBhXXUW dywMZGcs3+fGKBD25dCQg1Eo89f6BK8AbWFjhgVyY4PABcT+MvoiUESD+0Bub3/x5T8v 2SwNC+Go0o53CiV0nbyhFZPd+d2rGUYG1HpwK6WFUKFv/kkJSKXUkepGT5uYScS5pnpE LnmhbLrH9BzVIuAT7FYcwlGzXtFuQSVJubvhNjLHCncDvcYYagIX5mTdkZobbFBEwDly N2J25rMn9EJ6YQmLoHyNLtFf1+avUVyjC/KiwxfSH96DrBUY5eMsvdcOjYfvLLMzgNBf gHGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=UfacaA3R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ze11si1814533ejb.434.2021.03.24.06.34.15; Wed, 24 Mar 2021 06:34:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=UfacaA3R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235264AbhCXNaB (ORCPT + 99 others); Wed, 24 Mar 2021 09:30:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235252AbhCXN3w (ORCPT ); Wed, 24 Mar 2021 09:29:52 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A9F3C0613DE for ; Wed, 24 Mar 2021 06:29:52 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id u9so32863559ejj.7 for ; Wed, 24 Mar 2021 06:29:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=PgVEYZSY9NfCWwR2RJxuZCY5immorWw9IvrU7FEyMZY=; b=UfacaA3RcLXv73AcHDgJVuzH7YaisiE/ItZkS1uBCiSsqiLdWBFj5OF+zflnrnsnYx thUzVL+tmZYONqMfijfYvZh9KKy38ZVe3ey1RBu19Et4GjUIPC2BNLhXkOOj251o6RaW 8XtBzRFZKyeNdJxct3Rszmmy3GIlPfZ2VEtjw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=PgVEYZSY9NfCWwR2RJxuZCY5immorWw9IvrU7FEyMZY=; b=dmNgdG3u9mUCb+aXYKCl6RXemrpqO9ryFSUfKbDVczA7FE8oGp1nOzjKrg5yQ9bIsr +aqc9PfSGj4w88ruf9jed5wTjPSatlFtUO8MNJ3xLk0E+p86LZoFc4mG1kWnsAVDTl3R 7nT9v/vvUfShiHOuLKsg+rExW3MtkgX/ZW7DuJWln6Mq95+Hg+oDnN4hWWEyvdYGqzhM d4W/nU/xsAZcHEGWyRzdayWKq5k1vtKbDryotvusUmh78U26QTpfA72d4TXEwCD2RWE4 RD/KI97ynJOUtXMJ6JAlXsosMP6PiEqkUedN0fuX6hl+9Oisg1ccTEdE308cZ/3IHuX3 kUeg== X-Gm-Message-State: AOAM530p2re7+uj7WaY+poERffFx5APtvt4wlPymXv4zBk/7qwKuOHF0 IzbfnPouvdw+j72blYKBgIC/N23shwU0BlTh X-Received: by 2002:a17:906:3159:: with SMTP id e25mr3694270eje.303.1616592590531; Wed, 24 Mar 2021 06:29:50 -0700 (PDT) Received: from [192.168.1.149] ([80.208.71.248]) by smtp.gmail.com with ESMTPSA id u16sm1178887edq.4.2021.03.24.06.29.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 Mar 2021 06:29:50 -0700 (PDT) Subject: Re: [PATCH] [v2] hinic: avoid gcc -Wrestrict warning To: Arnd Bergmann , Bin Luo , "David S. Miller" , Jakub Kicinski Cc: Arnd Bergmann , Rasmus Villemoes , netdev@vger.kernel.org, linux-kernel@vger.kernel.org References: <20210324130731.1513798-1-arnd@kernel.org> From: Rasmus Villemoes Message-ID: Date: Wed, 24 Mar 2021 14:29:49 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: <20210324130731.1513798-1-arnd@kernel.org> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/03/2021 14.07, Arnd Bergmann wrote: > From: Arnd Bergmann > > 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. > This looks much better. Thanks. > Cc: Rasmus Villemoes > Signed-off-by: Arnd Bergmann > --- > v2: rework according to feedback from Rasmus. This one could > easily avoid most of the pitfalls > --- > .../net/ethernet/huawei/hinic/hinic_ethtool.c | 25 ++++++++----------- > 1 file changed, 10 insertions(+), 15 deletions(-) > > diff --git a/drivers/net/ethernet/huawei/hinic/hinic_ethtool.c b/drivers/net/ethernet/huawei/hinic/hinic_ethtool.c > index c340d9acba80..d7e20dab6e48 100644 > --- a/drivers/net/ethernet/huawei/hinic/hinic_ethtool.c > +++ b/drivers/net/ethernet/huawei/hinic/hinic_ethtool.c > @@ -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 > @@ -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; It's not your invention of course, but this both seems needlessly harsh and EFAULT is a weird error to return. It's just a printk() message that might be truncated, and now that the format string only has a %d specifier, it can actually be verified statically that overflow will never happen (though I don't know or think gcc can do that, perhaps there's some locale nonsense in the standard that allows using utf16-encoded sanskrit runes). So probably that test should just be dropped, but that's a separate thing. Reviewed-by: Rasmus Villemoes