[posix] fix ot-ctl crash on unrecognized long options (#13423)

The `kOptions` array passed to `getopt_long()` is missing its required
terminating entry. When `getopt_long()` is given a long option that does
not match any entry, it walks past the end of the array and dereferences
whatever happens to follow it:

    $ ot-ctl --version
    Segmentation fault

    #0  __strncmp_evex ()
    #1  process_long_option (..., longopts=kOptions, ...) at getopt.c:212
    #2  _getopt_internal_r (...) at getopt.c:650
    #5  ParseArg (...) at src/posix/client.cpp:239
    #6  main (argc=2, ...) at src/posix/client.cpp:273

`--help` happens to match the second entry and so returns before running
off the end, which is why only unrecognized long options crash. Short
options are unaffected, as they never walk the long option array.

Add the terminating entry.

Also declare `--help` as `no_argument`. It takes no argument, and the
short form already has none in the option string ("+I:h"), so
`ot-ctl --help` failed with "option '--help' requires an argument" and
exited non-zero instead of printing the usage.

Assisted-By: Claude Opus 4.8
This commit is contained in:
Christian Glombek
2026-07-27 19:10:11 -07:00
committed by GitHub
parent 803266d2f2
commit 72e383919b
+2 -1
View File
@@ -210,7 +210,8 @@ constexpr char kOptHelp = 'h';
const struct option kOptions[] = {
{"interface-name", required_argument, NULL, kOptInterfaceName},
{"help", required_argument, NULL, kOptHelp},
{"help", no_argument, NULL, kOptHelp},
{nullptr, 0, nullptr, 0},
};
void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode)