From bcb2b73358f1c05e6b5c48cfd19e3762cc69c677 Mon Sep 17 00:00:00 2001 From: Eric Sandeen Date: Fri, 25 Jan 2013 13:27:47 -0600 Subject: btrfs-progs: simplify ioctl name copy and null termination In the places where we copy a string into the name member of btrfs_ioctl_vol_args or btrfs_ioctl_vol_args_v2, we use strncopy (to not overflow the name array) and then set the last position to the null character. Howver, in both cases the arrays are defined with: char name[MAX+1]; hence the last array position is name[MAX]. In most cases, we now insert the null at name[MAX-1] which deprives us of one useful character. Even the above isn't consistent through the code, so make some helper code to make it simple, i.e. strncpy_null(dest, src) which automatically does the right thing based on the size of dest. Thanks to Zach Brown for the macro suggestion. Signed-off-by: Eric Sandeen --- utils.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 7a1e39d..f9ee812 100644 --- a/utils.c +++ b/utils.c @@ -1125,6 +1125,26 @@ char *pretty_sizes(u64 size) return pretty; } +/* + * __strncpy__null - strncpy with null termination + * @dest: the target array + * @src: the source string + * @n: maximum bytes to copy (size of *dest) + * + * Like strncpy, but ensures destination is null-terminated. + * + * Copies the string pointed to by src, including the terminating null + * byte ('\0'), to the buffer pointed to by dest, up to a maximum + * of n bytes. Then ensure that dest is null-terminated. + */ +char *__strncpy__null(char *dest, const char *src, size_t n) +{ + strncpy(dest, src, n); + if (n > 0) + dest[n - 1] = '\0'; + return dest; +} + /* * Checks to make sure that the label matches our requirements. * Returns: -- cgit v1.2.1