mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] tools/thermal: Fix common realloc mistake
@ 2024-11-08  4:47 zhangjiao2
  2024-11-08 12:09 ` Rafael J. Wysocki
  2024-11-14  8:08 ` Daniel Lezcano
  0 siblings, 2 replies; 4+ messages in thread
From: zhangjiao2 @ 2024-11-08  4:47 UTC (permalink / raw)
  To: rafael
  Cc: daniel.lezcano, rui.zhang, lukasz.luba, linux-pm, linux-kernel,
	zhang jiao

From: zhang jiao <zhangjiao2@cmss.chinamobile.com>

Do not set thermometer->tz NULL when realloc failed.

Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
---
 tools/thermal/thermometer/thermometer.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
index 1a87a0a77f9f..e08291a97fd8 100644
--- a/tools/thermal/thermometer/thermometer.c
+++ b/tools/thermal/thermometer/thermometer.c
@@ -259,6 +259,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
 {
 	int fd;
 	char tz_path[PATH_MAX];
+	void *tmp;
 
 	sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
 
@@ -268,12 +269,13 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
 		return -1;
 	}
 
-	thermometer->tz = realloc(thermometer->tz,
+	tmp = realloc(thermometer->tz,
 				  sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
-	if (!thermometer->tz) {
+	if (!tmp) {
 		ERROR("Failed to allocate thermometer->tz\n");
 		return -1;
 	}
+	thermometer->tz = tmp;
 
 	thermometer->tz[thermometer->nr_tz].fd_temp = fd;
 	thermometer->tz[thermometer->nr_tz].name = strdup(name);
-- 
2.33.0




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/thermal: Fix common realloc mistake
  2024-11-08  4:47 [PATCH] tools/thermal: Fix common realloc mistake zhangjiao2
@ 2024-11-08 12:09 ` Rafael J. Wysocki
  2024-11-11  8:19   ` zhangjiao2
  2024-11-14  8:08 ` Daniel Lezcano
  1 sibling, 1 reply; 4+ messages in thread
From: Rafael J. Wysocki @ 2024-11-08 12:09 UTC (permalink / raw)
  To: zhangjiao2
  Cc: rafael, daniel.lezcano, rui.zhang, lukasz.luba, linux-pm, linux-kernel

On Fri, Nov 8, 2024 at 7:39 AM zhangjiao2
<zhangjiao2@cmss.chinamobile.com> wrote:
>
> From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
>
> Do not set thermometer->tz NULL when realloc failed.

Presumably, this fixes a problem.

What problem does it fix?

> Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
> ---
>  tools/thermal/thermometer/thermometer.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
> index 1a87a0a77f9f..e08291a97fd8 100644
> --- a/tools/thermal/thermometer/thermometer.c
> +++ b/tools/thermal/thermometer/thermometer.c
> @@ -259,6 +259,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
>  {
>         int fd;
>         char tz_path[PATH_MAX];
> +       void *tmp;
>
>         sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
>
> @@ -268,12 +269,13 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
>                 return -1;
>         }
>
> -       thermometer->tz = realloc(thermometer->tz,
> +       tmp = realloc(thermometer->tz,
>                                   sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
> -       if (!thermometer->tz) {
> +       if (!tmp) {
>                 ERROR("Failed to allocate thermometer->tz\n");
>                 return -1;
>         }
> +       thermometer->tz = tmp;
>
>         thermometer->tz[thermometer->nr_tz].fd_temp = fd;
>         thermometer->tz[thermometer->nr_tz].name = strdup(name);
> --
> 2.33.0
>
>
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/thermal: Fix common realloc mistake
  2024-11-08 12:09 ` Rafael J. Wysocki
@ 2024-11-11  8:19   ` zhangjiao2
  0 siblings, 0 replies; 4+ messages in thread
From: zhangjiao2 @ 2024-11-11  8:19 UTC (permalink / raw)
  To: rafael
  Cc: daniel.lezcano, linux-kernel, linux-pm, lukasz.luba, rui.zhang,
	zhang jiao

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 1897 bytes --]

From: zhang jiao <zhangjiao2@cmss.chinamobile.com>

On Fri, 8 Nov 2024 13:09 PM Rafael J. Wysocki
<Rafael J. Wysocki> wrote:
>>On Fri, Nov 8, 2024 at 7:39 AM zhangjiao2
>><zhangjiao2@cmss.chinamobile.com> wrote:
>>
>> From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
>>
>> Do not set thermometer->tz NULL when realloc failed.

>Presumably, this fixes a problem.

>What problem does it fix?
Thermometer->tz is NULL when realloc failed.
This will cause memory leaks. 
We should use temporary variables to 
check the return value of realloc.

>> Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
>> ---
>>  tools/thermal/thermometer/thermometer.c | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
>> index 1a87a0a77f9f..e08291a97fd8 100644
>> --- a/tools/thermal/thermometer/thermometer.c
>> +++ b/tools/thermal/thermometer/thermometer.c
>> @@ -259,6 +259,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
>>  {
>>         int fd;
>>         char tz_path[PATH_MAX];
>> +       void *tmp;
>>
>>         sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
>>
>> @@ -268,12 +269,13 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
>>                 return -1;
>>         }
>>
>> -       thermometer->tz = realloc(thermometer->tz,
>> +       tmp = realloc(thermometer->tz,
>>                                   sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
>> -       if (!thermometer->tz) {
>> +       if (!tmp) {
>>                 ERROR("Failed to allocate thermometer->tz\n");
>>                 return -1;
>>         }
>> +       thermometer->tz = tmp;
>>
>>         thermometer->tz[thermometer->nr_tz].fd_temp = fd;
>>         thermometer->tz[thermometer->nr_tz].name = strdup(name);
-- 
2.33.0




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/thermal: Fix common realloc mistake
  2024-11-08  4:47 [PATCH] tools/thermal: Fix common realloc mistake zhangjiao2
  2024-11-08 12:09 ` Rafael J. Wysocki
@ 2024-11-14  8:08 ` Daniel Lezcano
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel Lezcano @ 2024-11-14  8:08 UTC (permalink / raw)
  To: zhangjiao2, rafael; +Cc: rui.zhang, lukasz.luba, linux-pm, linux-kernel


Hi Zhang,

thanks for spotting the issue

On 08/11/2024 05:47, zhangjiao2 wrote:
> From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
> 
> Do not set thermometer->tz NULL when realloc failed.

You may describe a bit more the change.

eg.

If the 'realloc' fails, the thermal zones pointer is set to NULL. This 
makes all thermal zones references which were previously successfully 
initialized to be lost.

> Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
> ---
>   tools/thermal/thermometer/thermometer.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/thermal/thermometer/thermometer.c b/tools/thermal/thermometer/thermometer.c
> index 1a87a0a77f9f..e08291a97fd8 100644
> --- a/tools/thermal/thermometer/thermometer.c
> +++ b/tools/thermal/thermometer/thermometer.c
> @@ -259,6 +259,7 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
>   {
>   	int fd;
>   	char tz_path[PATH_MAX];
> +	void *tmp;

Please use right type:

	struct tz *tz;

>   	sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
>   
> @@ -268,12 +269,13 @@ static int thermometer_add_tz(const char *path, const char *name, int polling,
>   		return -1;
>   	}
>   
> -	thermometer->tz = realloc(thermometer->tz,
> +	tmp = realloc(thermometer->tz,
>   				  sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
> -	if (!thermometer->tz) {
> +	if (!tmp) {
>   		ERROR("Failed to allocate thermometer->tz\n");
>   		return -1;
>   	}
> +	thermometer->tz = tmp;
>   
>   	thermometer->tz[thermometer->nr_tz].fd_temp = fd;
>   	thermometer->tz[thermometer->nr_tz].name = strdup(name);


-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-11-14  8:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-08  4:47 [PATCH] tools/thermal: Fix common realloc mistake zhangjiao2
2024-11-08 12:09 ` Rafael J. Wysocki
2024-11-11  8:19   ` zhangjiao2
2024-11-14  8:08 ` Daniel Lezcano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®