Skip to content
Snippets Groups Projects
Commit 13ca484a authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Use strlcpy() for SAFECOPY() and XSAFECOPY() macros.

While we're here, Update strlcpy() to slightly more optimal
FreeBSD-13.3 version.
parent 2e39f3d2
Branches
Tags
1 merge request!455Update branch with changes from master
Pipeline #6243 failed
......@@ -427,9 +427,9 @@ typedef struct {
#define TERMINATE(str) str[sizeof(str)-1]=0
/* This is a bound-safe version of strcpy basically - only works with fixed-length arrays */
#define SAFECOPY(dst,src) (strncpy(dst,src,sizeof(dst)), TERMINATE(dst))
#define SAFECOPY(dst,src) strlcpy(dst,src,sizeof(dst))
/* Extra-safe SAFECOPY doesn't pass NULL-pointer to strncpy */
#define XSAFECOPY(dst,src) (strncpy(dst,(src)==NULL?"(null)":(src),sizeof(dst)), TERMINATE(dst))
#define XSAFECOPY(dst,src) strlcpy(dst,(src)==NULL?"(null)":(src),sizeof(dst))
#define SAFECAT(dst, src) do { \
if(strlen((char*)(dst)) + strlen((char*)(src)) < sizeof(dst)) { \
......
......@@ -69,17 +69,26 @@ int safe_snprintf(char *dst, size_t size, const char *fmt, ...)
#ifdef NEEDS_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
if(size < 1)
return 0;
for(i = 0; src[i] != '\0'; i++) {
if(i < (size - 1))
*(dst++) = src[i];
}
*dst = 0;
return i;
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(src - osrc - 1); /* count does not include NUL */
}
size_t
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment