mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Organize companion code and materials by book part: part-01/ Foundations part-02/ Kernel & driver fundamentals part-03/ Character & block devices part-04/ Networking & storage part-05/ Buses & advanced topics part-06/ Debugging & performance part-07/ Real-world drivers appendices/ Supplementary material README.md Overview and build instructions
31 lines
782 B
C
31 lines
782 B
C
/*
|
|
* lab2_leak.c - DELIBERATELY LEAKY.
|
|
*
|
|
* Companion for "Hands-On Lab 2: Memory Leak and the Forgotten Free"
|
|
* in Chapter 4. This program works correctly, but it never frees the
|
|
* buffer it allocates. In userspace the OS reclaims the memory on
|
|
* exit so the leak is invisible. In the kernel, the same bug would
|
|
* accumulate until the system was rebooted.
|
|
*
|
|
* Run with AddressSanitizer to see the leak report:
|
|
* cc -fsanitize=address -g -o lab2_leak lab2_leak.c
|
|
* ./lab2_leak
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char *buffer = malloc(128);
|
|
if (buffer == NULL)
|
|
return (1);
|
|
|
|
strcpy(buffer, "FreeBSD device drivers are awesome!");
|
|
printf("%s\n", buffer);
|
|
|
|
/* Intentionally no free(buffer) here. */
|
|
return (0);
|
|
}
|