Skip to contentSkip to conversation

Input: tca8418_keypad: hide gcc-4.9 -Wmaybe-uninitialized warning

PATCH In review Arnd Bergmann 2 participants 4 messages Oct 24, 2016 – Oct 26, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: tca8418_keypad: hide gcc-4.9 -Wmaybe-uninitialized warning

Older versions of gcc warn about the tca8418_irq_handler function as they can't keep track of the variable assignment inside of the loop when using the -Wmaybe-unintialized flag:

drivers/input/keyboard/tca8418_keypad.c: In function ‘tca8418_irq_handler’:
drivers/input/keyboard/tca8418_keypad.c:172:9: error: ‘reg’ may be used uninitialized in this function [-Werror=maybe-uninitialized] drivers/input/keyboard/tca8418_keypad.c:165:5: note: ‘reg’ was declared here

This is fixed in gcc-6, but it's possible to rearrange the code in a way that avoids the warning on older compilers as well.

---
 drivers/input/keyboard/tca8418_keypad.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/input/keyboard/tca8418_keypad.c +12 -9
index 9002298698fc..3048ef3e3e16 100644
@@ -164,11 +164,18 @@ static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
 	int error, col, row;
 	u8 reg, state, code;
 
-	/* Initial read of the key event FIFO */
-	error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+	do {
+		error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+		if (error < 0) {
+			dev_err(&keypad_data->client->dev,
+				"unable to read REG_KEY_EVENT_A\n");
+			break;
+		}
+
+		/* Assume that key code 0 signifies empty FIFO */
+		if (reg <= 0)
+			break;
 
-	/* Assume that key code 0 signifies empty FIFO */
-	while (error >= 0 && reg > 0) {
 		state = reg & KEY_EVENT_VALUE;
 		code  = reg & KEY_EVENT_CODE;
 
@@ -184,11 +191,7 @@ static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
 
 		/* Read for next loop */
 		error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
-	}
-
-	if (error < 0)
-		dev_err(&keypad_data->client->dev,
-			"unable to read REG_KEY_EVENT_A\n");
+	} while (1);
 
 	input_sync(input);
 }
Signature · 1 line
2.9.0
maintainer
#1 lore raw

Hi Arnd,

Quoted 27 lines · Arnd Bergmann

Older versions of gcc warn about the tca8418_irq_handler function as they can't keep track of the variable assignment inside of the loop when using the -Wmaybe-unintialized flag:

drivers/input/keyboard/tca8418_keypad.c: In function ‘tca8418_irq_handler’:
drivers/input/keyboard/tca8418_keypad.c:172:9: error: ‘reg’ may be used uninitialized in this function [-Werror=maybe-uninitialized] drivers/input/keyboard/tca8418_keypad.c:165:5: note: ‘reg’ was declared here

This is fixed in gcc-6, but it's possible to rearrange the code in a way that avoids the warning on older compilers as well.

---
 drivers/input/keyboard/tca8418_keypad.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/input/keyboard/tca8418_keypad.c +1 -2
index 9002298698fc..3048ef3e3e16 100644
@@ -164,11 +164,18 @@ static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
 	int error, col, row;
 	u8 reg, state, code;
 
-	/* Initial read of the key event FIFO */
-	error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+	do {

drivers/input/keyboard/tca8418_keypad.c static void tca8418_read_keypad(struct tca8418_keypad *keypad_data) · line 177

Quoted 10 lines
drivers/input/keyboard/tca8418_keypad.c +10 -0
+		error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+		if (error < 0) {
+			dev_err(&keypad_data->client->dev,
+				"unable to read REG_KEY_EVENT_A\n");
+			break;
+		}
+
+		/* Assume that key code 0 signifies empty FIFO */
+		if (reg <= 0)
+			break;

I am unconvinced that this rearrangement fixes the issue for all older GCCs. Can we simply do:

	u8 uninitialized_var(reg);

and be done with it?

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 25 lines · CEST Dmitry Torokhov
Quoted 11 lines
drivers/input/keyboard/tca8418_keypad.c +1 -2
index 9002298698fc..3048ef3e3e16 100644
@@ -164,11 +164,18 @@ static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
      int error, col, row;
      u8 reg, state, code;
 
-     /* Initial read of the key event FIFO */
-     error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+     do {

drivers/input/keyboard/tca8418_keypad.c static void tca8418_read_keypad(struct tca8418_keypad *keypad_data) · line 177

Quoted 10 lines
drivers/input/keyboard/tca8418_keypad.c +10 -0
+             error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+             if (error < 0) {
+                     dev_err(&keypad_data->client->dev,
+                             "unable to read REG_KEY_EVENT_A\n");
+                     break;
+             }
+
+             /* Assume that key code 0 signifies empty FIFO */
+             if (reg <= 0)
+                     break;

I am unconvinced that this rearrangement fixes the issue for all older GCCs. Can we simply do:

Quoted 3 lines
        u8 uninitialized_var(reg);

and be done with it?

Yes, that would work. However:

- avoiding the fake intialization tends to produce better object code, as gcc actually knows what's going on
- Linus doesn't like uninitialized_var() and would rather see it removed from the kernel
- llvm produces warnings for uninitialized_var()

I have checked gcc-4.6/4.7/4.8/4.9/5.x/6.x, and only gcc-4.9 produces the warning. 4.9 changed the detection for uninitialized variables significantly, which generally caused fewer false positives but unfortunately introduced a couple of new ones like this.

Arnd

maintainer
#3 lore raw
Quoted 38 lines · Arnd Bergmann
Quoted 25 lines · CEST Dmitry Torokhov
Quoted 11 lines
drivers/input/keyboard/tca8418_keypad.c +1 -2
index 9002298698fc..3048ef3e3e16 100644
@@ -164,11 +164,18 @@ static void tca8418_read_keypad(struct tca8418_keypad *keypad_data)
      int error, col, row;
      u8 reg, state, code;
 
-     /* Initial read of the key event FIFO */
-     error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+     do {

drivers/input/keyboard/tca8418_keypad.c static void tca8418_read_keypad(struct tca8418_keypad *keypad_data) · line 177

Quoted 10 lines
drivers/input/keyboard/tca8418_keypad.c +10 -0
+             error = tca8418_read_byte(keypad_data, REG_KEY_EVENT_A, &reg);
+             if (error < 0) {
+                     dev_err(&keypad_data->client->dev,
+                             "unable to read REG_KEY_EVENT_A\n");
+                     break;
+             }
+
+             /* Assume that key code 0 signifies empty FIFO */
+             if (reg <= 0)
+                     break;

I am unconvinced that this rearrangement fixes the issue for all older GCCs. Can we simply do:

Quoted 3 lines
        u8 uninitialized_var(reg);

and be done with it?

Yes, that would work. However:

- avoiding the fake intialization tends to produce better object code, as gcc actually knows what's going on
- Linus doesn't like uninitialized_var() and would rather see it removed from the kernel
- llvm produces warnings for uninitialized_var()

Quoted 5 lines

I have checked gcc-4.6/4.7/4.8/4.9/5.x/6.x, and only gcc-4.9 produces the warning. 4.9 changed the detection for uninitialized variables significantly, which generally caused fewer false positives but unfortunately introduced a couple of new ones like this.

OK, I'll apply it then.

Thanks.

Signature · 1 line
Dmitry