Skip to contentSkip to conversation

fdtdec: test: Fix memory leak

PATCH New Thierry Reding 2 participants 7 messages May 20, 2019 – Jun 28, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fdtdec: test: Fix memory leak

From: Thierry Reding <[email protected]>

Free the memory allocated to store the test FDT upon test completion to avoid leaking the memory. We don't bother cleaning up on test failure since the code is broken in that case and should be fixed, in which case the leak would also go away.

---
 lib/fdtdec_test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/fdtdec_test.c +2 -0
index f6defe16c5a6..54efcc3d46ac 100644
@@ -138,6 +138,7 @@ static int run_test(const char *aliases, const char *nodes, const char *expect)
 	}
 
 	printf("pass\n");
+	free(blob);
 	return 0;
 }
 
@@ -288,6 +289,7 @@ static int check_carveout(void)
 	CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 2), 0);
 	CHECKOK(check_fdt_carveout(fdt, 2, 2));
 
+	free(fdt);
 	return 0;
 }
 
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/2 fdtdec: Remove fdt_{addr,size}_unpack()

From: Thierry Reding <[email protected]>

U-Boot already defines the {upper,lower}_32_bits() macros that have the same purpose. Use the existing macros instead of defining new APIs.

---
 include/fdtdec.h  | 24 ------------------------
 lib/fdtdec.c      |  8 ++++++--
 lib/fdtdec_test.c |  8 ++++++--
 3 files changed, 12 insertions(+), 28 deletions(-)
include/fdtdec.h +0 -24
index 110aa6ab6dea..fa8e34f6f960 100644
@@ -24,30 +24,6 @@
 typedef phys_addr_t fdt_addr_t;
 typedef phys_size_t fdt_size_t;
 
-static inline fdt32_t fdt_addr_unpack(fdt_addr_t addr, fdt32_t *upper)
-{
-	if (upper)
-#ifdef CONFIG_PHYS_64BIT
-		*upper = addr >> 32;
-#else
-		*upper = 0;
-#endif
-
-	return addr;
-}
-
-static inline fdt32_t fdt_size_unpack(fdt_size_t size, fdt32_t *upper)
-{
-	if (upper)
-#ifdef CONFIG_PHYS_64BIT
-		*upper = size >> 32;
-#else
-		*upper = 0;
-#endif
-
-	return size;
-}
-
 #ifdef CONFIG_PHYS_64BIT
 #define FDT_ADDR_T_NONE (-1U)
 #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
lib/fdtdec.c +6 -2
index fea44a9a8c65..d0ba88897335 100644
@@ -1300,6 +1300,7 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 	fdt32_t cells[4] = {}, *ptr = cells;
 	uint32_t upper, lower, phandle;
 	int parent, node, na, ns, err;
+	fdt_size_t size;
 	char name[64];
 
 	/* create an empty /reserved-memory node if one doesn't exist */
@@ -1340,7 +1341,8 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 	 * Unpack the start address and generate the name of the new node
 	 * base on the basename and the unit-address.
 	 */
-	lower = fdt_addr_unpack(carveout->start, &upper);
+	upper = upper_32_bits(carveout->start);
+	lower = lower_32_bits(carveout->start);
 
 	if (na > 1 && upper > 0)
 		snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
@@ -1374,7 +1376,9 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 	*ptr++ = cpu_to_fdt32(lower);
 
 	/* store one or two size cells */
-	lower = fdt_size_unpack(carveout->end - carveout->start + 1, &upper);
+	size = carveout->end - carveout->start + 1;
+	upper = upper_32_bits(size);
+	lower = lower_32_bits(size);
 
 	if (ns > 1)
 		*ptr++ = cpu_to_fdt32(upper);
lib/fdtdec_test.c +6 -2
index 54efcc3d46ac..e8bfd1fb1ec3 100644
@@ -156,11 +156,13 @@ static int make_fdt_carveout_device(void *fdt, uint32_t na, uint32_t ns)
 	};
 	fdt32_t cells[4], *ptr = cells;
 	uint32_t upper, lower;
+	fdt_size_t size;
 	char name[32];
 	int offset;
 
 	/* store one or two address cells */
-	lower = fdt_addr_unpack(carveout.start, &upper);
+	upper = upper_32_bits(carveout.start);
+	lower = lower_32_bits(carveout.start);
 
 	if (na > 1 && upper > 0)
 		snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
@@ -174,7 +176,9 @@ static int make_fdt_carveout_device(void *fdt, uint32_t na, uint32_t ns)
 	*ptr++ = cpu_to_fdt32(lower);
 
 	/* store one or two size cells */
-	lower = fdt_size_unpack(carveout.end - carveout.start + 1, &upper);
+	size = carveout.end - carveout.start + 1;
+	upper = upper_32_bits(size);
+	lower = lower_32_bits(size);
 
 	if (ns > 1)
 		*ptr++ = cpu_to_fdt32(upper);
Signature · 1 line
2.21.0
#2 lore raw

PATCH 2/2 fdtdec: Remove fdt_{addr,size}_unpack()

Quoted 6 lines · Thierry Reding

From: Thierry Reding <[email protected]>

U-Boot already defines the {upper,lower}_32_bits() macros that have the same purpose. Use the existing macros instead of defining new APIs.

Quoted 6 lines
---
 include/fdtdec.h  | 24 ------------------------
 lib/fdtdec.c      |  8 ++++++--
 lib/fdtdec_test.c |  8 ++++++--
 3 files changed, 12 insertions(+), 28 deletions(-)

Applied to u-boot-dm, thanks!

#3 lore raw

PATCH 1/2 fdtdec: test: Fix memory leak

In reply to Thierry Reding

Hi Thierry,

