mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute()
@ 2013-03-14 13:30 David Woodhouse
  2013-03-14 13:59 ` Guenter Roeck
  2013-03-14 16:18 ` [PATCH] checkpatch: Add check for reuse of krealloc arg Joe Perches
  0 siblings, 2 replies; 4+ messages in thread
From: David Woodhouse @ 2013-03-14 13:30 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Jean Delvare, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1488 bytes --]

If krealloc() returns NULL, it *doesn't* free the original. So any code
of the form 'foo = krealloc(foo, …);' is almost certainly a bug.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
---
 drivers/hwmon/pmbus/pmbus_core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index 80eef50..9add6092 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -766,12 +766,14 @@ static ssize_t pmbus_show_label(struct device *dev,
 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
 {
 	if (data->num_attributes >= data->max_attributes - 1) {
-		data->max_attributes += PMBUS_ATTR_ALLOC_SIZE;
-		data->group.attrs = krealloc(data->group.attrs,
-					     sizeof(struct attribute *) *
-					     data->max_attributes, GFP_KERNEL);
-		if (data->group.attrs == NULL)
+		int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
+		void *new_attrs = krealloc(data->group.attrs,
+					   new_max_attrs * sizeof(void *),
+					   GFP_KERNEL);
+		if (!new_attrs)
 			return -ENOMEM;
+		data->group.attrs = new_attrs;
+		data->max_attributes = new_max_attrs;
 	}
 
 	data->group.attrs[data->num_attributes++] = attr;
-- 
1.8.1.4


-- 
David Woodhouse                            Open Source Technology Centre
David.Woodhouse@intel.com                              Intel Corporation

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 6171 bytes --]

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

* Re: [PATCH] hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute()
  2013-03-14 13:30 [PATCH] hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute() David Woodhouse
@ 2013-03-14 13:59 ` Guenter Roeck
  2013-03-14 16:18 ` [PATCH] checkpatch: Add check for reuse of krealloc arg Joe Perches
  1 sibling, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2013-03-14 13:59 UTC (permalink / raw)
  To: David Woodhouse; +Cc: Jean Delvare, linux-kernel

On Thu, Mar 14, 2013 at 01:30:20PM +0000, David Woodhouse wrote:
> If krealloc() returns NULL, it *doesn't* free the original. So any code
> of the form 'foo = krealloc(foo, …);' is almost certainly a bug.
> 
> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>

Good catch. Thanks a lot for the fix!

Guenter

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

* [PATCH] checkpatch: Add check for reuse of krealloc arg
  2013-03-14 13:30 [PATCH] hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute() David Woodhouse
  2013-03-14 13:59 ` Guenter Roeck
@ 2013-03-14 16:18 ` Joe Perches
  2013-03-14 16:39   ` Guenter Roeck
  1 sibling, 1 reply; 4+ messages in thread
From: Joe Perches @ 2013-03-14 16:18 UTC (permalink / raw)
  To: David Woodhouse, Andrew Morton, Andy Whitcroft
  Cc: Guenter Roeck, Jean Delvare, linux-kernel

On Thu, 2013-03-14 at 13:30 +0000, David Woodhouse wrote:
> If krealloc() returns NULL, it *doesn't* free the original. So any code
> of the form 'foo = krealloc(foo, …);' is almost certainly a bug.

So add a check for it to checkpatch.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b28cc38..7ef2b4b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3476,6 +3476,13 @@ sub process {
 			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
 		}
 
+# check for krealloc arg reuse
+		if ($^V && $^V ge 5.10.0 &&
+		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
+			WARN("KREALLOC_ARG_REUSE",
+			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
+		}
+
 # check for alloc argument mismatch
 		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
 			WARN("ALLOC_ARRAY_ARGS",



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

* Re: [PATCH] checkpatch: Add check for reuse of krealloc arg
  2013-03-14 16:18 ` [PATCH] checkpatch: Add check for reuse of krealloc arg Joe Perches
@ 2013-03-14 16:39   ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2013-03-14 16:39 UTC (permalink / raw)
  To: Joe Perches
  Cc: David Woodhouse, Andrew Morton, Andy Whitcroft, Jean Delvare,
	linux-kernel

On Thu, Mar 14, 2013 at 09:18:05AM -0700, Joe Perches wrote:
> On Thu, 2013-03-14 at 13:30 +0000, David Woodhouse wrote:
> > If krealloc() returns NULL, it *doesn't* free the original. So any code
> > of the form 'foo = krealloc(foo, …);' is almost certainly a bug.
> 
> So add a check for it to checkpatch.
> 
> Signed-off-by: Joe Perches <joe@perches.com>

Excellent idea.

Tested-by: Guenter Roeck <linux@roeck-us.net>
Acked-by: Guenter Roeck <linux@roeck-us.net>

> ---
>  scripts/checkpatch.pl | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index b28cc38..7ef2b4b 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -3476,6 +3476,13 @@ sub process {
>  			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
>  		}
>  
> +# check for krealloc arg reuse
> +		if ($^V && $^V ge 5.10.0 &&
> +		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
> +			WARN("KREALLOC_ARG_REUSE",
> +			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
> +		}
> +
>  # check for alloc argument mismatch
>  		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
>  			WARN("ALLOC_ARRAY_ARGS",
> 
> 
> 

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

end of thread, other threads:[~2013-03-14 16:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-14 13:30 [PATCH] hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute() David Woodhouse
2013-03-14 13:59 ` Guenter Roeck
2013-03-14 16:18 ` [PATCH] checkpatch: Add check for reuse of krealloc arg Joe Perches
2013-03-14 16:39   ` Guenter Roeck

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®