Skip to contentSkip to conversation

hw/input/tsc2005: Fix -Wchar-subscripts warning in tsc2005_txrx()

PATCH Superseded Philippe Mathieu-Daudé 2 participants 2 messages May 2, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/input/tsc2005: Fix -Wchar-subscripts warning in tsc2005_txrx()

Check the function index is not negative and use an unsigned variable to avoid the following warning with GCC 13.2.0:

[666/5358] Compiling C object libcommon.fa.p/hw_input_tsc2005.c.o hw/input/tsc2005.c: In function 'tsc2005_timer_tick':
hw/input/tsc2005.c:416:26: warning: array subscript has type 'char' [-Wchar-subscripts]

    416 |     s->dav |= mode_regs[s->function];
        |                         ~^~~~~~~~~~
---
 hw/input/tsc2005.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/input/tsc2005.c +4 -1
index 941f163d36..fa93eb5d25 100644
@@ -406,6 +406,7 @@ uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
 static void tsc2005_timer_tick(void *opaque)
 {
     TSC2005State *s = opaque;
+    unsigned func_idx;
 
     /* Timer ticked -- a set of conversions has been finished.  */
 
@@ -413,7 +414,9 @@ static void tsc2005_timer_tick(void *opaque)
         return;
 
     s->busy = false;
-    s->dav |= mode_regs[s->function];
+    assert(s->function >= 0);
+    func_idx = s->function;
+    s->dav |= mode_regs[func_idx];
     s->function = -1;
     tsc2005_pin_update(s);
 }
Signature · 1 line
2.41.0
#1 lore raw
Quoted 28 lines · Philippe Mathieu-Daudé

Check the function index is not negative and use an unsigned variable to avoid the following warning with GCC 13.2.0:

[666/5358] Compiling C object libcommon.fa.p/hw_input_tsc2005.c.o hw/input/tsc2005.c: In function 'tsc2005_timer_tick':
hw/input/tsc2005.c:416:26: warning: array subscript has type 'char' [-Wchar-subscripts]

    416 |     s->dav |= mode_regs[s->function];
        |                         ~^~~~~~~~~~
---
 hw/input/tsc2005.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/input/tsc2005.c +1 -0
index 941f163d36..fa93eb5d25 100644
@@ -406,6 +406,7 @@ uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
 static void tsc2005_timer_tick(void *opaque)
 {
     TSC2005State *s = opaque;
+    unsigned func_idx;
 
     /* Timer ticked -- a set of conversions has been finished.  */
 

hw/input/tsc2005.c static void tsc2005_timer_tick(void *opaque) · line 422

Quoted 10 lines
hw/input/tsc2005.c +3 -1
@@ -413,7 +414,9 @@ static void tsc2005_timer_tick(void *opaque)
         return;
 
     s->busy = false;
-    s->dav |= mode_regs[s->function];
+    assert(s->function >= 0);
+    func_idx = s->function;
+    s->dav |= mode_regs[func_idx];
     s->function = -1;
     tsc2005_pin_update(s);
 }

This code is in a deprecated device, but it makes sense to avoid the compiler warning in the meantime. (I'm curious why this only comes up now, since I thought this warning had been around for a while. Maybe gcc 13 is applying it in a wider range of situations.)

If we're going to assert I think we might as well catch array overruns in both directions:

     unsigned int function = s->function;

     assert(function < ARRAY_SIZE(mode_regs);
     s->dav |= mode_regs[function];

thanks
-- PMM