2021-12-09 04:39:20

by Kees Cook

[permalink] [raw]
Subject: [PATCH] rtc: Move variable into switch case statement

When building with automatic stack variable initialization, GCC 12
complains about variables defined outside of switch case statements.
Move the variable into the case that uses it, which silences the warning:

drivers/rtc/dev.c: In function 'rtc_dev_ioctl':
drivers/rtc/dev.c:394:30: warning: statement will never be executed [-Wswitch-unreachable]
394 | long offset;
| ^~~~~~

Fixes: 6a8af1b6568a ("rtc: add parameter ioctl")
Signed-off-by: Kees Cook <[email protected]>
---
drivers/rtc/dev.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/rtc/dev.c b/drivers/rtc/dev.c
index e104972a28fd..69325aeede1a 100644
--- a/drivers/rtc/dev.c
+++ b/drivers/rtc/dev.c
@@ -391,14 +391,14 @@ static long rtc_dev_ioctl(struct file *file,
}

switch(param.param) {
- long offset;
case RTC_PARAM_FEATURES:
if (param.index != 0)
err = -EINVAL;
param.uvalue = rtc->features[0];
break;

- case RTC_PARAM_CORRECTION:
+ case RTC_PARAM_CORRECTION: {
+ long offset;
mutex_unlock(&rtc->ops_lock);
if (param.index != 0)
return -EINVAL;
@@ -407,7 +407,7 @@ static long rtc_dev_ioctl(struct file *file,
if (err == 0)
param.svalue = offset;
break;
-
+ }
default:
if (rtc->ops->param_get)
err = rtc->ops->param_get(rtc->dev.parent, &param);
--
2.30.2



2021-12-14 00:41:47

by Gustavo A. R. Silva

[permalink] [raw]
Subject: Re: [PATCH] rtc: Move variable into switch case statement

On Wed, Dec 08, 2021 at 08:39:15PM -0800, Kees Cook wrote:
> When building with automatic stack variable initialization, GCC 12
> complains about variables defined outside of switch case statements.
> Move the variable into the case that uses it, which silences the warning:
>
> drivers/rtc/dev.c: In function 'rtc_dev_ioctl':
> drivers/rtc/dev.c:394:30: warning: statement will never be executed [-Wswitch-unreachable]
> 394 | long offset;
> | ^~~~~~
>
> Fixes: 6a8af1b6568a ("rtc: add parameter ioctl")
> Signed-off-by: Kees Cook <[email protected]>

Reviewed-by: Gustavo A. R. Silva <[email protected]>

Thanks
--
Gustavo

> ---
> drivers/rtc/dev.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/rtc/dev.c b/drivers/rtc/dev.c
> index e104972a28fd..69325aeede1a 100644
> --- a/drivers/rtc/dev.c
> +++ b/drivers/rtc/dev.c
> @@ -391,14 +391,14 @@ static long rtc_dev_ioctl(struct file *file,
> }
>
> switch(param.param) {
> - long offset;
> case RTC_PARAM_FEATURES:
> if (param.index != 0)
> err = -EINVAL;
> param.uvalue = rtc->features[0];
> break;
>
> - case RTC_PARAM_CORRECTION:
> + case RTC_PARAM_CORRECTION: {
> + long offset;
> mutex_unlock(&rtc->ops_lock);
> if (param.index != 0)
> return -EINVAL;
> @@ -407,7 +407,7 @@ static long rtc_dev_ioctl(struct file *file,
> if (err == 0)
> param.svalue = offset;
> break;
> -
> + }
> default:
> if (rtc->ops->param_get)
> err = rtc->ops->param_get(rtc->dev.parent, &param);
> --
> 2.30.2
>
>
>
>

2022-01-05 00:05:51

by Alexandre Belloni

[permalink] [raw]
Subject: Re: [PATCH] rtc: Move variable into switch case statement

On Wed, 8 Dec 2021 20:39:15 -0800, Kees Cook wrote:
> When building with automatic stack variable initialization, GCC 12
> complains about variables defined outside of switch case statements.
> Move the variable into the case that uses it, which silences the warning:
>
> drivers/rtc/dev.c: In function 'rtc_dev_ioctl':
> drivers/rtc/dev.c:394:30: warning: statement will never be executed [-Wswitch-unreachable]
> 394 | long offset;
> | ^~~~~~
>
> [...]

Applied, thanks!

[1/1] rtc: Move variable into switch case statement
commit: ba52eac083e1598e748811ff58d259f77e4c5c4d

Best regards,
--
Alexandre Belloni <[email protected]>