3 ms·
I love how, from reading the article, you can't tell if the count parameter of the proposed replacement includes the null terminator (is the length of the dest
by hedora 4y ago
I love how, from reading the article, you can't tell if the count parameter of the proposed replacement includes the null terminator (is the length of the dest buffer) or not (is the length - 1).
Also, it is unclear what side effects the function has if it returns ERR2BIG. Is dest null terminated or not in that case?
I could figure these things out, but my point is that all these str*cpy functions are fundamentally error prone because other people can't keep those details straight either, apparently.
I've never had an issue using 1+strlen() to figure out how many bytes to copy, checking the destination buffer size and then invoking memcpy. It's a bit inefficient (two passes, though both are usually using vectorized instructions) but at least it is really clear to the reader.
The main problem is that C lets you typo this:
strlen(s+1)
When you mean this:
strlen(s)+1
Also, of course, for untrusted buffers, you can't use strlen().
- saagarjha 4y agostrscpy always null terminates, unless the destination buffer has size zero. -E2BIG is returned when the entire string could not be copied. The return value is essentially strlen of the resulting string. It’s good all around.
- hedora 4y agoEven with that, I still don't have enough information to safely invoke it. (What is count?) These APIs are too complicated/subtle. Honestly, at this point, I just use (ptr, len) pairs that point to non-null terminated strings whenever possible.
- masklinn 4y ago> Even with that, I still don't have enough information to safely invoke it. (What is count?) Count is necessarily the target buffer’s size since it’s what you don’t want overrun.
- deleted 4y ago[deleted]
- saagarjha 4y agoI don’t usually call people out for moving the goalposts, but when it comes to string copying routines I get real cranky about it. Stop moving the goalposts. You want a string copying routine. Most suck. This is the good one. Or at least, it’s one in the family of good ones. You have a C-style string, you want it copied elsewhere, you want it to be mindful of the bounds you give it, you want it to not do something you didn’t ask it for, this does that. It’s not trivial to write one, I wrote a whole thing about how it’s not and how this is good. People did it right. Now, shut up. No, seriously, shut up. After all that work you don’t get to go “uh, this is actually unsafe because what if the original string is not null terminated and the buffer is not actually the size you said it is”. That’s not how strings in C work. Strings in C work in exactly one way and you don’t get to change that, because that’s just how they work, and there’s 50 years of code that uses string copying routines that work with this that would very much like to have the holy grail of string copies and doesn’t need you muddying the waters and saying that this cannot be invoked safely. It can. The invariants are challenging to uphold but this implementation provides every accommodation that the C standard can provide to this API. Every single time string copying comes up on Hacker News some enlightened commenter is like “yeah why even bother with this I’m just going to use pointer+length pairs and solve everything”. No. You don’t get to drag this discussion about a very real problem into your half-baked “solution” for C. You especially do not get to reply to me explaining exactly why this API improves upon the state of the art with a lazy dismissal where you retreat to something completely different and go “yeah this is way safer lol”. This would be like if I just responded “yeah I’d just use Rust here I don’t get what the deal is” to you. It doesn’t help. Look, I’d love to have a conversation about fat pointers and alternative strings in C. I’d love to talk about how we can migrate all this code to safer languages. But this isn’t the time or place for that, and I’m sick of this discussion repeating every single time we talk about, for the love of god, projects that are using normal C strings and safer ways to work with them. It’s not just you but you responding to my comment with something I perceive as fundamentally uninspired set me off.
- PieUser 4y ago
- 4y ago
- Gibbon1 4y agoI'm with you, fix the language to allow for proper strings. And tell anyone that doesn't like it to use std=c89.
- rabf 4y agoIf you want to use this function read the brief man page that covers all these points in very little text. Skip the article! https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html https://manpages.debian.org/testing/linux-manual-4.8/strscpy...
- 1over137 4y ago>The main problem is that C lets you typo this: >strlen(s+1) >When you mean this: >strlen(s)+1 There are tools to find that, like: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/misplaced-operator-in-strlen-in-alloc.html https://clang.llvm.org/extra/clang-tidy/checks/bugprone/misp...