6 ms·
Strlcpy and Strlcat – Consistent, Safe, String Copy and Concatenation (1999) [pdf]
- Denvercoder9 4y agoOne drawback of these functions is that the return value is the length of the source string. This is seldomly useful, as it's mostly a micro-optimization to avoid an extra string scan when using static buffers for small strings, but dynamic buffers for large ones. However, it prevents these functions from being used with input that might not be null-terminated, and causes performance to be bounded by the size of the source (which usually isn't controlled by the caller), instead of the destination (which usually is).
- ben_bai 4y ago> it prevents these functions from being used with input that might not be null-terminated Those are not valid C-strings! String functions are(have) "undefined behavior" for non string inputs!
- Denvercoder9 4y agoThat's true, but in practice you sometimes encounter such strings and it's nice if you can use the regular string functions with them, instead of having to special-code something.
- pixelbeat__ 4y agoThere have been various attempts to improve on strcpy() etc. The current thinking is that strscpy() is the best general interface to this functionality. See the "improving ... strcpy" section at https://www.pixelbeat.org/programming/gcc/string_buffers.html https://www.pixelbeat.org/programming/gcc/string_buffers.htm...
- TonyTrapp 4y agoThe sad part is really that there are several implementation of safe(r) string manipulation functions, they all do mostly the same thing, but have different names and parameter orders, preventing interoperability between major compilers. They really need to get their shit together and finally agree on a single variant to be standardized.
- jabl 4y agoAs mentioned in the article linked above https://saagarjha.com/blog/2020/04/12/designing-a-better-strcpy/ https://saagarjha.com/blog/2020/04/12/designing-a-better-str... , memccpy() comes relatively close and is slated to be part of C23.
- quietbritishjim 4y agoGoodness me, that is a terrible name. Lower case o and c are very hard to distinguish in the middle of a word, so that looks like memcopy (yes I know the actual existing function is memcpy but memmove is more common in application-ish code, and it includes the middle "o", so I still read memccpy wrong). What next? merncopy?
- jabl 4y agoAdmittedly the name is poor, but memccpy has been in POSIX since forever (the opengroup.org manual mentions it's from "Issue 1" which I believe refers to the X/Open Issue 1 from 1985), so I guess the C committee thought that an existing poor name was better than coming up with a new one.
- pjmlp 4y agoNothing that requires separate pointer and length will be safe, no matter how many times they keep going at it, unless it is backed by hardware memory tagging.
- jabl 4y agoThat is undoubtedly true, but while we're waiting for the widespread acceptance of a C string library that defines a separate string type, or for the world to be rewritten in Rust or other safer languages, it's still possible to make incremental progress.
- q3k 4y agoA person smarter than me once told me: “Just use snprintf()”. The tiny performance hit is usually worth the safety and simplicity. Only one error condition to check, covering both concatenation and copies, sensible 'size' argument behaviour (size includes null terminating byte, null terminating byte is always written)... Just use snprintf().
- unwind 4y agoI came here to post exactly that; I think using snprintf() is the proper solution to the "I need to build a string out of many parts in C" problem pretty much every time. Especially since these functions' return values make it hard to use them to step forward in the buffer, so subsequent calls need to walk over the already-inserted characters again and again. That kind of redundant work makes me grit my teeth, which I guess is a sign that I'm a C programmer at heart. :) Compare: char buf[1024]; snprintf(buf, sizeof buf, "%s%s", "foo", "bar"); and char buf[1024]; strcpy(buf, "foo"); strlcat(buf, "bar", sizeof buf); The latter is given a pointer to the base buffer, so it has to walk past the existing characters in order to find the end where the concatenation should start. I also find the "size" arguments confusing and weird. I think it's idiomatic in C to (tightly) associate a size with a buffer pointer, and speaking in terms of "there are n bytes available at this location" makes sense to me. So the prototype really should have been size_t strlcat_unwind(char *buf, size_t buf_size, const char *src);
- mtlmtlmtlmtl 4y agoEven better, if you don't mind using the heap, there's also asprintf() which figures out the right length and allocates a buffer itself, then returns it to you. Downside is you have to free the pointer returned of course. And you might want to be careful passing it user input without taking a look at the length first. But you have to do that anyway if you use the stack.
- tom_ 4y agoHere's something I've found a useful upgrade to asprintf, as it frees the passed-in buffer after expanding the format string. You can just pass the same char ** repeatedly (and also pass it as one of the string arguments corresponding to a %s!) and it'll update the pointer appropriately each time. Makes many kinds of string manipulation very simple. int xasprintf(char**p,const char *fmt,..) { int n=0; char *p2=nullptr; if(fmt) { va_list v; va_start(v); n=asprintf(&p2,fmt,v); va_end(v); } if(n>=0) { free(*p); *p=p2; } return n; } If you thought asprintf was inefficient, this is worse. But it's very convenient!
- TheNewAndy 4y agoI've never understood the circumstances where it is ok for a copy/concatenate operation to also include a bonus "truncate the result" operation at the end. I'm a believer that you should use strcpy and strcat, and also make sure your buffers are large enough. If you can't make them large enough, then you can't do the operation you are trying to do (and I would say that doing it incorrectly via truncation isn't obviously better since this seems like silent corruption to me)
- dwattttt 4y agoSomeone will likely be able to answer more specifically, but I seem to recall an old common operation was filling fixed width text fields; truncating a message would make sense there (not ideal, but probably familiar).
- gdprrrr 4y agoExcept to properly implement that you'll need to deal with grapheme clusters.
- tialaramex 4y agoI expect the parent is thinking of why strncpy is the way it is, and the people who built that were glad if their terminal had both uppercase and lowercase letters, they didn't have Unicode, or grapheme clusters, their concern wasn't "will this string fit in this many pixels on my 24" flat panel?" but "Will this string fit in these 10 bytes where the filename needs to go in the directory data structure of the filesystem?".
- tedunangst 4y agoYou will be surprised to learn that there were programmers who did not make the buffers large enough.
- ben_bai 4y agoNo matter how big you make it. Someone will find a way to overflow it.
- wongarsu 4y agoNull-terminated strings have to be up there with null-pointers as design ideas that seemed great at the time, but turned out to be extremely costly because of the insane number of bugs they produce. I wonder what an alternative world would look like where C had decided to use pascal-strings instead (prefixing the string with its length). We would undoubtedly have argued about the length of the size prefix, but a mistake there is trivial to spot compared to wrong application of str[nls]*cpy(_s)?.
- pjmlp 4y agoSpecially given the alternatives outside Bell Labs in systems programming, since at least 1958 starting with JOVIAL.
- tyingq 4y agoThat also would have made for a nice performance benefit passing to zero-copy type calls. And reduced complexity for new languages written in C implementing their own string types. Tcl was irritating when it was first invented, as it directly used C strings, and couldn't process binary data at all.
- theamk 4y agoI used to program in Pascal a lot, and its counted strings were one of the worst features. First, you cannot really "argue" about length of the size prefix - it is a part of ABI/calling convention, and unless you have template-like mechanism and overloading/conventions, you will have to use whatever stdlib was compiled with. And Borland Pascal's stdlib was compiled with 1 byte prefix, making max string length of 255 bytes. This made sense when total RAM was measured in hundreds of kilobytes, but generated so many bugs when a normally short string had to be just a bit longer. I was somewhat envious of my C-using friends who could put entire help message into a single string. But not envious enough to switch to much slower C tooltchain :) There were other reasons, too, like using less registers in assembly-level loops (old machines had much fewer registers than modern ones). Or not having to worry about validating strings when reading binary data from disk.
- pjmlp 4y ago
- knorker 4y agoAs other have said, this is no longer considered to be "true". Yes, they make it harder to get memory corruption. But that's a very narrow definition of "safe". If you tell the computer to copy a string, or append to a string, it's wrong, and not safe, to only do a partial copy or concatenation. It's a huge footgun. Smaller than strcpy(), sure, but "safe" it is not. I don't really have a better answer for C.
- pjmlp 4y agoThe better answer would be to add data types like SDS[0] to the standard library, and use them as ADTs (Abstract Data Types) [1]. Unfortunely WG14 has proven in 30 years of existence, that it isn't something that they care to fix, and while 3rd party solutions exist, without vocabulary types on the stardard library adoption will never take off. [0] - https://github.com/antirez/sds https://github.com/antirez/sds [1] - https://en.wikipedia.org/wiki/Abstract_data_type https://en.wikipedia.org/wiki/Abstract_data_type
- knorker 4y agoI don't know that this should be fixed. It seems so fundamental to the core language that it's simply the cost of doing business. The set of projects in 2023 that should be done in C are not that big. Even the Linux kernel is getting Rust. C is still good for embedded stuff, but there tends to be much less string manipulation there than in most other code.
- pjmlp 4y agoUnfortunately C is here to stay for generations, everywhere there is a POSIX kernel. Embedded has enough memory buffer manipulations to be a concern as possible CVE root causes, specially in the IoT age where the S stands for security.
- knorker 4y agoI don't even see it as unfortunate. It's just reality. It's a tool to be used when appropriate, like any other. My point is that adding a higher level abstraction for e.g. strings in C runs a strong risk of not being fit for purpose for the people who reach for C. People code in C because they have very specific requirements. If they could just pick up an opinionated library then it's likely that they don't need to use C in the first place.
- godshatter 4y agoThe standard C functions are enough of a mess mentally for me that I try to use them only for simple things. If I'm dealing with lots of string manipulations I usually create a struct with a count and an alloc size and a char* pointer and write init, cleanup, extend, and append functions specific for it and use that as a string type. It's less of a mental load for me to deal with reallocating an array and keeping count and alloc updated correctly once than it is knowing that character arrays are being recounted all the time and worrying about exactly where the '\0' is going to end up and if my buffer is large enough for every string I'm dealing with.
- GuB-42 4y agoBefore using any function of the strcpy family, try to think about why you are using them. The first question is: do I need a copy? Obviously, often, you do, but maybe you can avoid it. Copying data can be costly in terms of performance, and of course, without copying, no need to allocate memory or deal with buffers sizes. Now, that you are sure that you indeed need a copy, maybe you should consider using memcpy() instead. If you already know the sizes of your strings, then it is a no-brainer. If you don't, maybe doing strlen()/strnlen() first on your input data is a good idea, and if it is too big, handle the error appropriately, and if it is not, then do a memcpy() now that you have the size. The default behavior of "safe" variants of strcpy() it to truncate the string to the maximum size. It is certainly better than a buffer overflow, but it may not be what you want, just ask your ass isstant.