Quoted 20 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Free the memory allocated to store the test FDT upon test completion to avoid leaking the memory. We don't bother cleaning up on test failure since the code is broken in that case and should be fixed, in which case the leak would also go away.

---
 lib/fdtdec_test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/fdtdec_test.c +0 -0
index f6defe16c5a6..54efcc3d46ac 100644

lib/fdtdec_test.c static int run_test(const char *aliases, const char *nodes, const char *expect) · line 141

Quoted 4 lines
lib/fdtdec_test.c +1 -0
@@ -138,6 +138,7 @@ static int run_test(const char *aliases, const char *nodes, const char *expect)
        }
 
        printf("pass\n");
+       free(blob);

Strictly speaking, CHECKVAL() can cause a function return in the case of an error.

So a better solution might be to put the code after the malloc() into a separate function.

Quoted 12 lines
lib/fdtdec_test.c +1 -1
        return 0;
 }
 
@@ -288,6 +289,7 @@ static int check_carveout(void)
        CHECKVAL(make_fdt_carveout(fdt, FDT_SIZE, 2, 2), 0);
        CHECKOK(check_fdt_carveout(fdt, 2, 2));
 
+       free(fdt);
        return 0;
 }
 
--
Quoted 2 lines

2.21.0

Regards,
Simon

#4 lore raw

PATCH 1/2 fdtdec: test: Fix memory leak

Quoted 25 lines · Simon Glass

Hi Thierry,

Quoted 22 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Free the memory allocated to store the test FDT upon test completion to avoid leaking the memory. We don't bother cleaning up on test failure since the code is broken in that case and should be fixed, in which case the leak would also go away.

---
 lib/fdtdec_test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/fdtdec_test.c +0 -0
index f6defe16c5a6..54efcc3d46ac 100644
@@ -138,6 +138,7 @@ static int run_test(const char *aliases, const char *nodes, const char *expect)
        }
 
Quoted 8 lines

lib/fdtdec_test.c static int run_test(const char *aliases, const char *nodes, const char *expect) · line 141

Quoted 2 lines
lib/fdtdec_test.c +1 -0
        printf("pass\n");
+       free(blob);

Strictly speaking, CHECKVAL() can cause a function return in the case of an error.

So a better solution might be to put the code after the malloc() into a separate function.

When Heinrich suggested this fix he brought up the same issue, but concluded, and I agree with him, that it wasn't worth addressing the CHECKVAL case because if CHECKVAL failed, our code was buggy and would need fixing, at which point the leak would go away along with the bug.

Do you feel strongly about reworking this so it doesn't leak in the error case either?

Thierry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190521/5fa2473b/attachment.sig>

#5 lore raw

PATCH 1/2 fdtdec: test: Fix memory leak

Hi Thierry,

Quoted 36 lines · Thierry Reding
Quoted 25 lines · Simon Glass

Hi Thierry,

Quoted 22 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Free the memory allocated to store the test FDT upon test completion to avoid leaking the memory. We don't bother cleaning up on test failure since the code is broken in that case and should be fixed, in which case the leak would also go away.

---
 lib/fdtdec_test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/fdtdec_test.c +0 -0
index f6defe16c5a6..54efcc3d46ac 100644
@@ -138,6 +138,7 @@ static int run_test(const char *aliases, const char *nodes, const char *expect)
        }
 
Quoted 8 lines

lib/fdtdec_test.c static int run_test(const char *aliases, const char *nodes, const char *expect) · line 141

Quoted 2 lines
lib/fdtdec_test.c +1 -0
        printf("pass\n");
+       free(blob);

Strictly speaking, CHECKVAL() can cause a function return in the case of an error.

So a better solution might be to put the code after the malloc() into a separate function.

Quoted 7 lines

When Heinrich suggested this fix he brought up the same issue, but concluded, and I agree with him, that it wasn't worth addressing the CHECKVAL case because if CHECKVAL failed, our code was buggy and would need fixing, at which point the leak would go away along with the bug.

Do you feel strongly about reworking this so it doesn't leak in the error case either?

No. Oddly enough I haven't seen a Coverity report on this.

Regards,
Simon

#6 lore raw

PATCH 1/2 fdtdec: test: Fix memory leak

Quoted 45 lines · Simon Glass

Hi Thierry,

Quoted 41 lines · Thierry Reding
Quoted 25 lines · Simon Glass

Hi Thierry,

Quoted 22 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Free the memory allocated to store the test FDT upon test completion to avoid leaking the memory. We don't bother cleaning up on test failure since the code is broken in that case and should be fixed, in which case the leak would also go away.

---
 lib/fdtdec_test.c | 2 ++
 1 file changed, 2 insertions(+)
lib/fdtdec_test.c +0 -0
index f6defe16c5a6..54efcc3d46ac 100644
@@ -138,6 +138,7 @@ static int run_test(const char *aliases, const char *nodes, const char *expect)
        }
 
Quoted 8 lines

lib/fdtdec_test.c static int run_test(const char *aliases, const char *nodes, const char *expect) · line 141

Quoted 2 lines
lib/fdtdec_test.c +1 -0
        printf("pass\n");
+       free(blob);

Strictly speaking, CHECKVAL() can cause a function return in the case of an error.

So a better solution might be to put the code after the malloc() into a separate function.

When Heinrich suggested this fix he brought up the same issue, but concluded, and I agree with him, that it wasn't worth addressing the CHECKVAL case because if CHECKVAL failed, our code was buggy and would need fixing, at which point the leak would go away along with the bug.

Quoted 6 lines
Quoted 2 lines

Do you feel strongly about reworking this so it doesn't leak in the error case either?

No. Oddly enough I haven't seen a Coverity report on this.

Applied to u-boot-dm/next, thanks!