NationalSecurityAgency/ghidra · error

%s: option requires an argument -- %c\n

Error message

%s: option requires an argument -- %c\n

What it means

Same 'option requires an argument' condition as error 24 but in the non-reentrant getopt code path (the _getopt_internal wrapper at line 956, not the _r reentrant variant at line 827). A short option marked with ':' in optstring was the last token. The non-reentrant variant sets c but does not return immediately, differing slightly from the reentrant path.

Source

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

	      optarg = NULL;
	    nextchar = NULL;
	  }
	else
	  {
	    /* This is an option that requires an argument.  */
	    if (*nextchar != '\0')
	      {
		optarg = nextchar;
		/* If we end this ARGV-element by taking the rest as an arg,
		   we must advance to the next element now.  */
		optind++;
	      }
	    else if (optind == argc)
	      {
		if (opterr)
		  {
		    /* 1003.2 specifies the format of this message.  */
		    fprintf (stderr,
			   _("%s: option requires an argument -- %c\n"),
			   argv[0], c);
		  }
		optopt = c;
		if (optstring[0] == ':')
		  c = ':';
		else
		  c = '?';
	      }
	    else
	      /* We already incremented `optind' once;
		 increment it again when taking next ARGV-elt as argument.  */
	      optarg = argv[optind++];
	    nextchar = NULL;
	  }
      }
    return c;
  }

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Provide the required argument value after the option
  2. Check that shell quoting preserves the argument
  3. Restructure the command to avoid trailing required-arg options

Example fix

// before
./c++filt -s
// after
./c++filt -s gnu-v3
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check: ensure no trailing option-requiring-argument is left without a value
static int no_trailing_missing_arg(int argc, char *const argv[], const char *optstring) {
    if (argc < 1) return 1;
    const char *last = argv[argc - 1];
    if (last[0] == '-' && last[1] != '-') {
        char last_char = last[strlen(last) - 1];
        char *pos = strchr(optstring, last_char);
        if (pos && pos[1] == ':') return 0; // trailing option needs arg
    }
    return 1;
}

Try / catch

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

Prevention

When it happens

Trigger: Tool calls the non-reentrant getopt()/getopt_long() rather than getopt_r(); user passes an argument-requiring short option as the final token; optind == argc. opterr is non-zero.

Common situations: Same as error 24: trailing option without its value. The distinction is internal to which getopt variant the tool links against.

Related errors


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