bug-coreutils
[Top][All Lists]
Advanced

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

Re: coreutils-6.3 on MacOS X


From: Paul Eggert
Subject: Re: coreutils-6.3 on MacOS X
Date: Mon, 09 Oct 2006 12:27:56 -0700
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Bruno Haible <address@hidden> writes:

> Here the problem is:
>       mbstate_t mbstate = {0};
> ISO C 99 guarantees only that mbstate_t is not an array type; it could
> be a scalar or pointer type.

But that's OK.  The initializer is valid C89 (and C99) even if
mbstate_t is a scalar or pointer type.  The following is standard C:

  int x = { 0 };

Similarly, the following is also valid C:

  mbstate_t mbstate = {0};

regardless of whether mbstate_t is a struct, or an array, or a scalar,
or a union.

Changing the code to use memset makes the code less readable and more
error-prone.  Also, it has some problems on implementations where null
pointers and/or floating point values do not have all-bits-zero
representation; a problem in this area is has almost zero probability
for GNU code and mbstate_t but if it's easy to make the code portable
then we might as well do so.

This subject has come up a couple of times before.  In
<http://lists.gnu.org/archive/html/bug-coreutils/2005-06/msg00156.html>
I noted that glibc now sometimes uses the following style when the
type isn't known to be a structure or a scalar:

   sometype somevar = { 0, };

as an indication to the reader as to what is going on.

The question is whether we want to use this style, which I think is
nicer, or cater to "gcc -Wall" even if its warning is
counterproductive.  Personally I'd rather do the former, so I'd prefer
the following patch, but it's really up to Jim.

2006-10-09  Paul Eggert  <address@hidden>

        * lib/quotearg.c (quotearg_buffer_restyled): Use
        initializer rather than memset to initialize an object
        to zero.  This is easier to read and is less likely to
        introduce an runtime error due to a mixup.  It causes
        "gcc -Wall" to issue a warning, but you can work around
        this by appending -Wno-missing-braces.
        * src/ls.c (quote_name): Likewise.
        * src/pathchk.c (portable_chars_only): Likewise.
        * src/shred.c (main): Likewise.
        * src/stty.c (main): Likewise.
        * src/tr.c (card_of_complement): Likewise.
        * src/wc.c (wc): Likewise.

