diff --git a/smartmontools/ChangeLog b/smartmontools/ChangeLog index 296ef7be582bf8fbc01323a671a881a264a94fb9..eebc8cae31815ebb50d1d78383b1de713ab2c894 100644 --- a/smartmontools/ChangeLog +++ b/smartmontools/ChangeLog @@ -1,5 +1,12 @@ $Id$ +2024-05-08 Christian Franke <franke@computer.org> + + smartctl.cpp, smartd.cpp: Fix segfault on missing option argument (#1830). + + getopt*() from musl libc sets 'optind = argc + 1' if the argument of + a short option is missing. + 2024-04-26 Christian Franke <franke@computer.org> os_win32/vc1[67]/*: Set svn:eol-style=CRLF, MSVC does not preserve LF. diff --git a/smartmontools/smartctl.cpp b/smartmontools/smartctl.cpp index 15f22a7b7b555993e7b951dd1118a08d1d29690c..67d1beef2068291da666cc83db74b700f69584df 100644 --- a/smartmontools/smartctl.cpp +++ b/smartmontools/smartctl.cpp @@ -1168,7 +1168,8 @@ static int parse_options(int argc, char** argv, const char * & type, printing_is_off = false; printslogan(); // Point arg to the argument in which this option was found. - arg = argv[optind-1]; + // Note: getopt_long() may set optind > argc (e.g. musl libc) + arg = argv[optind <= argc ? optind - 1 : argc - 1]; // Check whether the option is a long option that doesn't map to -h. if (arg[1] == '-' && optchar != 'h') { // Iff optopt holds a valid option then argument must be missing. diff --git a/smartmontools/smartd.cpp b/smartmontools/smartd.cpp index b6473c8ea5d3de3653d6df4d52dd9c59ddb1f2bf..34f2cde23d5b04acfc39cc3e1e969e021d725fd1 100644 --- a/smartmontools/smartd.cpp +++ b/smartmontools/smartd.cpp @@ -5361,7 +5361,8 @@ static int parse_options(int argc, char **argv) debugmode=1; PrintHead(); // Point arg to the argument in which this option was found. - arg = argv[optind-1]; + // Note: getopt_long() may set optind > argc (e.g. musl libc) + arg = argv[optind <= argc ? optind - 1 : argc - 1]; // Check whether the option is a long option that doesn't map to -h. if (arg[1] == '-' && optchar != 'h') { // Iff optopt holds a valid option then argument must be missing.