Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's up with `strlcpy` and `strlcat`? Are they getting standardized?


We'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.


There 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).


strdup seems like a perfect example of "standardizing existing practice." And it has never struck me as running against the spirit of C.


In 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.


No 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.


There 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...


The results (from the minutes 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.


There 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.


glibc still refuses to add the functions because they are not required by a standard.


> 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.


FWIW, Annex K is not currently deprecated.


It's not commonly available though, e.g. on Linux/BSD systems...


Correct -- 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.


> rseacord 22 minutes ago [-]

> The C Committee has taken two votes on this, and in each case, the committee has been equally divided. Without a consensus to change the standard, the status quo wins.

The fact that it has only survived on status quo is a pretty crass hint that things aren't well with Annex K.


And every BSD out there. And whatever it is that macOS does. Microsoft looks to be the outlier to me.


Microsoft 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


macOS is POSIX compliant.


It should be.


Well, 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.


I disagree, because they return a value that nobody really wants and thus perform poorly: https://saagarjha.com/blog/2020/04/12/designing-a-better-str...


So standardize them as returning void — great!


What you really want is it to tell you how much it copied.


I don't think I've ever wanted to know that, actually. Void is totally fine for my use.


I hope not, they perform much worse than they need to :(




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

Search: