Skip to contentSkip to conversation

common: elf: use strdup_const for symmetry

PATCH Applied Ahmad Fatoum 2 participants 3 messages Jan 5 – Jan 6

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 common: elf: use strdup_const for symmetry

The elf->filename is free'd by free_const, but it's always allocated with strdup. Change this to strdup_const to be able to avoid duplicating string literals.

---
 common/elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/elf.c +1 -1
index c68ea0be3fa6..e8126c9b64e7 100644
@@ -315,7 +315,7 @@ static struct elf_image *elf_check_init(const char *filename)
 	}
 	close(fd);
 
-	elf->filename = strdup(filename);
+	elf->filename = strdup_const(filename);
 	if (!elf->filename) {
 		ret = -ENOMEM;
 		goto err_free_hdr_buf;
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/2 common: elf: use memcmp for fixed-size comparison

strncmp would only make sense if it were taking the size of buf as argument, but it's taking the size of ELFMAG instead, which is a fixed 4 bytes. It's thus effectively equal to memcmp, so switch to using that instead.

---
 common/elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/elf.c +1 -1
index e8126c9b64e7..18c541bf827e 100644
@@ -201,7 +201,7 @@ static int load_elf_image_segments(struct elf_image *elf)
 
 static int elf_check_image(struct elf_image *elf, void *buf)
 {
-	if (strncmp(buf, ELFMAG, SELFMAG)) {
+	if (memcmp(buf, ELFMAG, SELFMAG)) {
 		pr_err("ELF magic not found.\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.47.3
Applied
#2 lore raw
In reply to Ahmad Fatoum
Quoted 5 lines · Ahmad Fatoum

The elf->filename is free'd by free_const, but it's always allocated with strdup. Change this to strdup_const to be able to avoid duplicating string literals.

Applied, thanks!

[1/2] common: elf: use strdup_const for symmetry

      https://git.pengutronix.de/cgit/barebox/commit/?id=2e47b5900193 (link may not be stable)

[2/2] common: elf: use memcmp for fixed-size comparison
https://git.pengutronix.de/cgit/barebox/commit/?id=3dc2cad1a7e7 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>