diff mbox

[15/21] thermal: cooling: avoid uninitialied used gcc warning

Message ID 1366910944-3033663-16-git-send-email-arnd@arndb.de (mailing list archive)
State New, archived
Headers show

Commit Message

Arnd Bergmann April 25, 2013, 5:28 p.m. UTC
The newly rewritten get_property() function causes a bogus warning
from gcc-3.8, which cannot figure out that "level" is always
initialized at the point where it gets evaluated:

drivers/thermal/cpu_cooling.c: In function 'get_property':
drivers/thermal/cpu_cooling.c:189:37: warning: 'level' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (property == GET_FREQ && level == j) {
                                     ^

Slightly rearranging the code makes this more obvious and
avoids the warning.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Amit Daniel kachhap <amit.daniel@samsung.com>
---
 drivers/thermal/cpu_cooling.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

Eduardo Valentin April 25, 2013, 8:07 p.m. UTC | #1
Arnd,

On Thu, Apr 25, 2013 at 1:28 PM, Arnd Bergmann <arnd@arndb.de> wrote:
> The newly rewritten get_property() function causes a bogus warning
> from gcc-3.8, which cannot figure out that "level" is always
> initialized at the point where it gets evaluated:
>
> drivers/thermal/cpu_cooling.c: In function 'get_property':
> drivers/thermal/cpu_cooling.c:189:37: warning: 'level' may be used uninitialized in this function [-Wmaybe-uninitialized]
>    if (property == GET_FREQ && level == j) {
>                                      ^
>
> Slightly rearranging the code makes this more obvious and
> avoids the warning.
>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> Cc: Zhang Rui <rui.zhang@intel.com>
> Cc: Amit Daniel kachhap <amit.daniel@samsung.com>

Rui has merged a patch that removes this bogus compiler warning:
http://git.kernel.org/cgit/linux/kernel/git/rzhang/linux.git/commit/?h=next&id=4469b99743d296e24aefc5f8ed7df1bc9cfbbac8

Though, as not as elegant as your patch, it does the trick. :-)

> ---
>  drivers/thermal/cpu_cooling.c | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> index 768b508..34878e6 100644
> --- a/drivers/thermal/cpu_cooling.c
> +++ b/drivers/thermal/cpu_cooling.c
> @@ -165,10 +165,6 @@ static int get_property(unsigned int cpu, unsigned long input,
>                 return 0;
>         }
>
> -       if (property == GET_FREQ)
> -               level = descend ? input : (max_level - input -1);
> -
> -
>         for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
>                 /* ignore invalid entry */
>                 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> @@ -186,10 +182,15 @@ static int get_property(unsigned int cpu, unsigned long input,
>                         *output = descend ? j : (max_level - j - 1);
>                         return 0;
>                 }
> -               if (property == GET_FREQ && level == j) {
> -                       /* get frequency by level */
> -                       *output = freq;
> -                       return 0;
> +
> +               if (property == GET_FREQ) {
> +                       level = descend ? input : (max_level - input -1);
> +
> +                       if (level == j) {
> +                               /* get frequency by level */
> +                               *output = freq;
> +                               return 0;
> +                       }
>                 }
>                 j++;
>         }
> --
> 1.8.1.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
Arnd Bergmann April 25, 2013, 9:09 p.m. UTC | #2
On Thursday 25 April 2013, edubezval@gmail.com wrote:
> Rui has merged a patch that removes this bogus compiler warning:
> http://git.kernel.org/cgit/linux/kernel/git/rzhang/linux.git/commit/?h=next&amp;id=4469b99743d296e24aefc5f8ed7df1bc9cfbbac8
> 
> Though, as not as elegant as your patch, it does the trick. :-)

Ok, works for me.

Looking at the patch however tells me that it has the potential to hide real
bugs if the code is ever changed to actually do an uninitialized access.
It's not very likely in this case, but I generally recommend not to add
any variables at declaration time unless the initialization is to a
meaningful value that the code later uses.

	Arnd
Zhang, Rui April 26, 2013, 8:10 a.m. UTC | #3
On Thu, 2013-04-25 at 19:28 +0200, Arnd Bergmann wrote:
> The newly rewritten get_property() function causes a bogus warning
> from gcc-3.8, which cannot figure out that "level" is always
> initialized at the point where it gets evaluated:
> 
> drivers/thermal/cpu_cooling.c: In function 'get_property':
> drivers/thermal/cpu_cooling.c:189:37: warning: 'level' may be used uninitialized in this function [-Wmaybe-uninitialized]
>    if (property == GET_FREQ && level == j) {
>                                      ^
> 
> Slightly rearranging the code makes this more obvious and
> avoids the warning.
> 
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> Cc: Zhang Rui <rui.zhang@intel.com>
> Cc: Amit Daniel kachhap <amit.daniel@samsung.com>

we already have a fix for this.
please refer to
https://patchwork.kernel.org/patch/2454891/

thanks for your patch, Arnd!

-rui
> ---
>  drivers/thermal/cpu_cooling.c | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> index 768b508..34878e6 100644
> --- a/drivers/thermal/cpu_cooling.c
> +++ b/drivers/thermal/cpu_cooling.c
> @@ -165,10 +165,6 @@ static int get_property(unsigned int cpu, unsigned long input,
>  		return 0;
>  	}
>  
> -	if (property == GET_FREQ)
> -		level = descend ? input : (max_level - input -1);
> -
> -
>  	for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
>  		/* ignore invalid entry */
>  		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
> @@ -186,10 +182,15 @@ static int get_property(unsigned int cpu, unsigned long input,
>  			*output = descend ? j : (max_level - j - 1);
>  			return 0;
>  		}
> -		if (property == GET_FREQ && level == j) {
> -			/* get frequency by level */
> -			*output = freq;
> -			return 0;
> +
> +		if (property == GET_FREQ) {
> +			level = descend ? input : (max_level - input -1);
> +
> +			if (level == j) {
> +				/* get frequency by level */
> +				*output = freq;
> +				return 0;
> +			}
>  		}
>  		j++;
>  	}
diff mbox

Patch

diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
index 768b508..34878e6 100644
--- a/drivers/thermal/cpu_cooling.c
+++ b/drivers/thermal/cpu_cooling.c
@@ -165,10 +165,6 @@  static int get_property(unsigned int cpu, unsigned long input,
 		return 0;
 	}
 
-	if (property == GET_FREQ)
-		level = descend ? input : (max_level - input -1);
-
-
 	for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
 		/* ignore invalid entry */
 		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
@@ -186,10 +182,15 @@  static int get_property(unsigned int cpu, unsigned long input,
 			*output = descend ? j : (max_level - j - 1);
 			return 0;
 		}
-		if (property == GET_FREQ && level == j) {
-			/* get frequency by level */
-			*output = freq;
-			return 0;
+
+		if (property == GET_FREQ) {
+			level = descend ? input : (max_level - input -1);
+
+			if (level == j) {
+				/* get frequency by level */
+				*output = freq;
+				return 0;
+			}
 		}
 		j++;
 	}