Blame SOURCES/0063-hexdump.h-fix-arithmetic-error.patch

834e5a
From 9313a515432ba938e66f2edc1e22d548fed1eb5c Mon Sep 17 00:00:00 2001
834e5a
From: Peter Jones <pjones@redhat.com>
834e5a
Date: Thu, 30 Jul 2020 14:34:22 -0400
834e5a
Subject: [PATCH] hexdump.h: fix arithmetic error.
834e5a
834e5a
When I modified the hexdumper to help debug MokListRT mirroring not
834e5a
working because of PcdMaxVolatileVariableSize being tiny, I
834e5a
inadvertently added something that is effectively:
834e5a
834e5a
hexdump(..., char *buf, ..., int position)
834e5a
{
834e5a
	unsigned long begin = (position % 16);
834e5a
	unsigned long i;
834e5a
	...
834e5a
	for (i = 0; i < begin; i++) {
834e5a
		...
834e5a
	}
834e5a
	...
834e5a
}
834e5a
834e5a
Unfortunately, in c if 0x8 is set in position, that means begin is
834e5a
0xfffffffffffff8, because signed integer math is horrifying:
834e5a
834e5a
include/hexdump.h:99:vhexdumpf() &data[offset]:0x9E77E6BC size-offset:0x14
834e5a
include/hexdump.h:15:prepare_hex() position:0x9E77E6BC
834e5a
include/hexdump.h:17:prepare_hex() before:0xFFFFFFFFFFFFFFFC size:0x14
834e5a
include/hexdump.h:19:prepare_hex() before:0xFFFFFFFFFFFFFFFC after:0x0
834e5a
include/hexdump.h:21:prepare_hex() buf:0x000000009E77E2BC offset:0 &buf[offset]:0x000000009E77E2BC
834e5a
834e5a
Woops.
834e5a
834e5a
This could further have been prevented in /some/ cases by simply not
834e5a
preparing the hexdump buffer when "verbose" is disabled.
834e5a
834e5a
This patch makes "pos" be unsigned in all cases, and also checks for
834e5a
verbose in vhexdumpf() and simply returns if it is 0.
834e5a
834e5a
Signed-off-by: Peter Jones <pjones@redhat.com>
834e5a
---
834e5a
 include/hexdump.h | 7 +++++--
834e5a
 1 file changed, 5 insertions(+), 2 deletions(-)
834e5a
834e5a
diff --git a/include/hexdump.h b/include/hexdump.h
834e5a
index f3f3ac284a3..b2968cd4f85 100644
834e5a
--- a/include/hexdump.h
834e5a
+++ b/include/hexdump.h
834e5a
@@ -4,7 +4,7 @@
834e5a
 #include <stdint.h>
834e5a
 
834e5a
 static inline unsigned long UNUSED
834e5a
-prepare_hex(const void *data, size_t size, char *buf, int position)
834e5a
+prepare_hex(const void *data, size_t size, char *buf, unsigned int position)
834e5a
 {
834e5a
 	char hexchars[] = "0123456789abcdef";
834e5a
 	int offset = 0;
834e5a
@@ -48,7 +48,7 @@ prepare_hex(const void *data, size_t size, char *buf, int position)
834e5a
 #define isprint(c) ((c) >= 0x20 && (c) <= 0x7e)
834e5a
 
834e5a
 static inline void UNUSED
834e5a
-prepare_text(const void *data, size_t size, char *buf, int position)
834e5a
+prepare_text(const void *data, size_t size, char *buf, unsigned int position)
834e5a
 {
834e5a
 	int offset = 0;
834e5a
 	unsigned long i;
834e5a
@@ -84,6 +84,9 @@ vhexdumpf(const char *file, int line, const char *func, const CHAR16 * const fmt
834e5a
 	unsigned long display_offset = at;
834e5a
 	unsigned long offset = 0;
834e5a
 
834e5a
+	if (verbose == 0)
834e5a
+		return;
834e5a
+
834e5a
 	while (offset < size) {
834e5a
 		char hexbuf[49];
834e5a
 		char txtbuf[19];
834e5a
-- 
834e5a
2.26.2
834e5a