Use "%*s" when printing VERSION

The "%s" conversion specifier expects a NUL-terminated string.
However, the VERSION variable does not contain a NUL-terminator,
so formatting it using "%s" may lead to printing whatever happens
to be in memory next to VERSION.

Using "%*s" allows to specify how many characters to print,
thus making sure we don't go off the array.
This commit is contained in:
suve 2020-12-01 19:59:14 +01:00
parent 2da4dc4591
commit 7bb2fef0e2

View file

@ -126,7 +126,7 @@ void handle_signal(int signum) {
void print_help(char *argv[]) { void print_help(char *argv[]) {
fprintf(stderr, fprintf(stderr,
"dumb-init v%s" "dumb-init v%*s"
"Usage: %s [option] command [[arg] ...]\n" "Usage: %s [option] command [[arg] ...]\n"
"\n" "\n"
"dumb-init is a simple process supervisor that forwards signals to children.\n" "dumb-init is a simple process supervisor that forwards signals to children.\n"
@ -144,7 +144,7 @@ void print_help(char *argv[]) {
" -V, --version Print the current version and exit.\n" " -V, --version Print the current version and exit.\n"
"\n" "\n"
"Full help is available online at https://github.com/Yelp/dumb-init\n", "Full help is available online at https://github.com/Yelp/dumb-init\n",
VERSION, VERSION_len, VERSION,
argv[0] argv[0]
); );
} }
@ -199,7 +199,7 @@ char **parse_command(int argc, char *argv[]) {
debug = 1; debug = 1;
break; break;
case 'V': case 'V':
fprintf(stderr, "dumb-init v%s", VERSION); fprintf(stderr, "dumb-init v%*s", VERSION_len, VERSION);
exit(0); exit(0);
case 'c': case 'c':
use_setsid = 0; use_setsid = 0;