Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp9776rwe; Wed, 31 Aug 2022 15:15:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR5e0Ioif20Dk99lvf5DmLb7FfXpqwAovS2Bwy1JDzJEApahZ0PAmkd9MjGwBQwXJHsfi3BF X-Received: by 2002:a17:907:2ceb:b0:741:6115:a57 with SMTP id hz11-20020a1709072ceb00b0074161150a57mr14062376ejc.423.1661984113971; Wed, 31 Aug 2022 15:15:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661984113; cv=none; d=google.com; s=arc-20160816; b=wwUtz4XjkeaosOC12AoDI3iOJlDY/RlEAgaxrcMeehMYowxbyHboHSDLhrJQAXfkPw TeYDr+bx2eVJYzGVNp1Yz3iFgWmAtC3nZFyRsiHdjrBNNvfAfcdnc2AZJimW8Hg9vnoz 3DSZP5KIFTdhAbxrqAJ4vmj13+TzGTX3QbG820fYOhTopAcFgecxKWOdDqNVVJjj8Lp+ i6l7zFAYYGhLbkRNTLlQvBeeEBgUWSKVzjndg8JlzqlYfBrVe1B9/n3/7k9BZF7voxUG cQ2QA3jW9tNon0YLyxhpTczJQjAbQ7fzQwpOIpF9ndk3bJRzJ1GaKsqTsC2Y6xjjpgbj AtNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=HhElJvUhitJ5nxGObCO2CCVplpp0m7vWmHMrypvXYVo=; b=r3m+guLS0WiUUAbah1BhqhfvQFsrrzzUcuZyA4e4hdXzBnBzotVqckDkYT1aNo99Fx fA0G2Z4gz5q/d1volllLq7QfveqZOVKiSvvkD6k8Wv0EoD5nE5GqVsezXSj3xqne+6IU XBnVwzbXeqAcnwo1MdloSbkHPJqeXDyYU0t57r/PhO6QoBDsxdU5D3xP/sYrfEX0Wmee LGLM5D461wUNnasi7mbPBwVvtlbzFUtS6L/MfzEyBOmM+BxGAT1ma1jwXkpYl+3DOUfP j/AxMBAeZokecC4hcblTHicTAEkJo3SrWv0+4TNtumgw9e3jAe42o28WZ3kkq8dB9FXk 3e7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=eUGEVMDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e20-20020a056402089400b004480a02d5f5si309016edy.252.2022.08.31.15.14.49; Wed, 31 Aug 2022 15:15:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=eUGEVMDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232136AbiHaVFW (ORCPT + 99 others); Wed, 31 Aug 2022 17:05:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231329AbiHaVFA (ORCPT ); Wed, 31 Aug 2022 17:05:00 -0400 Received: from mail.sberdevices.ru (mail.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99356F72D4 for ; Wed, 31 Aug 2022 14:04:43 -0700 (PDT) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mail.sberdevices.ru (Postfix) with ESMTP id 89B055FD05; Thu, 1 Sep 2022 00:04:40 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1661979880; bh=HhElJvUhitJ5nxGObCO2CCVplpp0m7vWmHMrypvXYVo=; h=Date:From:To:Subject:Message-ID:MIME-Version:Content-Type; b=eUGEVMDm3cm1G+rwNMmJS7QVYT3EMMAZsc8lX5UsXEO+dnIyD9RZ/xAUZVzarjCnI AJ4G3Mj3sb8wi5g0ZusonbDdM7amDOynMwlsGbYi+5AV+MKc44fce+YtoyMHEFt69T Rv3FOTzrz8jLhJlfnWvG1sTbGVCbHOsIa1MFL1oJe4kOwA5zxMsCo0KF9NqrrCN3yM zbgoz+zUCde2wfw+I6i7FnpzRR//C4A7lC5Uv938XGr5PdbbM7+HTLt9UOReQPN0SB 7a42sPlsijDbQEhyTTI2FiVMwPUUmmgpz12BRalHqKhbQWI6rkI565siArD0ffeksB nd0+Mt98VqstQ== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mail.sberdevices.ru (Postfix) with ESMTP; Thu, 1 Sep 2022 00:04:35 +0300 (MSK) Date: Thu, 1 Sep 2022 00:04:35 +0300 From: Dmitry Rokosov To: Jonathan Cameron CC: Andy Shevchenko , kernel test robot , , , , Linux Kernel Mailing List Subject: Re: [jic23-iio:testing 124/129] drivers/iio/accel/msa311.c:993:24: warning: format specifies type 'unsigned char' but the argument has type 'unsigned int' Message-ID: <20220831210435.uon3bder7ooeulfd@Rockosov-MBP> References: <202208290618.wU7mHfOp-lkp@intel.com> <20220830110329.00000d18@huawei.com> <20220831002405.m3j5sug2rz7bdz5s@Rockosov-MBP> <20220831093810.00006112@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20220831093810.00006112@huawei.com> User-Agent: NeoMutt/20220429 X-Originating-IP: [172.16.1.6] X-ClientProxiedBy: S-MS-EXCH01.sberdevices.ru (172.16.1.4) To S-MS-EXCH01.sberdevices.ru (172.16.1.4) X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/08/31 14:19:00 #20193471 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 31, 2022 at 09:38:10AM +0100, Jonathan Cameron wrote: > On Wed, 31 Aug 2022 03:24:05 +0300 > Dmitry Rokosov wrote: > > > Hello Jonathan and Andy, > > > > Sorry for such a late response, a couple of days ago my daughter was born. > > So I couldn't reach my laptop :) > > Congratulations and good luck! :) Thank you! :) > > > > > >> drivers/iio/accel/msa311.c:993:24: warning: format specifies type 'unsigned char' but the argument has type 'unsigned int' [-Wformat] > > > > > "msa311-%hhx", partid); > > > > > ~~~~ ^~~~~~ > > > > > %x > > > > > 1 warning generated. > > > > > > > > 992 msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL, > > > > > > 993 "msa311-%hhx", partid); > > > > > > > I'm thinking intent here was to limit range of what was printed. Maybe better to use > > > > local u8 variable or cast? > > > > > > > > I can fix it up if that's fine with you - or even better send me a patch that fixes > > > > it however you prefer! > > > > > > Looking back at what Linus said about those specifiers, I would rather > > > go with simple %x or %02x. > > > > > > P.S. Surprisingly many C developers don't know the difference between > > > %hhx and %02x, which is easy to check by > > > > > > char a = -1; > > > printf("%hhx <==> %02x\n", a, a); > > > a = 217; > > > printf("%hhx <==> %02x\n", a, a); > > > > Thank you for pointing to Linus answer. I have explored it at the link: > > > > https://lore.kernel.org/lkml/CAHk-=wgoxnmsj8GEVFJSvTwdnWm8wVJthefNk2n6+4TC=20e0Q@mail.gmail.com/ > > > > Actually, Linus described one exception to this rule, which I have > > in my patch. I have an integer which I want to print as a char. > > I see that Linus mentions it's a bad idea. I agree with that. But > > currently %hhx => %02x replacement breaks the requested behavior, %02x > > will not shrink integer value to char. I want to say, maybe it's better > > just cast the value to u8 type and print as %x. What do you think? I can > > prepare such a patch. > > > > P.S. Andy's example to show the difference between %hhx and %02x makes > > more clear why such a replacement is not acceptable here. > > > > Output: > > ff <==> ffffffff > > d9 <==> ffffffd9 > > > In this case the storage is an unsigned int, not an unsigned char. > Hence the value will be small and positive. So I'm fairly sure you > won't hit the above because it's > > 0x000000ff --> ff > 0x000000d9 --> d9 > > The range is limited to 8 bits because that's all the underlying register > holds. From "data" format point of view you are right. We have regmap over I2C and register values will be limited to 8 bits only. But in general unsigned int value bigger than 0xff formatted by %02x will not be limited by two positions only. In other words, we can use a simple %x with the same success. I want to say if our goal is shrinking the unsigned int value to first byte in hex format w/o %hhx using, we need to cast unsigned int value to unsigned char and printout it using simple %x or %02x. For example, in my opinion, in the below code snippet, only first and third printout formatting are correct. Currently, we are using the second in the merged patchset. >>> unsigned int a = 0xDEADBEEF; printf("%hhx <==> %02x (uint8_t:%02x)\n", a, a, (unsigned char)a); <<< Output: ef <==> deadbeef (uint8_t:ef) === Anyway, regmap over I2C abstraction limits our value to the 8-bit range, so functionally %02x is working well here. -- Thank you, Dmitry