News for hackers. Stuff that matters.
Re:Beware of strncpy and strncat by wyldfire on Wednesday June 06, 2018 @12:08PM (#17248280)
> strlcpy has a better API, though sadly it's not standard on Linux.

I think it's because drepper hates us [1].

[1] http://www.sourceware.org/ml/libc-alpha/2000-08/msg00061.htm...

Re:Beware of strncpy and strncat by huhtenberg on Wednesday June 06, 2018 @12:18PM (#17248372)
> Dammit, it is not safe. It hides bugs in programs. If a string is too long for an allocated memory block the copying must not simply silently stop.

This is spot on. There are very few cases when silently truncating a copy of a string is an OK thing to do.

Re:Beware of strncpy and strncat by wyldfire on Wednesday June 06, 2018 @12:21PM (#17248415)
I think it's not genuine to describe the behavior of strlcpy() as "silently truncating". strlcpy() is documented as "If the return value is >= dstsize, the output string has been truncated. It is the caller's responsibility to handle this."
Re:Beware of strncpy and strncat by thefifthsetpin on Wednesday June 06, 2018 @12:30PM (#17248523)
Silent doesn't imply undocumented. strlcpy's truncation is silent because it doesn't inform the caller about whether or not it truncated.
Re:Beware of strncpy and strncat by stephencanon on Wednesday June 06, 2018 @12:37PM (#17248595)
The caller is informed: "If the return value is >= dstsize, the output string has been truncated"
Re:Beware of strncpy and strncat by akkartik on Wednesday June 06, 2018 @12:51PM (#17248747)
How is returning a different value "silent"? It's informing the caller just fine.

This is C we're talking about. Always be checking return values. (And errno! And errno! And errno!)

Re:Beware of strncpy and strncat by bripeace on Wednesday June 06, 2018 @01:58PM (#17249417)
Careful! Errno can be set even when there is not an error.it's value is only relevant when the return value of the function indicates an error.

That said there are some functions where the possible return values can't indicate an error. In those cases errno should be set to zero before the function call and then a change during will alert you to an error.

Why it's like this I have no idea but it's pretty annoying.

Re:Beware of strncpy and strncat by nothrabannosir on Wednesday June 06, 2018 @02:23PM (#17249637)
> Why it's like this I have no idea but it's pretty annoying.

The bit about errno possibly being non-zero but not indicating error unless the function does? I guess because the called function can itself call functions / libs which set errno and properly handle those (or just not care). Without the "errno only has meaning if the function returns error" guard, every single function needs to always reset errno to zero before returning, which is annoying and will almost certainly not happen in the majority of cases.

When the function has no way to communicate error, there is no other option but to use errno. But it's good that it's the exception, I imagine.

Re:Beware of strncpy and strncat by Sohcahtoa82 on Wednesday June 06, 2018 @01:26PM (#17249105)
But....it does inform the caller via the return value...what are you expecting it to do?
Re:Beware of strncpy and strncat by raverbashing on Wednesday June 06, 2018 @12:24PM (#17248438)
I'll take truncating errors in place of non-null terminating errors, thanks.

Can it makes things worse? Yes. But C is broken.

Re:Beware of strncpy and strncat by loeg on Wednesday June 06, 2018 @04:07PM (#17250623)
Ok, but, we've already got that kind of foot-shooting enabled by strncpy(), and glibc can't remove it as it is part of ISO C. If glibc must contain a truncating string copy, I'd much rather have strlcpy than strncpy. Do you find that persuasive?
Re:Beware of strncpy and strncat by matheusmoreira on Wednesday June 06, 2018 @12:27PM (#17248495)
I think he's right. Treating strings as if they were normal arrays makes things easier and more uniform. The size of arrays should always be known.

I still keep the NUL terminator around for compatibility reasons. Linux system calls don't need the NUL, but other C code might depend on it.

Re:Beware of strncpy and strncat by gowld on Wednesday June 06, 2018 @12:15PM (#17248348)
> 08 Aug 2000

Any progress since?

Re:Beware of strncpy and strncat by wyldfire on Wednesday June 06, 2018 @12:22PM (#17248418)
I think he still hates us ;)
Re:Beware of strncpy and strncat by loeg on Wednesday June 06, 2018 @04:19PM (#17250739)
On the plus side, Drepper left Redhat in 2010 and has been out as the glibc DFL since 2012.[0]

Amusingly, a request for strlcpy was the very 2nd comment on that LWN article.

Unfortunately, glibc still hasn't added it — Roland rejected it again in 2015.[1] Also unfortunately, RH hired Drepper back in 2017. Because they hate their other employees, I guess. Fortunately, no one is eager to give him back the reins of glibc.

[0]: https://lwn.net/Articles/488847/

[1]: https://sourceware.org/ml/libc-alpha/2015-11/msg00086.html