-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utilities.c: add an iio_strlcpy, a "safer" version of strncpy
Add a private verison of strlcpy, from FreeBSD (Todd C. Miller). strlcpy() is guarantee to NUL-terminate the resulting dest, even if it is too small. Also note that strlcpy() and strlcat() only operate on true 'C' strings. This means that for strlcpy(), src must be NUL-terminated. This is considered by many to be safer, and less error prone that strncpy, which does not NUL-terminate on truncation. Signed-off-by: Robin Getz <[email protected]>
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,3 +214,46 @@ char *iio_strdup(const char *str) | |
return buf; | ||
#endif | ||
} | ||
|
||
/* strlcpy is designed to be safer, more consistent, and less error prone | ||
* replacements for strncpy, since it guarantees to NUL-terminate the result. | ||
* | ||
* This function | ||
* Copyright (c) 1998, 2015 Todd C. Miller <[email protected]> | ||
* https://github.com/freebsd/freebsd/blob/master/sys/libkern/strlcpy.c | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* Copy string src to buffer dst of size dsize. At most dsize-1 | ||
* chars will be copied. Always NUL terminates (unless dsize == 0). | ||
* Returns strlen(src); if retval >= dsize, truncation occurred. | ||
* | ||
* src is assumed to be null terminated, if it is not, this function will | ||
* dereference unknown memory beyond src. | ||
*/ | ||
size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize) | ||
{ | ||
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 */ | ||
} |