Re: mm/sparse.c:145:2: warning: int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jul 13, 2021 at 03:19:06AM +0800, kernel test robot wrote:
> cppcheck warnings: (new ones prefixed by >>)
> >> mm/sparse.c:145:2: warning: int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]
>     return (nid << SECTION_NID_SHIFT);

Hrm.

include/linux/mmzone.h:#define SECTION_NID_SHIFT                3

I'm going to suggest that we don't allow for 2^29 node IDs in a single
system, so this doesn't actually represent a bug that needs to be fixed.
On the other hand, this type of bug continually bites us, and it would
be good to warn about this kind of thing.  So in the spirit of silencing
the warning by doing the promotion that C should have specified in
the first place ...

--- 8< ---

[PATCH] Avoid a warning in sparse memory support

cppcheck warns that we're possibly losing information by shifting an int.
It's a false positive, because we don't allow for a NUMA node ID that
large, but if we ever change SECTION_NID_SHIFT, it could become a problem,
and in any case this is usually a legitimate warning.  Fix it by adding
the necessary cast, which makes the compiler generate the right code.

diff --git a/mm/sparse.c b/mm/sparse.c
index 6326cdf36c4f..f17bd4f7caaa 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -143,7 +143,7 @@ unsigned long __section_nr(struct mem_section *ms)
  */
 static inline unsigned long sparse_encode_early_nid(int nid)
 {
-	return (nid << SECTION_NID_SHIFT);
+	return ((unsigned long)nid << SECTION_NID_SHIFT);
 }
 
 static inline int sparse_early_nid(struct mem_section *section)




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux