Skip to contentSkip to conversation

vfs: samples: fix memory leak

PATCH New [email protected] 1 participant 1 message Aug 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfs: samples: fix memory leak

From: Tom Rix <[email protected]>

clang static analysis reports this represenative problem

test-fsinfo.c:615:3: warning: Potential leak of memory pointed to by 'r'

                memset(r, 0xbd, buf_size);
                ^~~~~~

r dynamically allocated in a loop an increasing size.

	for (;;) {
		r = malloc(buf_size);
...
		buf_size = (ret + 4096 - 1) & ~(4096 - 1);
	}

The problem is that r is never freed at the bottom of the loop. So free r.

---
 samples/vfs/test-fsinfo.c  | 1 +
 samples/vfs/test-mntinfo.c | 1 +
 2 files changed, 2 insertions(+)
samples/vfs/test-fsinfo.c +1 -0
index aa122fb555b6..d669201d03ac 100644
@@ -622,6 +622,7 @@ static ssize_t get_fsinfo(const char *file, const char *name,
 		if (ret <= buf_size - 1)
 			break;
 		buf_size = (ret + 4096 - 1) & ~(4096 - 1);
+		free(r);
 	}
 
 	if (debug)
samples/vfs/test-mntinfo.c +1 -0
index 40125ad81f17..54a6abf4a8b0 100644
@@ -105,6 +105,7 @@ static void *get_attr_alloc(unsigned int mnt_id, unsigned int attr,
 			break;
 		}
 		buf_size = (ret + 4096 - 1) & ~(4096 - 1);
+		free(r);
 	}
 
 	return r;
Signature · 1 line
2.18.1