5 ms·
What's up with `strlcpy` and `strlcat`? Are they getting standardized?
by teleonorax 7y ago
What's up with `strlcpy` and `strlcat`? Are they getting standardized?
- rseacord 7y agoNo one has proposed making these standard. I doubt they would gain much support as they are similar to the Annex K Bounds Checked Interface functions strcpy_s and strcat_s but not quite as good IMHO.
- rseacord 7y agoThere were a number of recent proposals to adopt various POSIX functions by Martin Sebor into C including: N2353 2019/03/17 Sebor, Add strdup and strndup to C2X N2352 2019/03/17 Sebor, Add stpcpy, and stpncpy to C2X N2351 2019/03/17 Sebor, Add strnlen to C2X He is lurking on this thread as well. These proposals can all be found in the document log at http://www.open-std.org/jtc1/sc22/wg14/www/wg14_document_log.htm http://www.open-std.org/jtc1/sc22/wg14/www/wg14_document_log...
- rseacord 7y agoThe results (from the minutes http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2377.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2377.pdf) 6.33 Sebor, Add strnlen to C2X [N 2351] Result: No consensus on putting N2351 into C2X. 6.34 Sebor, Add stpcpy, and stpncpy to C2X [N 2352] Result: No consensus to put N2352 into C2X. 6.35 Sebor, Add strdup and strndup to C2X [N 2353] Result: N2353 be put into C2X. The committee wants a proposal for the wide character versions of any POSIX functions voted in this meeting.
- rmind 7y agoThere have been some disagreements on strlcpy/strlcat (BSD vs glibc crowd), although by now the debate has died off and these functions are pretty widely used. Also, while here, it would be lovely to have strchrnul() included.
- loeg 7y agoglibc still refuses to add the functions because they are not required by a standard.
- teleonorax 7y ago> similar to the Annex K Bounds Checked Interface functions strcpy_s and strcat_s but not quite as good IMHO. Err... I thought Annex K is deprecated and dead? Whereas strl* seem very much alive, some compilers even give a "strcpy/strncpy is unsafe, use strlcpy instead" warning.
- AaronBallman 7y agoFWIW, Annex K is not currently deprecated.
- eqvinox 7y agoIt's not commonly available though, e.g. on Linux/BSD systems...
- AaronBallman 7y agoCorrect -- it would be nice if the glibc maintainers would reconsider their opinion of supporting the optional Annex K functionality. There is definitely user demand for the feature.
- beefhash 7y agoAnd every BSD out there. And whatever it is that macOS does. Microsoft looks to be the outlier to me.
- loeg 7y agoMicrosoft does not even implement Annex K. > Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. > As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm
- 7y ago
- loeg 7y agoWell, I am informally proposing making those standard :-). IMO they're a lot more ergonomic than the Annex K functions, and do the thing most programmers think the strncat/strncpy functions do (admittedly, not part of ISO C). Annex K should be forgotten as the mistake it is and we can move on with existing real-world interfaces instead of inventing features from whole-cloth. I thought that was generally the C standard operating practice.
- saagarjha 7y agoI disagree, because they return a value that nobody really wants and thus perform poorly: https://saagarjha.com/blog/2020/04/12/designing-a-better-strcpy/ https://saagarjha.com/blog/2020/04/12/designing-a-better-str...
- AaronBallman 7y agoWe've been considering proposals to add common POSIX APIs into C, but I don't believe we've seen a proposal for strlcpy or strlcat yet. I recall we agreed to add strdup to C given its wide availability and usage.
- DougGwyn 7y agoThere are deficiencies in almost all proposals. Two new functions which avoid the problems are supposed to be published in C202x: strcasecmp and strncasecmp, added in header strings.h (note: not string.h).
- sramsay 7y agostrdup seems like a perfect example of "standardizing existing practice." And it has never struck me as running against the spirit of C.
- DougGwyn 7y agoIn fact I proposed strdup on a few occasions, but it wasn't adopted. It seems that they didn't like for standard library functions to use malloc. POSIX.1 specifies strdup.
- saagarjha 7y agoI hope not, they perform much worse than they need to :(