3 ms·
Yes this is something to get use to. The BSDs created strlcpy(3) and wcslcpy(3) https://man.openbsd.org/strlcpy.3 https://man.openbsd.org/strlcpy.3 https://m
by jmclnx 4y ago
Yes this is something to get use to. The BSDs created strlcpy(3) and wcslcpy(3)
https://man.openbsd.org/strlcpy.3 https://man.openbsd.org/strlcpy.3
https://man.openbsd.org/wcslcpy.3 https://man.openbsd.org/wcslcpy.3
which to me will help with some of these issues. Too bad other Operating Systems do not have these. On Linux there is libbsd to get these, but I would like to see these to be added to the stdc.
Instead the c23 standard is messing with realloc(3) which could break some old programs. I have not looked at that in detail yet, so maybe it is a non-issue :)
- deleted 4y ago[deleted]
- torstenvl 4y agoI don't know of a compiler that forces you to use the newest version of the standard, which is why I've always kind of thought "don't break old code" was treated too much like dogma. So from that perspective, a non issue. However, there is a problem that has nothing to do with old code: they increased the number of situations that constitute undefined behavior, with no public discussion and no justification. It's frankly dangerous behavior.
- alecco 4y agoUshering out strlcpy() https://lwn.net/Articles/905777/ https://lwn.net/Articles/905777/
- GabrielTFS 4y agoThese functions are in the current POSIX draft - though not published, it's quite unlikely to be removed (someone actually specifically filed an issue against POSIX to try and get it removed, basically on the basis of "it's not perfect so it should be removed", and the issue got rejected on the basis that there's no consensus for removal, and it seems unlikely this will change), and as a result, the functions are getting added to glibc: https://sourceware.org/pipermail/libc-alpha/2023-April/146967.html https://sourceware.org/pipermail/libc-alpha/2023-April/14696...
- anthomtb 4y agostrlcpy is nice due to the guaranteed NUL termination. strlcpy is not so nice due to the strange (IMO) return value of the number of characters in the source string. Which could be the number of characters copied or much, much larger than the number of characters copied. snprintf does the same thing. So using strlcpy is safe (by C's low bar) but using the return value may be highly unsafe.
- jandrese 4y agoThe thing that annoys me the most about strlcpy is that it is supposed to be safer, but what happens in the case where the source string is not properly NULL terminated? You might think that it will stop at the character limit you specified, but that's not what it does. It just blows on past the end of the buffer looking for a \0 until it either finds one or causes a segmentation violation. IMHO I would like it much more if the return values were: 0: string copied 1: string partially copied but truncated -1: Error, errno set. This can occur when src or dst are NULL.
- hgs3 4y agoLinux's strscpy addresses these issues.
- jandrese 4y agoWhich is great if you're a kernel developer, a bit of a moot point for application developers.
- saagarjha 4y ago> what happens in the case where the source string is not properly NULL terminated That’s not a string.
- anthomtb 4y agoNone of strlcpy, strncpy, and strcpy will know that you have not provided a string. They will assume the source pointer is a string and as such, will read (and write, in the case of strcpy) bytes until they find that NUL. This is the upside of strlcpy. Whatever is in your output buffer is guaranteed to be a NUL terminated and have your desired length. strncpy does not make that guarantee. strcpy will give you something with a NUL terminator but it could be well past the end of the output buffer. Hello, CVE. The more I write here, the more I realize how silly it would be to write anything dealing with human-readable text in C in 2023. I had been working on a C webserver a while back but I think I'm going to purge that from my local git server and start over with something else.
- hgs3 4y agoYup, there is also Linux's strscpy which doesn't require reading memory from the source string beyond the specified "count" bytes and the return value is idiot proof.