[PATCH v2 21/29] scripts: imx: replace static string allocation

Marco Felsch m.felsch at pengutronix.de
Tue Oct 17 07:51:23 PDT 2023


Make the CSF command string allocation more dynamic by introducing
strcata(lloc)(). To make it more straight forward.

Suggested-by: Sascha Hauer <s.hauer at pengutronix.de>
Signed-off-by: Marco Felsch <m.felsch at pengutronix.de>
---
Changelog:
v2:
- new patch suggested by Sascha
- v1-link: https://lore.barebox.org/barebox/20231010143314.2031253-1-m.felsch@pengutronix.de/T/#t

 include/mach/imx/imx-header.h |  1 -
 scripts/imx/imx.c             | 34 +++++++++++++++++++---------------
 2 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/include/mach/imx/imx-header.h b/include/mach/imx/imx-header.h
index 0ad3485eb261..372dee6cb8e9 100644
--- a/include/mach/imx/imx-header.h
+++ b/include/mach/imx/imx-header.h
@@ -115,7 +115,6 @@ struct config_data {
 	int (*write_mem)(const struct config_data *data, uint32_t addr,
 			 uint32_t val, int width, int set_bits, int clear_bits);
 	int (*nop)(const struct config_data *data);
-	int csf_space;
 	char *csf;
 	int sign_image;
 	char *signed_hdmi_firmware_file;
diff --git a/scripts/imx/imx.c b/scripts/imx/imx.c
index 933019eba449..50f44d5cb896 100644
--- a/scripts/imx/imx.c
+++ b/scripts/imx/imx.c
@@ -20,6 +20,23 @@
  */
 #define ENCRYPT_OFFSET	(HEADER_LEN + 0x10)
 
+static char *strcata(char *str, const char *add)
+{
+	size_t size = (str ? strlen(str) : 0) + strlen(add) + 1;
+	bool need_init = str ? false : true;
+
+	str = realloc(str, size);
+	if (!str)
+		return NULL;
+
+	if (need_init)
+		memset(str, 0, size);
+
+	strcat(str, add);
+
+	return str;
+}
+
 static int parse_line(char *line, char *argv[])
 {
 	int nargs = 0;
@@ -284,15 +301,10 @@ static int do_max_load_size(struct config_data *data, int argc, char *argv[])
 
 static int hab_add_str(struct config_data *data, const char *str)
 {
-	int len = strlen(str);
-
-	if (data->csf_space < len)
+	data->csf = strcata(data->csf, str);
+	if (!data->csf)
 		return -ENOMEM;
 
-	strcat(data->csf, str);
-
-	data->csf_space -= len;
-
 	return 0;
 }
 
@@ -300,14 +312,6 @@ static int do_hab(struct config_data *data, int argc, char *argv[])
 {
 	int i, ret;
 
-	if (!data->csf) {
-		data->csf_space = 0x10000;
-
-		data->csf = calloc(data->csf_space + 1, 1);
-		if (!data->csf)
-			return -ENOMEM;
-	}
-
 	for (i = 1; i < argc; i++) {
 		ret = hab_add_str(data, argv[i]);
 		if (ret)
-- 
2.39.2




More information about the barebox mailing list