[Bug build/26320] New: [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug build/26320] New: [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c

Sourceware - gdb-prs mailing list
https://sourceware.org/bugzilla/show_bug.cgi?id=26320

            Bug ID: 26320
           Summary: [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c
           Product: gdb
           Version: HEAD
            Status: NEW
          Severity: normal
          Priority: P2
         Component: build
          Assignee: unassigned at sourceware dot org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

When building with gcc 4.8.5, we run into:
...
src/gdb/cli/cli-style.c:154:42: warning: '*((void*)&<anonymous> +8)' may be
used uninitialized in this function [-Wmaybe-uninitialized]
src/gdb/cli/cli-style.c:154:42: warning: '*((void*)&<anonymous> +9)' may be
used uninitialized in this function [-Wmaybe-uninitialized]
src/gdb/cli/cli-style.c:154:42: warning: '*((void*)&<anonymous> +10)' may be
used uninitialized in this function [-Wmaybe-uninitialized]
...

The problem is basically here in class color, nested in struct ui_file_style in
gdb/ui-style.h:
...
    bool m_simple;
    int m_value;
    uint8_t m_red, m_green, m_blue;
...

The m_simple var determines what is valid:
- m_value (with m_simple == true)
- m_red, m_green, m_blue (with m_simple == false)

So, if we construct a color object like this:
...
    color (int c)
      : m_simple (true),
        m_value (c)
    {
      gdb_assert (c >= -1 && c <= 255);
    }
...
m_simple and m_value get initialized, but not m_red, m_green, m_blue.

The constructor is called here twice here in cli_style_option::style in
gdb/cli/cli-style.c:
...
  int fg = color_number (m_foreground);
  int bg = color_number (m_background);

  ...

  return ui_file_style (fg, bg, intensity);
...
but doesn't initialize m_red, m_green, m_blue, for both fg and bg, and then the
copy into the return value accesses an uninitialized value.

I guess this:
....
    bool m_simple;
    union {
      int m_value;
      uint8_t m_red, m_green, m_blue;
    };
...
would fix the warning.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug build/26320] [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c

Sourceware - gdb-prs mailing list
https://sourceware.org/bugzilla/show_bug.cgi?id=26320

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tromey at sourceware dot org

--- Comment #1 from Tom de Vries <vries at gcc dot gnu.org> ---
Yep, this fixes it:
...
diff --git a/gdb/ui-style.h b/gdb/ui-style.h
index 48ab52d5ea..9cb6dda5fc 100644
--- a/gdb/ui-style.h
+++ b/gdb/ui-style.h
@@ -126,8 +126,14 @@ struct ui_file_style
   private:

     bool m_simple;
-    int m_value;
-    uint8_t m_red, m_green, m_blue;
+    union
+    {
+      int m_value;
+      struct
+      {
+       uint8_t m_red, m_green, m_blue;
+      };
+    };
   };

   /* Intensity settings that are available.  */
...

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug build/26320] [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c

Sourceware - gdb-prs mailing list
In reply to this post by Sourceware - gdb-prs mailing list
https://sourceware.org/bugzilla/show_bug.cgi?id=26320

--- Comment #2 from cvs-commit at gcc dot gnu.org <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tom de Vries <[hidden email]>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5a99adb86018c10da294e7556544e401c492c2fb

commit 5a99adb86018c10da294e7556544e401c492c2fb
Author: Tom de Vries <[hidden email]>
Date:   Thu Jul 30 09:23:01 2020 +0200

    [gdb/build] Fix Wmaybe-uninitialized in gdb/ui-style.h

    When building CFLAGS/CXXFLAGS="-O2 -g -Wall" and gcc 4.8.5, we run into:
    ...
    src/gdb/cli/cli-style.c:154:42: warning: '*((void*)&<anonymous> +8)' \
      may be used uninitialized in this function [-Wmaybe-uninitialized]
    src/gdb/cli/cli-style.c:154:42: warning: '*((void*)&<anonymous> +9)' \
      may be used uninitialized in this function [-Wmaybe-uninitialized]
    src/gdb/cli/cli-style.c:154:42: warning: '*((void*)&<anonymous> +10)' \
      may be used uninitialized in this function [-Wmaybe-uninitialized]
    ...

    The root cause is that the data members of class color, nested in struct
    ui_file_style in gdb/ui-style.h:
    ...
        bool m_simple;
        int m_value;
        uint8_t m_red, m_green, m_blue;
    ...
    are only partially initialized by this constructor:
    ...
        color (int c)
          : m_simple (true),
            m_value (c)
        {
          gdb_assert (c >= -1 && c <= 255);
        }
    ...
    but the default copy constructor will copy all the fields.

    The member m_simple acts as a discriminant, to indicate which other members
    are valid:
    - m_value (with m_simple == true)
    - m_red, m_green, m_blue (with m_simple == false)
    So, we don't need storage for both m_value and m_red/m_green/m_blue at the
    same time.

    Fix this by wrapping the respective members in a union:
    ...
        bool m_simple;
        union
        {
          int m_value;
          struct
          {
           uint8_t m_red, m_green, m_blue;
          };
        };
    ...
    which also fixes the warning.

    Build and reg-tested on x86_64-linux.

    gdb/ChangeLog:

    2020-07-30  Tom de Vries  <[hidden email]>

            PR build/26320
            * ui-style.h (struct ui_file_style::color): Wrap m_value and
            m_red/m_green/m_blue in a union.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug build/26320] [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c

Sourceware - gdb-prs mailing list
In reply to this post by Sourceware - gdb-prs mailing list
https://sourceware.org/bugzilla/show_bug.cgi?id=26320

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #3 from Tom de Vries <vries at gcc dot gnu.org> ---
Patch fixing warning committed, marking resolved-fixed.

--
You are receiving this mail because:
You are on the CC list for the bug.
Reply | Threaded
Open this post in threaded view
|

[Bug build/26320] [Wmaybe-uninitialized] warning in gdb/cli/cli-style.c

Sourceware - gdb-prs mailing list
In reply to this post by Sourceware - gdb-prs mailing list
https://sourceware.org/bugzilla/show_bug.cgi?id=26320

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |10.1

--
You are receiving this mail because:
You are on the CC list for the bug.