Index: lib/quotearg.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/quotearg.c,v
retrieving revision 1.50
diff -p -u -r1.50 quotearg.c
--- lib/quotearg.c      3 Oct 2006 06:33:39 -0000       1.50
+++ lib/quotearg.c      9 Oct 2006 19:14:01 -0000
@@ -415,8 +415,7 @@ quotearg_buffer_restyled (char *buffer, 
              }
            else
              {
-               mbstate_t mbstate;
-               memset (&mbstate, 0, sizeof mbstate);
+               mbstate_t mbstate = { 0, };
 
                m = 0;
                printable = true;
Index: src/ls.c
===================================================================
RCS file: /fetish/cu/src/ls.c,v
retrieving revision 1.442
diff -p -u -r1.442 ls.c
--- src/ls.c    3 Sep 2006 02:53:16 -0000       1.442
+++ src/ls.c    9 Oct 2006 19:14:17 -0000
@@ -3604,8 +3604,7 @@ quote_name (FILE *out, const char *name,
                     reach its end, replacing each non-printable multibyte
                     character with a single question mark.  */
                  {
-                   mbstate_t mbstate;
-                   memset (&mbstate, 0, sizeof mbstate);
+                   mbstate_t mbstate = { 0, };
                    do
                      {
                        wchar_t wc;
Index: src/pathchk.c
===================================================================
RCS file: /fetish/cu/src/pathchk.c,v
retrieving revision 1.89
diff -p -u -r1.89 pathchk.c
--- src/pathchk.c       9 Oct 2006 11:56:41 -0000       1.89
+++ src/pathchk.c       9 Oct 2006 19:14:19 -0000
@@ -200,11 +200,8 @@ portable_chars_only (char const *file, s
 
   if (*invalid)
     {
-      mbstate_t mbstate;
-      size_t charlen;
-
-      memset (&mbstate, 0, sizeof mbstate);
-      charlen = mbrlen (invalid, filelen - validlen, &mbstate);
+      mbstate_t mbstate = { 0, };
+      size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
       error (0, 0,
             _("nonportable character %s in file name %s"),
             quotearg_n_style_mem (1, locale_quoting_style, invalid,
Index: src/shred.c
===================================================================
RCS file: /fetish/cu/src/shred.c,v
retrieving revision 1.130
diff -p -u -r1.130 shred.c
--- src/shred.c 3 Sep 2006 02:53:16 -0000       1.130
+++ src/shred.c 9 Oct 2006 19:14:21 -0000
@@ -1091,7 +1091,7 @@ int
 main (int argc, char **argv)
 {
   bool ok = true;
-  struct Options flags;
+  struct Options flags = { 0, };
   char **file;
   int n_files;
   int c;
@@ -1106,8 +1106,6 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  memset (&flags, 0, sizeof flags);
-
   flags.n_iterations = DEFAULT_PASSES;
   flags.size = -1;
 
Index: src/stty.c
===================================================================
RCS file: /fetish/cu/src/stty.c,v
retrieving revision 1.138
diff -p -u -r1.138 stty.c
--- src/stty.c  9 Jul 2005 07:39:04 -0000       1.138
+++ src/stty.c  9 Oct 2006 19:14:23 -0000
@@ -729,7 +729,10 @@ settings, CHAR is taken literally, or co
 int
 main (int argc, char **argv)
 {
-  struct termios mode;
+  /* Initialize to all zeroes so there is no risk memcmp will report a
+     spurious difference in an uninitialized portion of the structure.  */
+  struct termios mode = { 0, };
+
   enum output_type output_type;
   int optc;
   int argi = 0;
@@ -840,9 +843,6 @@ main (int argc, char **argv)
   else
     device_name = _("standard input");
 
-  /* Initialize to all zeroes so there is no risk memcmp will report a
-     spurious difference in an uninitialized portion of the structure.  */
-  memset (&mode, 0, sizeof (mode));
   if (tcgetattr (STDIN_FILENO, &mode))
     error (EXIT_FAILURE, errno, "%s", device_name);
 
@@ -1002,7 +1002,9 @@ main (int argc, char **argv)
 
   if (require_set_attr)
     {
-      struct termios new_mode;
+      /* Initialize to all zeroes so there is no risk memcmp will report a
+        spurious difference in an uninitialized portion of the structure.  */
+      struct termios new_mode = { 0, };
 
       if (tcsetattr (STDIN_FILENO, TCSADRAIN, &mode))
        error (EXIT_FAILURE, errno, "%s", device_name);
@@ -1014,9 +1016,6 @@ main (int argc, char **argv)
         this partial failure, get the current terminal attributes and
         compare them to the requested ones.  */
 
-      /* Initialize to all zeroes so there is no risk memcmp will report a
-        spurious difference in an uninitialized portion of the structure.  */
-      memset (&new_mode, 0, sizeof (new_mode));
       if (tcgetattr (STDIN_FILENO, &new_mode))
        error (EXIT_FAILURE, errno, "%s", device_name);
 
Index: src/tr.c
===================================================================
RCS file: /fetish/cu/src/tr.c,v
retrieving revision 1.145
diff -p -u -r1.145 tr.c
--- src/tr.c    9 Jul 2006 17:04:48 -0000       1.145
+++ src/tr.c    9 Oct 2006 19:14:24 -0000
@@ -1177,9 +1177,8 @@ card_of_complement (struct Spec_list *s)
 {
   int c;
   int cardinality = N_CHARS;
-  bool in_set[N_CHARS];
+  bool in_set[N_CHARS] = { 0, };
 
-  memset (in_set, 0, sizeof in_set);
   s->state = BEGIN_STATE;
   while ((c = get_next (s, NULL)) != -1)
     {
Index: src/wc.c
===================================================================
RCS file: /fetish/cu/src/wc.c,v
retrieving revision 1.112
diff -p -u -r1.112 wc.c
--- src/wc.c    21 Aug 2006 07:30:47 -0000      1.112
+++ src/wc.c    9 Oct 2006 19:14:27 -0000
@@ -273,7 +273,7 @@ wc (int fd, char const *file_x, struct f
     {
       bool in_word = false;
       uintmax_t linepos = 0;
-      mbstate_t state;
+      mbstate_t state = { 0, };
       uintmax_t last_error_line = 0;
       int last_error_errno = 0;
 # if SUPPORT_OLD_MBRTOWC
@@ -289,7 +289,6 @@ wc (int fd, char const *file_x, struct f
       const size_t prev = 0;
 # endif
 
-      memset (&state, 0, sizeof (mbstate_t));
       while ((bytes_read = safe_read (fd, buf + prev, BUFFER_SIZE - prev)) > 0)
        {
          const char *p;




reply via email to

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