Skip to content

Commit

Permalink
utilities.c: add an iio_strlcpy, a "safer" version of strncpy
Browse files Browse the repository at this point in the history
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
rgetz committed Apr 22, 2020
1 parent 36204e2 commit 474c708
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ void iio_channel_init_finalize(struct iio_channel *chn);
unsigned int find_channel_modifier(const char *s, size_t *len_p);

char *iio_strdup(const char *str);
size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);

int iio_context_add_attr(struct iio_context *ctx,
const char *key, const char *value);
Expand Down
43 changes: 43 additions & 0 deletions utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}

0 comments on commit 474c708

Please sign in to comment.