why was the VAR 'optind' never changed in initdb? - Mailing list pgsql-hackers

From Clover White
Subject why was the VAR 'optind' never changed in initdb?
Date
Msg-id CAEwsEE48V3aa7oPQhNBz3S28rABbXuTasOGDPf=hoRVJy2A4fw@mail.gmail.com
Whole thread Raw
Responses Re: why was the VAR 'optind' never changed in initdb?  (Andrew Dunstan <andrew@dunslane.net>)
Re: why was the VAR 'optind' never changed in initdb?  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
Hi,<br />  I'm debugging initdb using gdb.<br />  I found that I could not step in the function getopt_long in line
2572in initdb.c.<br />  I also found that the value of VAR optind never be changed. VAR optind is always equal to 1 but
howcould optind be larger than the value of argc(the value of argc is 6) in line 2648 and 2654.<br /><br />I was so
confused.Could someone give me some help? Thank you~<br /><br />here is my configure:<br />./configure CFLAGS=-O0
--enable-debug--enable-depend --enable-cassert --prefix=/home/pgsql/pgsql<br /><br />follows is my debug log by gdb:<br
/><br/>[pgsql@vmlinux postgresql-9.1.2]$ gdb initdb<br />GNU gdb Red Hat Linux (6.3.0.0-1.63rh)<br />Copyright 2004
FreeSoftware Foundation, Inc.<br />GDB is free software, covered by the GNU General Public License, and you are<br />
welcometo change it and/or distribute copies of it under certain conditions.<br />Type "show copying" to see the
conditions.<br/>There is absolutely no warranty for GDB.  Type "show warranty" for details.<br /> This GDB was
configuredas "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".<br /><br />(gdb)
setargs -U pgsql -W -D /home/pgsql/pg_data<br />(gdb) b main<br />Breakpoint 1 at 0x804d133: file initdb.c, line
2553.<br/> (gdb) b 2572<br />Breakpoint 2 at 0x804d20c: file initdb.c, line 2572.<br />(gdb) run<br />Starting program:
/home/pgsql/pgsql/bin/initdb-U pgsql -W -D /home/pgsql/pg_data<br /><br />Breakpoint 1, main (argc=6, argv=0xbfec0ad4)
atinitdb.c:2553<br /> 2553            progname = get_progname(argv[0]);<br />(gdb) c<br />Continuing.<br /><br
/>Breakpoint2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572<br />2572            while ((c = getopt_long(argc, argv,
"dD:E:L:nU:WA:sT:X:",long_options, &option_index)) != -1)<br /> (gdb) p optind<br />$1 = 1<br />(gdb) s<br
/>2574                   switch (c)<br />(gdb) n<br />2589                                    username =
xstrdup(optarg);<br/>(gdb) <br />2590                                    break;<br />(gdb) p optind<br /> $2 = 1<br
/>(gdb)n<br /><br />Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572<br />2572            while ((c =
getopt_long(argc,argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1)<br />(gdb) p optind<br /> $3 =
1<br/>(gdb) n<br />2574                    switch (c)<br />(gdb) p optind<br />$4 = 1<br />(gdb) n<br
/>2586                                   pwprompt = true;<br />(gdb) <br />2587                                   
break;<br/>(gdb) <br /><br />Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at initdb.c:2572<br />2572            while
((c= getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1)<br />(gdb) p optind<br />$5
=1<br /> (gdb) n<br />2574                    switch (c)<br />(gdb) <br />2580                                   
pg_data= xstrdup(optarg);<br />(gdb) p optarg<br />$6 = 0x0<br />(gdb) n<br />2581                                   
break;<br/> (gdb) p optarg<br /> $7 = 0x0<br />(gdb) n<br /><br />Breakpoint 2, main (argc=6, argv=0xbfec0ad4) at
initdb.c:2572<br/>2572            while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options,
&option_index))!= -1)<br /> (gdb) p pg_data<br /> $8 = 0x9d328e8 "/home/pgsql/pg_data"<br />(gdb) n<br
/>2648           if (optind < argc)<br />(gdb) p optind<br />$9 = 1<br />(gdb) p argc<br />$10 = 6<br />(gdb) n<br
/>2654           if (optind < argc)<br />(gdb) p optind<br /> $11 = 1<br />(gdb) p argc<br />$12 = 6<br />(gdb) n<br
/>2663           if (pwprompt && pwfilename)<br />(gdb) <br clear="all" /><br />-- <br />Clover White<br /><br
/>

pgsql-hackers by date:

Previous
From: Ashutosh Bapat
Date:
Subject: Potential for bugs while using COPY_POINTER_FIELD to copy NULL pointer
Next
From: Andrew Dunstan
Date:
Subject: Re: why was the VAR 'optind' never changed in initdb?