NationalSecurityAgency/ghidra · error

%s: illegal option -- %c

Error message

%s: illegal option -- %c

What it means

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.

Source

Thrown at GPL/DemanglerGnu/src/demangler_gnu_v2_24/c/getopt.c:794

    }

  /* Look at and handle the next short option-character.  */

  {
    char c = *nextchar++;
    char *temp = my_index (optstring, c);

    /* Increment `optind' when we start to process its last character.  */
    if (*nextchar == '\0')
      ++optind;

    if (temp == NULL || c == ':')
      {
	if (opterr)
	  {
	    if (posixly_correct)
	      /* 1003.2 specifies the format of this message.  */
	      fprintf (stderr, _("%s: illegal option -- %c\n"),
		       argv[0], c);
	    else
	      fprintf (stderr, _("%s: invalid option -- %c\n"),
		       argv[0], c);
	  }
	optopt = c;
	return '?';
      }
    /* Convenience. Treat POSIX -W foo same as long option --foo */
    if (temp[0] == 'W' && temp[1] == ';')
      {
	char *nameend;
	const struct option *p;
	const struct option *pfound = NULL;
	int exact = 0;
	int ambig = 0;
	int indfound = 0;
	int option_index;

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Remove the unsupported `-X` option
  2. Run `tool --help` or `man tool` for valid short options
  3. Unset POSIXLY_CORRECT if the POSIX wording is unexpected

Example fix

// before
POSIXLY_CORRECT=1 ./c++filt -Z
// after
POSIXLY_CORRECT=1 ./c++filt -n
Defensive patterns

Strategy: validation

Validate before calling

// Validate that every short-option character exists in optstring
static int all_short_opts_valid(int argc, char *const argv[], const char *optstring) {
    for (int i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != '-'; i++) {
        for (const char *p = argv[i] + 1; *p; p++) {
            if (strchr(optstring, *p) == NULL) return 0;
        }
    }
    return 1;
}

Type guard

static int is_valid_short_opt(char c, const char *optstring) {
    return strchr(optstring, c) != NULL;
}

Try / catch

int c;
while ((c = getopt(argc, argv, optstring)) != -1) {
    if (c == '?') { exit(EXIT_FAILURE); }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/d41ae9921e397860. Report an issue: GitHub.