diff options
author | Eric Sandeen <sandeen@redhat.com> | 2013-01-25 13:27:47 -0600 |
---|---|---|
committer | Zach Brown <zab@redhat.com> | 2013-02-05 16:09:41 -0800 |
commit | bcb2b73358f1c05e6b5c48cfd19e3762cc69c677 (patch) | |
tree | 0527a68a1c20f3777f1c5f3559e29c424fb4dde4 /cmds-device.c | |
parent | 7ced17b07f9524c999395240458a46e403036d6b (diff) |
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 <sandeen@redhat.com>
Diffstat (limited to 'cmds-device.c')
-rw-r--r-- | cmds-device.c | 9 |
1 files changed, 3 insertions, 6 deletions
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 |