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

> It's to provide a drop-in replacement to previous, memory-unsafe string copy routines

Nitpick: it’s not quite a drop-in. Prototypes of these functions are

  char * strncpy(char *dst, const char *src, size_t num);
  size_t strlcpy(char *dst, const char *src, size_t num);
strncpy(dst, src, num) always returns dst (https://cplusplus.com/reference/cstring/strncpy/), which is quite useless, as the caller knew that already.

strlcpy(dst, src, num) returns the total length of the string it tried to create (https://www.unix.com/man-page/posix/3/strlcpy/). Callers can use that to detect that the string didn’t fit the buffer and reallocate a buffer that’s long enough.



Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: