From 9f26b250bbc1d97f1093a0997bfd5ccc504a56b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 1 Jan 2022 21:11:31 +0100 Subject: [PATCH] foot{,client}: handle argc == 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Our getopt_long() based argument parser would (correctly) break out of the while loop immediately. However, we would then proceed and decrement ‘argc’ by 1, resulting in it being -1. This was then passed to the terminal constructor, which passed it on to the client application fork+exec logic. That finally resulted in an exec(3) failure: err: slave.c:339: SHELL=/bin/zsh: failed to execute: No such file or directory err: fdm.c:215: no such FD: 7 info: main.c:604: goodbye --- client.c | 8 +++++--- main.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client.c b/client.c index b6749835..df43e7d5 100644 --- a/client.c +++ b/client.c @@ -124,7 +124,7 @@ main(int argc, char *const *argv) static const int foot_exit_failure = -36; int ret = foot_exit_failure; - const char *const prog_name = argv[0]; + const char *const prog_name = argc > 0 ? argv[0] : ""; static const struct option longopts[] = { {"term", required_argument, NULL, 't'}, @@ -309,8 +309,10 @@ main(int argc, char *const *argv) } } - argc -= optind; - argv += optind; + if (argc > 0) { + argc -= optind; + argv += optind; + } log_init(log_colorize, false, LOG_FACILITY_USER, log_level); diff --git a/main.c b/main.c index 7370de29..34f72a7c 100644 --- a/main.c +++ b/main.c @@ -168,7 +168,7 @@ main(int argc, char *const *argv) * don't pass this on to programs launched by us */ unsetenv("DESKTOP_STARTUP_ID"); - const char *const prog_name = argv[0]; + const char *const prog_name = argc > 0 ? argv[0] : ""; static const struct option longopts[] = { {"config", required_argument, NULL, 'c'}, @@ -401,8 +401,10 @@ main(int argc, char *const *argv) as_server && log_syslog, (enum fcft_log_class)log_level); - argc -= optind; - argv += optind; + if (argc > 0) { + argc -= optind; + argv += optind; + } LOG_INFO("%s", version_and_features());