hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute()
Subsystems (2)
PATCH 1/1 hwmon: (pmbus) Fix krealloc() misuse in pmbus_add_attribute()
If krealloc() returns NULL, it *doesn't* free the original. So any code of the form 'foo = krealloc(foo, …);' is almost certainly a bug.
--- drivers/hwmon/pmbus/pmbus_core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +7 -5
@@ -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;
Signature · 6 lines
1.8.1.4 -- David Woodhouse Open Source Technology Centre [email protected] Intel Corporation
Guenter Roeck
maintainerQuoted 4 lines · David Woodhouse
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 <[email protected]>
Good catch. Thanks a lot for the fix!
Guenter
Joe Perches
maintainerPATCH 1/1 checkpatch: Add check for reuse of krealloc arg
In reply to David WoodhouseQuoted 2 lines · David Woodhouse
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.
--- scripts/checkpatch.pl | 7 +++++++ 1 file changed, 7 insertions(+)
scripts/checkpatch.pl +7 -0
@@ -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",
Re: [PATCH] checkpatch: Add check for reuse of krealloc arg
Quoted 7 lines · Joe Perches
Quoted 2 lines · David Woodhouse
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 <[email protected]>
Excellent idea.
Quoted 25 lines
--- scripts/checkpatch.pl | 7 +++++++ 1 file changed, 7 insertions(+)scripts/checkpatch.pl +7
-0@@ -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",