r/cprogramming • u/die-Banane • May 05 '26
How to do strings properly?
I just started learning C and I often read on the internet that it is bad practice to use null terminated strings, so my question is what should I do instead?
30
Upvotes
View all comments
Show parent comment
1
u/BatchModeBob May 05 '26
If an attacker can change your memory you're SOL no matter what you do. The attacks that make the news are when unexpected data breaks your code. Suppose you have char buffer [100] to process input from a URL. The attacker can make a much longer URL and send it to you. When your code does strcpy (buffer, URL), it writes past the end of buffer. Classic buffer overrun. In the simplest exploitable case, the overrun will overwrite code that gets executed later. So the hacker chooses his special URL characters to be the opcodes he wants your code to execute.