File 0001-hex-parser-Warn-about-large-gaps-and-detect-allocati.patch of Package stm32flash

From 80260ba9b5a1222dbb6cd1c3d27f322c807cffd5 Mon Sep 17 00:00:00 2001
From: Tormod Volden <debian.tormod@gmail.com>
Date: Mon, 28 Nov 2022 17:56:33 +0100
Subject: [PATCH 01/13] hex parser: Warn about large gaps and detect allocation
 failure

https://sourceforge.net/p/stm32flash/tickets/150/

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
---
 parsers/hex.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/parsers/hex.c b/parsers/hex.c
index 9a6330b..959dde1 100644
--- a/parsers/hex.c
+++ b/parsers/hex.c
@@ -30,6 +30,8 @@
 #include "../compiler.h"
 #include "../utils.h"
 
+extern FILE *diag;
+
 typedef struct {
 	size_t		data_len, offset;
 	uint8_t		*data;
@@ -181,10 +183,17 @@ parser_err_t hex_open(void *storage, const char *filename, const char write) {
 					}
 
 					/* if there is a gap, enlarge and fill with 0xff */
-					unsigned int len = base - st->base;
+					size_t len = base - st->base;
 					if (len > st->data_len) {
+						size_t gap = len - st->data_len;
+						if (gap > 16384) /* arbitrary limit for warning */
+							fprintf(diag, "Warning: Filling gap of %zu bytes at 0x%08zx\n", gap, st->base + st->data_len);
 						st->data = realloc(st->data, len);
-						memset(&st->data[st->data_len], 0xff, len - st->data_len);
+						if (st->data == NULL) {
+							fprintf(diag, "Error: Cannot reallocate memory\n");
+							return PARSER_ERR_SYSTEM;
+						}
+						memset(&st->data[st->data_len], 0xff, gap);
 						st->data_len = len;
 					}
 					break;
-- 
2.39.2

openSUSE Build Service is sponsored by