libuv/src/strscpy.c
Ben Noordhuis 8972e65bf5 unix: harden string copying, introduce strscpy()
Replace calls to strcpy() and strncpy() with the newly introduced
uv__strscpy() function that is meticulous about zero-terminating
the destination buffer.

PR-URL: https://github.com/libuv/libuv/pull/2065
Refs: https://www.kernel.org/doc/htmldocs/kernel-api/API-strscpy.html
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
2018-12-04 17:08:09 +01:00

18 lines
307 B
C

#include "strscpy.h"
#include <limits.h> /* SSIZE_MAX */
ssize_t uv__strscpy(char* d, const char* s, size_t n) {
size_t i;
for (i = 0; i < n; i++)
if ('\0' == (d[i] = s[i]))
return i > SSIZE_MAX ? UV_E2BIG : (ssize_t) i;
if (i == 0)
return 0;
d[--i] = '\0';
return UV_E2BIG;
}