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 --- cmds-device.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'cmds-device.c') diff --git a/cmds-device.c b/cmds-device.c index 7a0f7a40..198ad689 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -116,8 +116,7 @@ static int cmd_add_dev(int argc, char **argv) } close(devfd); - strncpy(ioctl_args.name, argv[i], BTRFS_PATH_NAME_MAX); - ioctl_args.name[BTRFS_PATH_NAME_MAX-1] = 0; + strncpy_null(ioctl_args.name, argv[i]); res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args); e = errno; if(res<0){ @@ -161,8 +160,7 @@ static int cmd_rm_dev(int argc, char **argv) struct btrfs_ioctl_vol_args arg; int res; - strncpy(arg.name, argv[i], BTRFS_PATH_NAME_MAX); - arg.name[BTRFS_PATH_NAME_MAX-1] = 0; + strncpy_null(arg.name, argv[i]); res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg); e = errno; if(res<0){ @@ -227,8 +225,7 @@ static int cmd_scan_dev(int argc, char **argv) printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]); - strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX); - args.name[BTRFS_PATH_NAME_MAX-1] = 0; + strncpy_null(args.name, argv[i]); /* * FIXME: which are the error code returned by this ioctl ? * it seems that is impossible to understand if there no is -- cgit v1.2.3