memory leak example

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-09-18 00:00:02 +00:00
parent 5378cb295b
commit fffc8d92da
2 changed files with 35 additions and 2 deletions

23
userland/c/memory_leak.c Normal file
View File

@@ -0,0 +1,23 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#memory-leaks */
#include <stdlib.h>
void * my_malloc(size_t n) {
return malloc(n);
}
void leaky(size_t n, int do_leak) {
void *p = my_malloc(n);
if (!do_leak) {
free(p);
}
}
int main(void) {
leaky(0x10, 0);
leaky(0x10, 1);
leaky(0x100, 0);
leaky(0x100, 1);
leaky(0x1000, 0);
leaky(0x1000, 1);
}