Skip to contentSkip to conversation

counter: add defaults to switch-statements

PATCH In review [email protected] 3 participants 4 messages Feb 27, 2022 – Feb 28, 2022

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

PATCH 1/1 counter: add defaults to switch-statements

From: Tom Rix <[email protected]>

Clang static analysis reports this representative problem counter-chrdev.c:482:3: warning: Undefined or garbage value
returned to caller
return ret;
^~~~~~~~~~

counter_get_data() has a multilevel switches, some without defaults, so ret is sometimes not set.
Add returning -EINVAL similar to other defaults.

Fixes b6c50affda59 ("counter: Add character device interface")
---
 drivers/counter/counter-chrdev.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/counter/counter-chrdev.c +4 -0
index b7c62f957a6a8..69d340be9c93f 100644
@@ -477,6 +477,8 @@ static int counter_get_data(struct counter_device *const counter,
 		case COUNTER_SCOPE_COUNT:
 			ret = comp->count_u8_read(counter, parent, &value_u8);
 			break;
+		default:
+			return -EINVAL;
 		}
 		*value = value_u8;
 		return ret;
@@ -496,6 +498,8 @@ static int counter_get_data(struct counter_device *const counter,
 		case COUNTER_SCOPE_COUNT:
 			ret = comp->count_u32_read(counter, parent, &value_u32);
 			break;
+		default:
+			return -EINVAL;
 		}
 		*value = value_u32;
 		return ret;
Signature · 1 line
2.26.3
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · On Sun, 27 Feb 2022 08:17:46 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this representative problem counter-chrdev.c:482:3: warning: Undefined or garbage value
returned to caller
return ret;
^~~~~~~~~~

Quoted 6 lines

counter_get_data() has a multilevel switches, some without defaults, so ret is sometimes not set.
Add returning -EINVAL similar to other defaults.

Fixes b6c50affda59 ("counter: Add character device interface")

I'm fairly sure this one is warning supression rather than a fix as that type u8 being used in the switch is storing an enum which is set only by kernel code and all values of the enum are covered.

However, that's not locally visible so to me the addition looks good, I'd just be tempted to drop the fixes tag as backporting this looks like noise to me.

Quoted 26 lines
---
 drivers/counter/counter-chrdev.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/counter/counter-chrdev.c +4 -0
index b7c62f957a6a8..69d340be9c93f 100644
@@ -477,6 +477,8 @@ static int counter_get_data(struct counter_device *const counter,
 		case COUNTER_SCOPE_COUNT:
 			ret = comp->count_u8_read(counter, parent, &value_u8);
 			break;
+		default:
+			return -EINVAL;
 		}
 		*value = value_u8;
 		return ret;
@@ -496,6 +498,8 @@ static int counter_get_data(struct counter_device *const counter,
 		case COUNTER_SCOPE_COUNT:
 			ret = comp->count_u32_read(counter, parent, &value_u32);
 			break;
+		default:
+			return -EINVAL;
 		}
 		*value = value_u32;
 		return ret;
#2 lore raw
Quoted 21 lines · Jonathan Cameron
Quoted 8 lines · On Sun, 27 Feb 2022 08:17:46 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this representative problem counter-chrdev.c:482:3: warning: Undefined or garbage value
returned to caller
return ret;
^~~~~~~~~~

Quoted 6 lines

counter_get_data() has a multilevel switches, some without defaults, so ret is sometimes not set.
Add returning -EINVAL similar to other defaults.

Fixes b6c50affda59 ("counter: Add character device interface")

I'm fairly sure this one is warning supression rather than a fix as that type u8 being used in the switch is storing an enum which is set only by kernel code and all values of the enum are covered.

Quoted 5 lines

However, that's not locally visible so to me the addition looks good, I'd just be tempted to drop the fixes tag as backporting this looks like noise to me.

Yes, this isn't really a bug because the only values available are those from the enum, but I see how this is not immediately obvious at first. If Tom has no objections, I'll pick this up and drop the fixes tag so that we have it along with the other counter changes for the 5.18 merge.

Thanks,

William Breathitt Gray

Quoted 27 lines
Quoted 26 lines
---
 drivers/counter/counter-chrdev.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/counter/counter-chrdev.c +4 -0
index b7c62f957a6a8..69d340be9c93f 100644
@@ -477,6 +477,8 @@ static int counter_get_data(struct counter_device *const counter,
 		case COUNTER_SCOPE_COUNT:
 			ret = comp->count_u8_read(counter, parent, &value_u8);
 			break;
+		default:
+			return -EINVAL;
 		}
 		*value = value_u8;
 		return ret;
@@ -496,6 +498,8 @@ static int counter_get_data(struct counter_device *const counter,
 		case COUNTER_SCOPE_COUNT:
 			ret = comp->count_u32_read(counter, parent, &value_u32);
 			break;
+		default:
+			return -EINVAL;
 		}
 		*value = value_u32;
 		return ret;
author
#3 lore raw
Quoted 26 lines · William Breathitt Gray
Quoted 25 lines · Jonathan Cameron
Quoted 8 lines · On Sun, 27 Feb 2022 08:17:46 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this representative problem counter-chrdev.c:482:3: warning: Undefined or garbage value
returned to caller

   return ret;
   ^~~~~~~~~~
Quoted 6 lines

counter_get_data() has a multilevel switches, some without defaults, so ret is sometimes not set.
Add returning -EINVAL similar to other defaults.

Fixes b6c50affda59 ("counter: Add character device interface")

I'm fairly sure this one is warning supression rather than a fix as that type u8 being used in the switch is storing an enum which is set only by kernel code and all values of the enum are covered.

However, that's not locally visible so to me the addition looks good, I'd just be tempted to drop the fixes tag as backporting this looks like noise to me.

Quoted 5 lines
Quoted 1 line

Yes, this isn't really a bug because the only values available are those from the enum, but I see how this is not immediately obvious at first. If Tom has no objections, I'll pick this up and drop the fixes tag so that we have it along with the other counter changes for the 5.18 merge.

Sounds good.

Thanks,

Tom

Quoted 30 lines

Thanks,

William Breathitt Gray

Quoted 26 lines
Quoted 26 lines
---
  drivers/counter/counter-chrdev.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/counter/counter-chrdev.c +4 -0
index b7c62f957a6a8..69d340be9c93f 100644
@@ -477,6 +477,8 @@ static int counter_get_data(struct counter_device *const counter,
  		case COUNTER_SCOPE_COUNT:
  			ret = comp->count_u8_read(counter, parent, &value_u8);
  			break;
+		default:
+			return -EINVAL;
  		}
  		*value = value_u8;
  		return ret;
@@ -496,6 +498,8 @@ static int counter_get_data(struct counter_device *const counter,
  		case COUNTER_SCOPE_COUNT:
  			ret = comp->count_u32_read(counter, parent, &value_u32);
  			break;
+		default:
+			return -EINVAL;
  		}
  		*value = value_u32;
  		return ret;