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
No related branches found
No related tags found
1 merge request!455Update branch with changes from master
Pipeline #6243 failed
...@@ -427,9 +427,9 @@ typedef struct { ...@@ -427,9 +427,9 @@ typedef struct {
#define TERMINATE(str) str[sizeof(str)-1]=0 #define TERMINATE(str) str[sizeof(str)-1]=0
/* This is a bound-safe version of strcpy basically - only works with fixed-length arrays */ /* 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 */ /* 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 { \ #define SAFECAT(dst, src) do { \
if(strlen((char*)(dst)) + strlen((char*)(src)) < sizeof(dst)) { \ if(strlen((char*)(dst)) + strlen((char*)(src)) < sizeof(dst)) { \
......
...@@ -69,17 +69,26 @@ int safe_snprintf(char *dst, size_t size, const char *fmt, ...) ...@@ -69,17 +69,26 @@ int safe_snprintf(char *dst, size_t size, const char *fmt, ...)
#ifdef NEEDS_STRLCPY #ifdef NEEDS_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t size) size_t strlcpy(char *dst, const char *src, size_t size)
{ {
size_t i; const char *osrc = src;
size_t nleft = dsize;
if(size < 1)
return 0; /* Copy as many bytes as will fit. */
if (nleft != 0) {
for(i = 0; src[i] != '\0'; i++) { while (--nleft != 0) {
if(i < (size - 1)) if ((*dst++ = *src++) == '\0')
*(dst++) = src[i]; break;
} }
*dst = 0; }
return i;
/* 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 size_t
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment