News for hackers. Stuff that matters.
Re:Considering C99 for Curl by dizzant on Tuesday November 22, 2022 @10:23AM (#33706747)
Is the original intent of strncpy() germane to the GPs comment? Explicitly stating the max length to copy is an effective tool for avoiding buffer overruns, regardless of whether the designers imagined that important use case.
Re:Considering C99 for Curl by tialaramex on Tuesday November 22, 2022 @10:56AM (#33707112)
But the thing it does (fill out a fixed sized buffer without caring about NUL-termination) is not at all what you'd want from a safety feature.

If you look at this function assuming it's a safety feature, that's a huge surprise, and indeed if you were skimming you might miss what it does because (in the context of "it's a safety feature") this is an insane choice. "Why would you do that?". Well, because it's not a safety feature.

The perf cost isn't what you'd expect from a safety feature either. Suppose we have a 1MB buffer, and we strncpy "THIS" into it using n = 1024. That's just four bytes right? Nope. strncpy() will write "THIS" and then 1020 zero bytes.

Re:Considering C99 for Curl by torstenvl on Tuesday November 22, 2022 @11:24AM (#33707490)
The issue is that strncpy() isn't a str___() function, despite its name. It's a 0x00-padded memcpy.
Re:Considering C99 for Curl by masklinn on Tuesday November 22, 2022 @11:36AM (#33707636)
But it's not an str* function, it's an strn* function. And most (though not all, that would be too easy) work on fixed-size (hence the n) nul-padded strings.
Re:Considering C99 for Curl by torstenvl on Tuesday November 22, 2022 @12:03PM (#33707990)
No. It isn't. It has absolutely nothing to do with strings. There is no such thing as a fixed-sized string in C. "A string is a contiguous sequence of characters terminated by and including the first null character." § 7.1.1.

Calling your bespoke byte-sequence data structure a "string" is inaccurate and viewing strncpy() as a string function is erroneous and will lead to buffer overflows.

Re:Considering C99 for Curl by masklinn on Tuesday November 22, 2022 @12:14PM (#33708147)
Have you considered giving reading comprehension a try?

Fixed-size strings are strings, just not nul-terminated strings. So are length-prefixed (“pascal”) strings.

Strncpy does not work with nul-terminated strings as I noted. If you’re working with fixed-size nul-padded strings however it’s the correct function to use.

Re:Considering C99 for Curl by torstenvl on Tuesday November 22, 2022 @12:16PM (#33708187)
> Have you considered giving reading comprehension a try?

Don't do this.

Re:Considering C99 for Curl by masklinn on Tuesday November 22, 2022 @01:46PM (#33709396)
Then don’t demand it by wilfully misunderstanding comments in order to “well actually” them.
Re:Considering C99 for Curl by cesarb on Tuesday November 22, 2022 @12:15PM (#33708163)
The output side of strncpy() might not be a str___() function, but AFAICS the input side of strncpy() is clearly a str___() function, since it stops reading (but not writing) at the first NUL byte.
Re:Considering C99 for Curl by masklinn on Tuesday November 22, 2022 @12:21PM (#33708255)
It’s incidental more than purposeful, though it does have its use as a conversion function: with a nul-padded field, why would you keep copying once you’ve hit the first nul? You can just set from an immediate without the need to read from memory.
Re:Considering C99 for Curl by masklinn on Tuesday November 22, 2022 @11:35AM (#33707622)
Except strncpy is broken for C strings, because it doesn't guarantee nul termination. So if you forget to force a termination on every use, you get buffer overruns.

Not only that, but (because of its actual purpose) it also fills the buffer with nuls, which is a complete waste of resources.

So yes, the original intent of strncpy() germane to the GPs comment, because it makes strncpy actively dangerous and complete shit when working with C strings.