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

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