mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 08:33:30 +00:00
Fix null terminator demonstration to show undefined behavior
The previous example wasn't reliably demonstrating the danger of missing null terminators because the stack happened to contain a '\0' byte immediately after the array, making the broken code appear to work. Added memset() to fill the stack with non-zero values before declaring the unterminated array. This ensures the broken example actually exhibits undefined behavior by reading past the array boundary, making the lesson about null terminators clear and visible. Changes: - Added garbage array filled with 'Z' characters using memset() - This forces printf() to read past the broken array until finding '\0' - Readers now see actual garbage output demonstrating why null terminators are required in C strings
This commit is contained in:
parent
87e7742451
commit
be78fa799d
1 changed files with 5 additions and 0 deletions
|
|
@ -3730,8 +3730,13 @@ To illustrate the subtlety of this class of bug, try the following small program
|
|||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
// Fill stack with non-zero garbage
|
||||
char garbage[100];
|
||||
memset(garbage, 'Z', sizeof(garbage));
|
||||
|
||||
// Deliberately forget the null terminator
|
||||
char broken[5] = {'B', 'S', 'D', '!', 'X'};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue