{"record":{"id":"d41ae9921e397860","repo":"NationalSecurityAgency/ghidra","slug":"s-illegal-option-c","errorCode":null,"errorMessage":"%s: illegal option -- %c","messagePattern":"(.+?): illegal option -- %c","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c","lineNumber":794,"sourceCode":"    }\n\n  /* Look at and handle the next short option-character.  */\n\n  {\n    char c = *nextchar++;\n    char *temp = my_index (optstring, c);\n\n    /* Increment `optind' when we start to process its last character.  */\n    if (*nextchar == '\\0')\n      ++optind;\n\n    if (temp == NULL || c == ':')\n      {\n\tif (opterr)\n\t  {\n\t    if (posixly_correct)\n\t      /* 1003.2 specifies the format of this message.  */\n\t      fprintf (stderr, _(\"%s: illegal option -- %c\\n\"),\n\t\t       argv[0], c);\n\t    else\n\t      fprintf (stderr, _(\"%s: invalid option -- %c\\n\"),\n\t\t       argv[0], c);\n\t  }\n\toptopt = c;\n\treturn '?';\n      }\n    /* Convenience. Treat POSIX -W foo same as long option --foo */\n    if (temp[0] == 'W' && temp[1] == ';')\n      {\n\tchar *nameend;\n\tconst struct option *p;\n\tconst struct option *pfound = NULL;\n\tint exact = 0;\n\tint ambig = 0;\n\tint indfound = 0;\n\tint option_index;","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c#L776-L812","documentation":"In POSIX-conformant mode (posixly_correct flag set), getopt reports a short option character that is absent from the optstring as 'illegal option'. POSIX 1003.2 mandates this exact wording. The character `c` was not found by my_index and opterr is non-zero.","triggerScenarios":"POSIXLY_CORRECT environment variable is set (or the tool sets posixly_correct); user passes `-X` where `X` is not in the optstring; or `c == ':'` (colon used where not valid). opterr is non-zero.","commonSituations":"Running a tool under `POSIXLY_CORRECT=1` env that changes the error wording from 'invalid' to 'illegal'; passing a short option that the tool doesn't support; leftover options from a wrapper script.","solutions":["Remove the unsupported `-X` option","Run `tool --help` or `man tool` for valid short options","Unset POSIXLY_CORRECT if the POSIX wording is unexpected"],"exampleFix":"// before\nPOSIXLY_CORRECT=1 ./c++filt -Z\n// after\nPOSIXLY_CORRECT=1 ./c++filt -n","handlingStrategy":"validation","validationCode":"// Validate that every short-option character exists in optstring\nstatic int all_short_opts_valid(int argc, char *const argv[], const char *optstring) {\n    for (int i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != '-'; i++) {\n        for (const char *p = argv[i] + 1; *p; p++) {\n            if (strchr(optstring, *p) == NULL) return 0;\n        }\n    }\n    return 1;\n}","typeGuard":"static int is_valid_short_opt(char c, const char *optstring) {\n    return strchr(optstring, c) != NULL;\n}","tryCatchPattern":"int c;\nwhile ((c = getopt(argc, argv, optstring)) != -1) {\n    if (c == '?') { exit(EXIT_FAILURE); }\n}","preventionTips":["Set opterr = 0 and print a custom usage message on '?'","Check POSIXLY_CORRECT env to understand which wording you'll get","Validate option characters against optstring before calling getopt"],"tags":["cli","getopt","arguments","posix","c"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}