Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753120AbdGGAUM (ORCPT ); Thu, 6 Jul 2017 20:20:12 -0400 Received: from server.atrad.com.au ([150.101.241.2]:35914 "EHLO server.atrad.com.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753012AbdGGAUL (ORCPT ); Thu, 6 Jul 2017 20:20:11 -0400 Date: Fri, 7 Jul 2017 09:49:00 +0930 From: Jonathan Woithe To: "Gustavo A. R. Silva" Cc: Darren Hart , Andy Shevchenko , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] platform/x86: fujitsu-laptop: add NULL check on devm_kzalloc() return value Message-ID: <20170707001900.GE6573@marvin.atrad.com.au> References: <20170706221902.GA29514@embeddedgus> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170706221902.GA29514@embeddedgus> User-Agent: Mutt/1.6.1 (2016-04-27) X-MIMEDefang-action: accept Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2692 Lines: 73 On Thu, Jul 06, 2017 at 05:19:02PM -0500, Gustavo A. R. Silva wrote: > Check return value from call to devm_kzalloc() > in order to prevent a NULL pointer dereference. > > This issue was detected using Coccinelle and the following semantic patch: > > @@ > expression x; > identifier fld; > @@ > > * x = devm_kzalloc(...); > ... when != x == NULL > x->fld > > Signed-off-by: Gustavo A. R. Silva These checks should be added in the interest of code correctness. devm_kzalloc() can fail (even if it's extremely unlikely in practice) so we should check for this. Reviewed-by: Jonathan Woithe > --- > drivers/platform/x86/fujitsu-laptop.c | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/drivers/platform/x86/fujitsu-laptop.c b/drivers/platform/x86/fujitsu-laptop.c > index c1a8528..593a350 100644 > --- a/drivers/platform/x86/fujitsu-laptop.c > +++ b/drivers/platform/x86/fujitsu-laptop.c > @@ -695,6 +695,9 @@ static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device) > if (call_fext_func(device, > FUNC_LEDS, 0x0, 0x0, 0x0) & LOGOLAMP_POWERON) { > led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL); > + if (!led) > + return -ENOMEM; > + > led->name = "fujitsu::logolamp"; > led->brightness_set_blocking = logolamp_set; > led->brightness_get = logolamp_get; > @@ -707,6 +710,9 @@ static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device) > FUNC_LEDS, 0x0, 0x0, 0x0) & KEYBOARD_LAMPS) && > (call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0) == 0x0)) { > led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL); > + if (!led) > + return -ENOMEM; > + > led->name = "fujitsu::kblamps"; > led->brightness_set_blocking = kblamps_set; > led->brightness_get = kblamps_get; > @@ -723,6 +729,9 @@ static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device) > */ > if (call_fext_func(device, FUNC_BUTTONS, 0x0, 0x0, 0x0) & BIT(24)) { > led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL); > + if (!led) > + return -ENOMEM; > + > led->name = "fujitsu::radio_led"; > led->brightness_set_blocking = radio_led_set; > led->brightness_get = radio_led_get; > @@ -741,6 +750,9 @@ static int acpi_fujitsu_laptop_leds_register(struct acpi_device *device) > (call_fext_func(device, > FUNC_LEDS, 0x2, ECO_LED, 0x0) != UNSUPPORTED_CMD)) { > led = devm_kzalloc(&device->dev, sizeof(*led), GFP_KERNEL); > + if (!led) > + return -ENOMEM; > + > led->name = "fujitsu::eco_led"; > led->brightness_set_blocking = eco_led_set; > led->brightness_get = eco_led_get; > -- > 2.5.0