Fix compile warning in symtab.c

Message ID 20190822225033.27254-1-cbiesinger@google.com
State New, archived
Headers

Commit Message

Terekhov, Mikhail via Gdb-patches Aug. 22, 2019, 10:50 p.m. UTC
  My compiler (g++ 7.4.0) can't tell that *bsc_ptr and *slot_ptr are
only used in the cases when it does get initialized. Just initialize
the vars earlier to avoid the warning, there does not seem to be a
downside to it.

../../gdb/symtab.c: In function ‘block_symbol lookup_static_symbol(const char*, domain_enum)’:
../../gdb/symtab.c:1366:11: warning: ‘slot’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     xfree (slot->value.not_found.name);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../gdb/symtab.c:2578:29: note: ‘slot’ was declared here
   struct symbol_cache_slot *slot;
                             ^~~~
../../gdb/symtab.c:1405:3: warning: ‘bsc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (bsc == NULL)
   ^~
../../gdb/symtab.c:2577:30: note: ‘bsc’ was declared here
   struct block_symbol_cache *bsc;
                              ^~~
../../gdb/symtab.c: In function ‘block_symbol lookup_global_symbol(const char*, const block*, domain_enum)’:
../../gdb/symtab.c:1366:11: warning: ‘slot’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     xfree (slot->value.not_found.name);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../gdb/symtab.c:2658:29: note: ‘slot’ was declared here
   struct symbol_cache_slot *slot;
                             ^~~~
../../gdb/symtab.c:1409:14: warning: ‘bsc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       ++bsc->collisions;
         ~~~~~^~~~~~~~~~
../../gdb/symtab.c:2657:30: note: ‘bsc’ was declared here
   struct block_symbol_cache *bsc;
                              ^~~

gdb/ChangeLog:

2019-08-22  Christian Biesinger  <cbiesinger@google.com>

	* symtab.c (symbol_cache_lookup): Always initialize *bsc_ptr and *slot_ptr.
---
 gdb/symtab.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
  

Comments

Tom Tromey Aug. 23, 2019, 5:03 p.m. UTC | #1
>>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:

Christian> My compiler (g++ 7.4.0) can't tell that *bsc_ptr and *slot_ptr are
Christian> only used in the cases when it does get initialized. Just initialize
Christian> the vars earlier to avoid the warning, there does not seem to be a
Christian> downside to it.

I think this patch is fine, but I don't understand the connection
between the patch and the warnings.

Christian> ../../gdb/symtab.c: In function ‘block_symbol lookup_static_symbol(const char*, domain_enum)’:
Christian> ../../gdb/symtab.c:1366:11: warning: ‘slot’ may be used uninitialized in this function [-Wmaybe-uninitialized]

It seems to me that if the patch fixes this warning, then it must just
be a gcc bug?

Anyway, this is ok.

Tom
  
Terekhov, Mikhail via Gdb-patches Aug. 24, 2019, 9:47 p.m. UTC | #2
On Fri, Aug 23, 2019 at 1:03 PM Tom Tromey <tom@tromey.com> wrote:
>
> >>>>> "Christian" == Christian Biesinger via gdb-patches <gdb-patches@sourceware.org> writes:
>
> Christian> My compiler (g++ 7.4.0) can't tell that *bsc_ptr and *slot_ptr are
> Christian> only used in the cases when it does get initialized. Just initialize
> Christian> the vars earlier to avoid the warning, there does not seem to be a
> Christian> downside to it.
>
> I think this patch is fine, but I don't understand the connection
> between the patch and the warnings.

GCC can't tell that slot/bsc are only used in the codepath when
symbol_cache_lookup returns "entry not found", so this patch always
initializes them.

> Christian> ../../gdb/symtab.c: In function ‘block_symbol lookup_static_symbol(const char*, domain_enum)’:
> Christian> ../../gdb/symtab.c:1366:11: warning: ‘slot’ may be used uninitialized in this function [-Wmaybe-uninitialized]
>
> It seems to me that if the patch fixes this warning, then it must just
> be a gcc bug?

Yeah, arguably this is a bug that gcc's control flow analysis isn't
smart enough to detect this.

> Anyway, this is ok.

Thanks, pushed.

Christian
  

Patch

diff --git a/gdb/symtab.c b/gdb/symtab.c
index cd208ab921..d85c77b4ce 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -1297,9 +1297,8 @@  set_symbol_cache_size_handler (const char *args, int from_tty,
    The result is the symbol if found, SYMBOL_LOOKUP_FAILED if a previous lookup
    failed (and thus this one will too), or NULL if the symbol is not present
    in the cache.
-   If the symbol is not present in the cache, then *BSC_PTR and *SLOT_PTR are
-   set to the cache and slot of the symbol to save the result of a full lookup
-   attempt.  */
+   *BSC_PTR and *SLOT_PTR are set to the cache and slot of the symbol, which
+   can be used to save the result of a full lookup attempt.  */
 
 static struct block_symbol
 symbol_cache_lookup (struct symbol_cache *cache,
@@ -1326,6 +1325,9 @@  symbol_cache_lookup (struct symbol_cache *cache,
   hash = hash_symbol_entry (objfile_context, name, domain);
   slot = bsc->symbols + hash % bsc->size;
 
+  *bsc_ptr = bsc;
+  *slot_ptr = slot;
+
   if (eq_symbol_entry (slot, objfile_context, name, domain))
     {
       if (symbol_lookup_debug)
@@ -1343,9 +1345,6 @@  symbol_cache_lookup (struct symbol_cache *cache,
 
   /* Symbol is not present in the cache.  */
 
-  *bsc_ptr = bsc;
-  *slot_ptr = slot;
-
   if (symbol_lookup_debug)
     {
       fprintf_unfiltered (gdb_stdlog,