getopt.c: optarg should not be initialized

Christophe Lyon christophe.lyon@st.com
Tue Sep 21 16:12:00 GMT 2010


Hello,

I think that in newlib/libc/stdlib/getopt.c, optarg should not be 
initialized (like in glibc).

Otherwise, if the user code contains a non-initialized "optarg" variable 
the linker will "magically" pull the whole getopt.o from Newlib, which 
is not desirable (especially if the user code actually contains a 
redefinition of getopt & co, as it will result in "multiply defined" 
errors).

Small patch as follows:

Index: newlib/libc/stdlib/getopt.c
===================================================================
--- newlib/libc/stdlib/getopt.c (revision 637)
+++ newlib/libc/stdlib/getopt.c (working copy)
@@ -103,7 +103,7 @@ typedef enum GETOPT_ORDERING_T
  } GETOPT_ORDERING_T;

  /* globally-defined variables */
-char *optarg = 0;
+char *optarg;
  int optind = 0;
  int opterr = 1;
  int optopt = '?';

OK?

(There might be more implications I did not consider, so the real patch 
may be more complex).

Christophe.



More information about the Newlib mailing list