btrfs-progs: use proper size for argv0 substitution
authorDavid Sterba <dsterba@suse.cz>
Tue, 18 Nov 2014 17:05:20 +0000 (18:05 +0100)
committerDavid Sterba <dsterba@suse.cz>
Tue, 18 Nov 2014 17:05:20 +0000 (18:05 +0100)
Make run from a long base path will overflow the argv0 buffer during
tests. Otherwise, this would happen for all the standalone binaries that
use set_argv0.

Original report:
https://bbs.archlinux.org/viewtopic.php?id=189861

Reported-by: WorMzy Tykashi <wormzy.tykashi@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
utils.c
utils.h

diff --git a/utils.c b/utils.c
index 4b3bace..2a92416 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -66,7 +66,8 @@ void fixup_argv0(char **argv, const char *token)
 
 void set_argv0(char **argv)
 {
-       sprintf(argv0_buf, "%s", argv[0]);
+       strncpy(argv0_buf, argv[0], sizeof(argv0_buf));
+       argv0_buf[sizeof(argv0_buf) - 1] = 0;
 }
 
 int check_argc_exact(int nargs, int expected)
diff --git a/utils.h b/utils.h
index 8c94624..289e86b 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -38,7 +38,7 @@
 
 #define BTRFS_UUID_UNPARSED_SIZE       37
 
-#define ARGV0_BUF_SIZE 64
+#define ARGV0_BUF_SIZE PATH_MAX
 
 int check_argc_exact(int nargs, int expected);
 int check_argc_min(int nargs, int expected);