In helpcovid git commit e27dd3b123c18300665 we have a stupid bug related to Linux GNU glibc argp program argument parsing.

hcv_parse_program_arguments(int &argc, char**argv)
  struct argp_state argstate;
  memset (&argstate, 0, sizeof(argstate));
  argstate.input = &hcv_progargs;
  hcv_progargs.hcv_progmagic = HCV_PROGARG_MAGIC;
  static struct argp argparser;
  argparser.options = hcv_progoptions;
  argparser.parser = hcv_parse1opt;
  argparser.args_doc = "*no-positional-arguments*";
  argparser.doc = "github.com/bstarynk/helpcovid"
                  " - a GPLv3+ free software to help organizing"
                  " against Covid19 - NO WARRANTY";
  argparser.children = nullptr;
  argparser.help_filter = nullptr;
  argparser.argp_domain = nullptr;
  if (argp_parse(&argparser, argc, argv, 0, nullptr, nullptr))
    HCV_FATALOUT("failed to parse program arguments to " << argv[0]);
#warning TODO: complete hcv_parse_program_arguments
} // end hcv_parse_program_arguments

The bug is obvious, but I cannot find it.

At runtime, with the main function being:

main(int argc, char**argv)
  hcv_parse_program_arguments(argc, argv);
  HCV_SYSLOGOUT(LOG_NOTICE, "start of " << argv[0] << std::endl
                <<  " version:" << hcv_versionmsg);
  HCV_SYSLOGOUT(LOG_INFO, "normal end of " << argv[0]);
  return 0;
} // end of main

we observe immediately:

rimski.x86_64 ~/helpcovid 19:12 .0 % ./helpcovid 
** HELPCOVID FATAL! hcv_main.cc:84:: corrupted program arguments
**** FATAL ERROR hcv_main.cc:84
./helpcovid[3684197]: FATAL STOP hcv_main.cc:84 (Success)
zsh: abort (core dumped)  ./helpcovid

happening in

// parse a single program option
static error_t
hcv_parse1opt (int key, char *arg, struct argp_state *state)
  /* Get the input argument from argp_parse, which we
     know is a pointer to our arguments structure. */
  struct hcv_progarguments *progargs
    = reinterpret_cast<hcv_progarguments *>(state->input);
  if (!progargs || progargs->hcv_progmagic != HCV_PROGARG_MAGIC)
    // this should never happen
    HCV_FATALOUT("corrupted program arguments"); //@@@@@@@@@@@@@@@@@@@
  switch (key)
      return ARGP_ERR_UNKNOWN;
} // end hcv_parse1opt

The error being at line with //@@@@@@@@@@@@@@@@@@@, file hcv_main.cc line 84. The gdb debugger tells that progargs is a null pointer (and should not be).

  • \$\begingroup\$ Can you please edit the post to share some more details about the bug? Such as the expected behavior vs the observed behavior. \$\endgroup\$ – Nimesh Neema Mar 21 at 18:08
  • \$\begingroup\$ Isn't code on this site supposed to be bug free? From a meta question tagged as an FAQ, "A guide to Code Review for Stack Overflow users" (my emphasis): "Please, we don't want the following kinds of questions ... questions where the code does not work as intended" \$\endgroup\$ – Peter Mortensen Mar 22 at 2:34

I think that the call to argp_parse() in hcv_parse_program_arguments() is missing a pointer to the arguments struct. Looking at the main() function in the canonical Argp Example 3, we see that the argp_parse() function is passed a pointer to struct arguments arguments.

main (int argc, char **argv)
  struct arguments arguments;

  /* Default values. */
  arguments.silent = 0;
  arguments.verbose = 0;
  arguments.output_file = "-";

  /* Parse our arguments; every option seen by parse_opt will
     be reflected in arguments. */
  argp_parse (&argp, argc, argv, 0, 0, &arguments);

  printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
          "VERBOSE = %s\nSILENT = %s\n",
          arguments.args[0], arguments.args[1],
          arguments.verbose ? "yes" : "no",
          arguments.silent ? "yes" : "no");

  exit (0);
| improve this answer | |
  • 2
    \$\begingroup\$ Welcome to Code Review! Please note, questions involving code that's not working as intended, are off-topic on this site. If OP edits their post to address the problem and make their post on-topic, they make your answer moot. It is advised not to answer such questions. Protip: There are tons of on-topic questions to answer; you'll make more reputation faster if you review code in questions that will get higher view counts for being on-topic. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Mar 22 at 5:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.