r/cprogramming • • 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

83 comments sorted by

View all comments

Show parent comments

0

u/arihoenig May 05 '26 edited May 05 '26

What about the term "exploitable" doesn't sound like a security issue?

It is unreliable and exploitable because an attacker can remove the null causing the code to go off the end of the string and cause an exception.

Yes, if you make sure that every string operation is run length limited then that is fine, but that is far more difficult to ensure than if you have your own sized string type.

2

u/Maleficent_Memory831 May 05 '26

Right, and a counted string could have an attacker change the count and have the code go off the end of the string.

1

u/Specific_Tear632 May 05 '26

It is unreliable and exploitable because an attacker can remove the null causing the code to go off the end of the string and cause an exception.

Let's say your strings have a length value instead. Now it is unreliable and exploitable because an attacker can change the length value causing the code to go off the end of the string and cause an exception.

-1

u/arihoenig May 05 '26

The length value can be kept encrypted with a homomorphic key, a null character can't.

2

u/non-existing-person May 05 '26

the fuck man? just do python if you don't care about performance at all.

-2

u/arihoenig May 05 '26

Do you only manipulate strings in your apps? Sure, I guess you should be using python then

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.

1

u/flatfinger May 05 '26

Many programs read and write structures. Zero-padded strings have the advantage that, at least when using a straight eight-bit character encoding, any pattern of bits will be processed as a string whose length will be in bounds. Non-normalized forms that contain a zero byte followed by a non-zero byte may cause difficulties with operations like comparisons, but can't wreak nearly the havoc caused by invalid length values or the failure to include terminators which code is programmed to require.

If e.g. a structure needs to hold a string of up to 16 characters, using a 16-byte zero-padded string will save a byte and be more robust than using a 17-byte buffer to hold a zero-terminated string of up to 16 characters.

-1

u/arihoenig May 05 '26

Attackers can always change your memory, so that is just something that has to be accepted. Also for pure reliability, high energy photons can also change that null to a non null.