c - Why does popt segfault when a string is too short? -


why popt segfaulting when argdescrip string isn't long enough?

take following example:

#include <popt.h>  struct poptoption options[] = {   popt_autohelp   {     .longname = "color",     .shortname = '\0',     .arginfo = popt_arg_string                | popt_argflag_optional                | popt_argflag_show_default,     .arg = (void*) "auto",     .val = 1,     .descrip = "whether or not use colored output",     .argdescrip = "always|never|auto (checks if tty)"   },   popt_tableend };   int main(int argc, const char** argv) {   // popt context   poptcontext ctx = poptgetcontext(       "program",       argc, argv,       options,       popt_context_posixmeharder);    // parse   poptgetnextopt(ctx);    return 0; } 

the above segfaults:

/tmp$ ./a.out --help usage: a.out [option...] [1]    47468 segmentation fault  ./a.out --help 

although changing .argdescrip to

.argdescrip = "always|never|auto (checks if tty) "               "........................................." 

popt happily accepts , displays output:

/tmp$ ./a.out --help usage: a.out [option...]       --color[=always|never|auto (checks if tty) .........................................]     whether or not use colored output  options:   -?, --help                                                                                  show message     --usage display brief usage message 

what gives? missing in c spec? popt man pages don't specify required length. bug?

your problem else. have set arg member constant string while have needed set pointer such (the man page says, arg should initialized pointer char *). program dies while trying dereference pointer isn't one.

try this:

char *garg = "auto";  struct poptoption options[] = {     popt_autohelp     {         .longname = "color",         .shortname = '\0',         .arginfo = popt_arg_string                 | popt_argflag_optional                 | popt_argflag_show_default,         .arg = &garg,          .val = 1,         .descrip = "whether or not use colored output",         .argdescrip = "always|never|auto (checks if tty)"     },     popt_tableend }; 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -