This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Silence -Wmaybe-uninitialized warning in minsyms.c:lookup_minimal_symbol_by_pc_section


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

commit 6ae502670996ec6e8b574dbaa4577d79fa9e9799
Author: Pedro Alves <palves@redhat.com>
Date:   Tue Jun 19 18:16:40 2018 +0100

    Silence -Wmaybe-uninitialized warning in minsyms.c:lookup_minimal_symbol_by_pc_section
    
    Compiling with GCC 8.1 shows this warning:
    
      gdb/minsyms.c: In function 'bound_minimal_symbol lookup_minimal_symbol_by_pc_section(CORE_ADDR, obj_section*, lookup_msym_prefer)':
      gdb/minsyms.c:825:40: warning: 'want_type' may be used uninitialized in this function [-Wmaybe-uninitialized]
    	   && MSYMBOL_TYPE (&msymbol[hi]) != want_type
    
    That warning is a false positive, because the switch that converts
    enum lookup_msym_prefer values to enum enum minimal_symbol_type values
    has a case for every lookup_msym_prefer enumerator:
    
      switch (prefer)
       {
        case lookup_msym_prefer::TEXT:
          want_type = mst_text;
          break;
        case lookup_msym_prefer::TRAMPOLINE:
          want_type = mst_solib_trampoline;
          break;
        case lookup_msym_prefer::GNU_IFUNC:
          want_type = mst_text_gnu_ifunc;
          break;
        }
    
    The problem is that GCC assumes that enum variables may hold values
    other than the named enumerators (like e.g., "lookup_msym_prefer
    prefer = (lookup_msym_prefer) 10;").
    
    Rework the code a bit, adding a gdb_assert to make it explicit to the
    compiler that want_type is initialized in all normal-return paths.
    
    gdb/ChangeLog:
    2018-06-19  Pedro Alves  <palves@redhat.com>
    
    	* minsyms.c (msym_prefer_to_msym_type): New, factored out from ...
    	(lookup_minimal_symbol_by_pc_section): ... here with
    	gdb_assert_not_reached added.

Diff:
---
 gdb/ChangeLog |  6 ++++++
 gdb/minsyms.c | 35 ++++++++++++++++++++++-------------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 93703fd..b52f106 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
 2018-06-19  Pedro Alves  <palves@redhat.com>
 
+	* minsyms.c (msym_prefer_to_msym_type): New, factored out from ...
+	(lookup_minimal_symbol_by_pc_section): ... here with
+	gdb_assert_not_reached added.
+
+2018-06-19  Pedro Alves  <palves@redhat.com>
+
 	* inline-frame.c (stopped_by_user_bp_inline_frame): Replace PC
 	parameter with a block parameter.  Compare location's block symbol
 	with the frame's block instead of addresses.
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 4882e58..4409e6f 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -656,6 +656,27 @@ frob_address (struct objfile *objfile, CORE_ADDR *pc)
   return 0;
 }
 
+/* Helper for lookup_minimal_symbol_by_pc_section.  Convert a
+   lookup_msym_prefer to a minimal_symbol_type.  */
+
+static minimal_symbol_type
+msym_prefer_to_msym_type (lookup_msym_prefer prefer)
+{
+  switch (prefer)
+    {
+    case lookup_msym_prefer::TEXT:
+      return mst_text;
+    case lookup_msym_prefer::TRAMPOLINE:
+      return mst_solib_trampoline;
+    case lookup_msym_prefer::GNU_IFUNC:
+      return mst_text_gnu_ifunc;
+    }
+
+  /* Assert here instead of in a default switch case above so that
+     -Wswitch warns if a new enumerator is added.  */
+  gdb_assert_not_reached ("unhandled lookup_msym_prefer");
+}
+
 /* Search through the minimal symbol table for each objfile and find
    the symbol whose address is the largest address that is still less
    than or equal to PC, and matches SECTION (which is not NULL).
@@ -683,7 +704,6 @@ lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *sectio
   struct minimal_symbol *best_symbol = NULL;
   struct objfile *best_objfile = NULL;
   struct bound_minimal_symbol result;
-  enum minimal_symbol_type want_type;
 
   if (section == NULL)
     {
@@ -692,18 +712,7 @@ lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *sectio
 	return {};
     }
 
-  switch (prefer)
-    {
-    case lookup_msym_prefer::TEXT:
-      want_type = mst_text;
-      break;
-    case lookup_msym_prefer::TRAMPOLINE:
-      want_type = mst_solib_trampoline;
-      break;
-    case lookup_msym_prefer::GNU_IFUNC:
-      want_type = mst_text_gnu_ifunc;
-      break;
-    }
+  minimal_symbol_type want_type = msym_prefer_to_msym_type (prefer);
 
   /* We can not require the symbol found to be in section, because
      e.g. IRIX 6.5 mdebug relies on this code returning an absolute


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]