diff mbox series

genload: fix memory corruption in hogvm

Message ID 20240610085622.87076-1-sjiwei@163.com
State Superseded
Headers show
Series genload: fix memory corruption in hogvm | expand

Commit Message

Jiwei Sun June 10, 2024, 8:56 a.m. UTC
From: Jiwei Sun <sunjw10@lenovo.com>

With the following command for doing memory stress test,

  ./genload -v --vm 10 --vm-chunks 4 --vm-bytes 1073741824

Some memory corruption issue was triggered,

  malloc(): corrupted top size

The root cause of the issue is that allocated memory for ptr is less
than what is actually needed.

Signed-off-by: Jiwei Sun <sunjw10@lenovo.com>
---
 tools/genload/genload.c | 2 +-
 tools/genload/stress.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/tools/genload/genload.c b/tools/genload/genload.c
index 7f56d5272..9712e7828 100644
--- a/tools/genload/genload.c
+++ b/tools/genload/genload.c
@@ -642,7 +642,7 @@  int hogvm(long long forks, long long chunks, long long bytes)
 			usleep(backoff);
 
 			while (1) {
-				ptr = (char **)malloc(chunks * 2);
+				ptr = (char **)malloc(chunks * 2 * sizeof(char *));
 				for (j = 0; chunks == 0 || j < chunks; j++) {
 					if ((ptr[j] =
 					     (char *)malloc(bytes *
diff --git a/tools/genload/stress.c b/tools/genload/stress.c
index 7f56d5272..9712e7828 100644
--- a/tools/genload/stress.c
+++ b/tools/genload/stress.c
@@ -642,7 +642,7 @@  int hogvm(long long forks, long long chunks, long long bytes)
 			usleep(backoff);
 
 			while (1) {
-				ptr = (char **)malloc(chunks * 2);
+				ptr = (char **)malloc(chunks * 2 * sizeof(char *));
 				for (j = 0; chunks == 0 || j < chunks; j++) {
 					if ((ptr[j] =
 					     (char *)malloc(bytes *