Fix warning in posix/tst-getopt_long1.c

Message ID alpine.DEB.2.10.1411252352150.17237@digraph.polyomino.org.uk
State Superseded
Headers

Commit Message

Joseph Myers Nov. 25, 2014, 11:53 p.m. UTC
  This patch fixes a "discards qualifiers" warning in
posix/tst-getopt_long1.c.  glibc is built with -Wwrite-strings,
meaning a char * cannot be initialized with a string constant; because
the array of char * gets passed to getopt_long, expecting char *const
*, the array can't be changed to one of const char *, hence the use of
compound literals to provide non-const character arrays initialized
with the strings, as explained in the comment added.

Tested for x86_64.

2014-11-25  Joseph Myers  <joseph@codesourcery.com>

	* posix/tst-getopt_long1.c (do_test): Use compound literals in
	argv array instead of directly using string constants.
  

Comments

Joseph Myers Dec. 2, 2014, 5:47 p.m. UTC | #1
Ping.  This patch 
<https://sourceware.org/ml/libc-alpha/2014-11/msg00741.html> is pending 
review.
  
Roland McGrath Dec. 2, 2014, 7:21 p.m. UTC | #2
The strings won't be modified.  So why not just add (char *) casts?
  

Patch

diff --git a/posix/tst-getopt_long1.c b/posix/tst-getopt_long1.c
index e0ecd12..daabcc6 100644
--- a/posix/tst-getopt_long1.c
+++ b/posix/tst-getopt_long1.c
@@ -39,7 +39,11 @@  do_test (void)
       return 1;
     }
 
-  char *argv[] = { "program", "--on" };
+  /* Because getopt_long takes a char *const * argument, this array
+     cannot be const char *[]; because glibc uses -Wwrite-strings, the
+     compound literals are needed to provide pointers non-const
+     character arrays, instead of using string literals directly.  */
+  char *argv[] = { (char []) { "program" }, (char []) { "--on" } };
   int argc = 2;
 
   int c = getopt_long (argc, argv, "12345", opts, NULL);