this post was submitted on 26 Aug 2024
1199 points (99.1% liked)

Programmer Humor

19176 readers
973 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] uis@lemm.ee 2 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

If n is smaller than the string length (as in: distance to first null byte) then you're bound to have garbage in your return destination

Wha? N is just maximum length of string to copy. Data after dst+n is unchanged.

In retrospect null-terminated strings were a mistake, but so were many other things, at some point you just have to accept that there's hysterical raisins everywhere.

All hail length-prefixed strings!

[–] barsoap@lemm.ee 2 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

Data after dst+n is unchanged.

Sure but that means the part before that is garbage because you have a null terminated string without terminator.

Or at least that's how I see it. If your intention isn't to start and end with a null-terminated string you should be using memcpy. Let us not talk about situations where CHAR_BIT != 8 that's not POSIX anyway.

Even better, just avoid doing string manipulation in C.

[–] uis@lemm.ee 1 points 3 weeks ago

Let us not talk about situations where CHAR_BIT != 8 that's not POSIX anyway.

Yeah, let's not talk about 20-bit one's complement ints.