bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

argp vs ':', '+', and '-' in getopt


From: Ben Asselstine
Subject: argp vs ':', '+', and '-' in getopt
Date: Fri, 6 Nov 2009 09:24:49 +0000

Hi folks,

Argp doesn't protect the user from picking short options that
interfere with argp's getopt implementation.

I apologize in advance for any line-splitting that gmail does to this message.

Consider the options in the following program:

$ ./bug1 --help
Usage: bug1 [OPTION...]

  -+, --plus                 add a plus sign
  --, --hyphen               add a hyphen sign
  -:, --colon                add a colon
  -;, --semicolon            add a semicolon
  -?, --help                 give this help list
      --usage                give a short usage message
$ ./bug1 -\;
./bug1: option requires an argument -- ;
Try `bug1 --help' or `bug1 --usage' for more information.
$ ./bug1 -:
./bug1: invalid option -- :
Try `bug1 --help' or `bug1 --usage' for more information.
$ ./bug1 --
$ ./bug1 -\; -+
semicolon
$

#include "argp.h"
#include <stdio.h>

static int
parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case ';': printf ("semicolon\n"); break;
    case ':': printf ("colon\n"); break;
    case '-': printf ("hyphen\n"); break;
    case '+': printf ("plus\n"); break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

int
main (int argc, char **argv)
{
  struct argp_option options[] = {
      { "semicolon", ';', 0, 0, "add a semicolon"},
      { "colon", ':', 0, 0, "add a colon"},
      { "plus", '+', 0, 0, "add a plus sign"},
      { "hyphen", '-', 0, 0, "add a hyphen sign"},
      { 0 }
  };

  struct argp argp = { options, parse_opt, 0, ""};
  argp_parse (&argp, argc, argv, 0, 0, 0);

}

--- argp.h.orig 2009-11-06 09:14:53.000000000 +0000
+++ argp.h      2009-11-06 09:14:34.000000000 +0000
@@ -623,7 +623,8 @@
   else
     {
       int __key = __opt->key;
-      return __key > 0 && __key <= UCHAR_MAX && isprint (__key);
+      return __key > 0 && __key <= UCHAR_MAX && isprint (__key) &&
+       __key != ':' && __key != '+' && __key != '-';
     }
 }

I hope this trivial patch can be applied without the need for
copyright assignment.

regards,

Ben




reply via email to

[Prev in Thread] Current Thread [Next in Thread]