All of lore.kernel.org
 help / color / mirror / Atom feed
From: kernel test robot <lkp@intel.com>
To: kbuild@lists.01.org
Subject: lib/hexdump.c:50 hex_to_bin() warn: shift has higher precedence than mask
Date: Wed, 27 Apr 2022 00:43:32 +0800	[thread overview]
Message-ID: <202204270058.nS3U9AEC-lkp@intel.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 4464 bytes --]

CC: kbuild-all(a)lists.01.org
BCC: lkp(a)intel.com
CC: linux-kernel(a)vger.kernel.org
TO: Mikulas Patocka <mpatocka@redhat.com>
CC: 0day robot <lkp@intel.com>

tree:   https://github.com/intel-lab-lkp/linux/commits/UPDATE-20220425-200806/Mikulas-Patocka/hex2bin-make-the-function-hex_to_bin-constant-time/20220425-045651
head:   1270242e9a05bb4547ff4c65b0ff2eef25ac9bf3
commit: 1270242e9a05bb4547ff4c65b0ff2eef25ac9bf3 hex2bin: make the function hex_to_bin constant-time
date:   28 hours ago
:::::: branch date: 28 hours ago
:::::: commit date: 28 hours ago
config: x86_64-randconfig-m001-20220425 (https://download.01.org/0day-ci/archive/20220427/202204270058.nS3U9AEC-lkp(a)intel.com/config)
compiler: gcc-11 (Debian 11.2.0-20) 11.2.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

New smatch warnings:
lib/hexdump.c:50 hex_to_bin() warn: shift has higher precedence than mask

Old smatch warnings:
lib/hexdump.c:51 hex_to_bin() warn: shift has higher precedence than mask

vim +50 lib/hexdump.c

3fc957721d18c9 Harvey Harrison 2008-05-14  18  
903788892ea0fc Andy Shevchenko 2010-05-24  19  /**
903788892ea0fc Andy Shevchenko 2010-05-24  20   * hex_to_bin - convert a hex digit to its real value
903788892ea0fc Andy Shevchenko 2010-05-24  21   * @ch: ascii character represents hex digit
903788892ea0fc Andy Shevchenko 2010-05-24  22   *
903788892ea0fc Andy Shevchenko 2010-05-24  23   * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
903788892ea0fc Andy Shevchenko 2010-05-24  24   * input.
1270242e9a05bb Mikulas Patocka 2022-04-25  25   *
1270242e9a05bb Mikulas Patocka 2022-04-25  26   * This function is used to load cryptographic keys, so it is coded in such a
1270242e9a05bb Mikulas Patocka 2022-04-25  27   * way that there are no conditions or memory accesses that depend on data.
1270242e9a05bb Mikulas Patocka 2022-04-25  28   *
1270242e9a05bb Mikulas Patocka 2022-04-25  29   * Explanation of the logic:
1270242e9a05bb Mikulas Patocka 2022-04-25  30   * (ch - '9' - 1) is negative if ch <= '9'
1270242e9a05bb Mikulas Patocka 2022-04-25  31   * ('0' - 1 - ch) is negative if ch >= '0'
1270242e9a05bb Mikulas Patocka 2022-04-25  32   * we "and" these two values, so the result is negative if ch is in the range
1270242e9a05bb Mikulas Patocka 2022-04-25  33   *	'0' ... '9'
1270242e9a05bb Mikulas Patocka 2022-04-25  34   * we are only interested in the sign, so we do a shift ">> 8"; note that right
1270242e9a05bb Mikulas Patocka 2022-04-25  35   *	shift of a negative value is implementation-defined, so we cast the
1270242e9a05bb Mikulas Patocka 2022-04-25  36   *	value to (unsigned) before the shift --- we have 0xffffff if ch is in
1270242e9a05bb Mikulas Patocka 2022-04-25  37   *	the range '0' ... '9', 0 otherwise
1270242e9a05bb Mikulas Patocka 2022-04-25  38   * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
1270242e9a05bb Mikulas Patocka 2022-04-25  39   *	in the range '0' ... '9', 0 otherwise
1270242e9a05bb Mikulas Patocka 2022-04-25  40   * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
1270242e9a05bb Mikulas Patocka 2022-04-25  41   *	... '9', -1 otherwise
1270242e9a05bb Mikulas Patocka 2022-04-25  42   * the next line is similar to the previous one, but we need to decode both
1270242e9a05bb Mikulas Patocka 2022-04-25  43   *	uppercase and lowercase letters, so we use (ch & 0xdf), which converts
1270242e9a05bb Mikulas Patocka 2022-04-25  44   *	lowercase to uppercase
903788892ea0fc Andy Shevchenko 2010-05-24  45   */
1270242e9a05bb Mikulas Patocka 2022-04-25  46  int hex_to_bin(unsigned char ch)
903788892ea0fc Andy Shevchenko 2010-05-24  47  {
1270242e9a05bb Mikulas Patocka 2022-04-25  48  	unsigned char cu = ch & 0xdf;
1270242e9a05bb Mikulas Patocka 2022-04-25  49  	return -1 +
1270242e9a05bb Mikulas Patocka 2022-04-25 @50  		((ch - '0' +  1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
1270242e9a05bb Mikulas Patocka 2022-04-25  51  		((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
903788892ea0fc Andy Shevchenko 2010-05-24  52  }
903788892ea0fc Andy Shevchenko 2010-05-24  53  EXPORT_SYMBOL(hex_to_bin);
903788892ea0fc Andy Shevchenko 2010-05-24  54  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

                 reply	other threads:[~2022-04-26 16:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=202204270058.nS3U9AEC-lkp@intel.com \
    --to=lkp@intel.com \
    --cc=kbuild@lists.01.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.