public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH] cmd/mem.c: fix undefined behavior in mem cmp
@ 2024-09-27 16:37 Quentin Schulz
  2024-09-27 18:56 ` Rasmus Villemoes
  0 siblings, 1 reply; 5+ messages in thread
From: Quentin Schulz @ 2024-09-27 16:37 UTC (permalink / raw)
  To: Tom Rini, Simon Glass; +Cc: u-boot, Quentin Schulz

From: Quentin Schulz <quentin.schulz@cherry.de>

My linter complains that "When using void pointers in calculations, the
behaviour is undefined".

GCC does say that "In GNU C, addition and subtraction operations are
supported on pointers to void"[1] but this hints at this only being
supported in the GNU flavor of C. And I assume U-Boot may want to be
compiled with clang/llvm?

Let's fix that warning by casting the void pointer to a u8 pointer since
the size variable unit is byte.

[1] https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html

Fixes: 0628ab8ec598 ("sandbox: Change memory commands to use map_physmem")
Signed-off-by: Quentin Schulz <quentin.schulz@cherry.de>
---
 cmd/mem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/cmd/mem.c b/cmd/mem.c
index 274348068c2..08ec0aefa7d 100644
--- a/cmd/mem.c
+++ b/cmd/mem.c
@@ -293,8 +293,8 @@ static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
 			break;
 		}
 
-		buf1 += size;
-		buf2 += size;
+		buf1 = ((u8 *)buf1) + size;
+		buf2 = ((u8 *)buf2) + size;
 
 		/* reset watchdog from time to time */
 		if ((ngood % (64 << 10)) == 0)

---
base-commit: 56b47b8b6a09c777e74fe6c52512c832691169aa
change-id: 20240927-cmd-mem-undefined-a4368d58b5b6

Best regards,
-- 
Quentin Schulz <quentin.schulz@cherry.de>


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-10-14 13:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-27 16:37 [PATCH] cmd/mem.c: fix undefined behavior in mem cmp Quentin Schulz
2024-09-27 18:56 ` Rasmus Villemoes
2024-09-30  8:38   ` Quentin Schulz
2024-09-30  9:54     ` Rasmus Villemoes
2024-10-14 13:13       ` Quentin Schulz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox