Hacker Newsnew | past | comments | ask | show | jobs | submit | more ahoka's commentslogin

"strncpy() is a weird function with a crappy API."

Well if you bother looking up that it's originally created for non null-terminated strings, then it kinda makes sense.

The real problem begun when static analyzers started to recommend using it instead of strcpy (the real alternative used to be snprintf, now strlcpy).


strlcpy is a BSD-ism that isn't in posix. The official recommendation is stpecpy. Unfortunately, it is only implemented in the documentation, but not available anywhere unless you roll your own:

https://man7.org/linux/man-pages/man7/string_copying.7.html



Ah, good point. I forgot it had just gotten added. Past context https://news.ycombinator.com/item?id=36765747


Who cares? Just vendor it into your project. It's a tiny string manipulation function.

(I agree with the author of the piece that strlcpy doesn't actually solve the real problem.)


Your comment makes no sense. If it was designed for non-null terminated strings, why would it specifically pad after a null terminator?

I looked up the actual reason for its inception:

---

    Rationale for the ANSI C Programming Language", Silicon Press 1990.

    4.11.2.4 The strncpy function
    strncpy was initially introduced into the C library to deal with fixed-length name fields in structures such as directory entries. Such fields are not used in the same way as strings: the trailing null is unnecessary for a maximum-length field, and setting trailing bytes for shorter names to null assures efficient field-wise comparisons. strncpy is not by origin a "bounded strcpy," and the Committee has preferred to recognize existing practice rather than alter the function to better suit it to such use.


> If it was designed for non-null terminated strings, why would it specifically pad after a null terminator?

Padded and terminated strings are completely different beasts. And the text you quote tells you black on white that strncpy deals in padded strings.


“fixed-length name fields in structures such as directory entries”

“the trailing null is unnecessary for a maximum-length field”

That is a non–null terminated string.


What a stupid opinion.


It is sarcasm

edit: wait i get it now


Pretty sure the one you're replying to is as well. :)


CSMA is last used with 10Mbit ethernet, so that’s why radios are only relevant.


I did, but so what? But make IS the generic model and no one should invent any kind of build system without understanding make first.


Probably useful in a non-latin codeset?


Soft boiled eggs are put in an egg cup and cracked to eat the soft yolk. I believe it’s not a thing in the US.


What surprises me is that someone would consider cracking the big end of the egg. And have it sitting upside down in the egg cup?

Ludicrous! Absolutely ludicrous! :)


They are not sadists, contrary to what others say in the comments.


Although for the benefit of masochists, they are going to offer 6 day certs as an option soon.


Yep, the "shortlived" (6-day) profile will be available to the general public later this week. But at this time we explicitly encourage only mature organizations with stable infrastructure and an oncall rotation to adopt that profile, as the risks associated with a renewal failing at the beginning of a holiday long weekend are just too high for many sites.


Yeah, just press the button? I thought all machines have that.


For ESP32? You just install the VS Code extension and it does all the toolchain setup and you can flash with a button.


I don’t think that was available when I was starting and now I am set in my ways lol


I thought you was kidding first…


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: