diff mbox series

[U-Boot,v2,02/13] sandbox: Correct maths in allocation routines

Message ID 20190408192053.236725-3-sjg@chromium.org
State Accepted
Commit 4a6409b74cc09b150f234ba3ab7d75ba6358270a
Delegated to: Simon Glass
Headers show
Series sandbox: Tidy up initcall and tracing | expand

Commit Message

Simon Glass April 8, 2019, 7:20 p.m. UTC
Allocation routines were adjusted to ensure that the returned addresses
are a multiple of the page size, but the header code was not updated to
take account of this. These routines assume that the header size is the
same as the page size which is unlikely.

At present os_realloc() does not work correctly due to this bug. The only
user is the hostfs 'ls' command, and only if the directory contains a
unusually long filename, which likely explains why this bug was not
caught earlier.

Fix this by doing the calculations using the obtained page size.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2:
- Add a new patch to Correct maths in allocation routines

 arch/sandbox/cpu/os.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

Comments

Simon Glass April 22, 2019, 2:38 a.m. UTC | #1
Allocation routines were adjusted to ensure that the returned addresses
are a multiple of the page size, but the header code was not updated to
take account of this. These routines assume that the header size is the
same as the page size which is unlikely.

At present os_realloc() does not work correctly due to this bug. The only
user is the hostfs 'ls' command, and only if the directory contains a
unusually long filename, which likely explains why this bug was not
caught earlier.

Fix this by doing the calculations using the obtained page size.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2:
- Add a new patch to Correct maths in allocation routines

 arch/sandbox/cpu/os.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index c20491493fc..47dfb476d37 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -209,8 +209,8 @@  void os_tty_raw(int fd, bool allow_sigs)
 
 void *os_malloc(size_t length)
 {
-	struct os_mem_hdr *hdr;
 	int page_size = getpagesize();
+	struct os_mem_hdr *hdr;
 
 	/*
 	 * Use an address that is hopefully available to us so that pointers
@@ -229,30 +229,34 @@  void *os_malloc(size_t length)
 
 void os_free(void *ptr)
 {
-	struct os_mem_hdr *hdr = ptr;
+	int page_size = getpagesize();
+	struct os_mem_hdr *hdr;
 
-	hdr--;
-	if (ptr)
-		munmap(hdr, hdr->length + sizeof(*hdr));
+	if (ptr) {
+		hdr = ptr - page_size;
+		munmap(hdr, hdr->length + page_size);
+	}
 }
 
 void *os_realloc(void *ptr, size_t length)
 {
-	struct os_mem_hdr *hdr = ptr;
+	int page_size = getpagesize();
+	struct os_mem_hdr *hdr;
 	void *buf = NULL;
 
-	hdr--;
-	if (length != 0) {
+	if (length) {
 		buf = os_malloc(length);
 		if (!buf)
 			return buf;
 		if (ptr) {
+			hdr = ptr - page_size;
 			if (length > hdr->length)
 				length = hdr->length;
 			memcpy(buf, ptr, length);
 		}
 	}
-	os_free(ptr);
+	if (ptr)
+		os_free(ptr);
 
 	return buf;
 }