Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp250041lqt; Thu, 18 Apr 2024 14:03:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX+G6INMlJJfCnFWMK2iNnR/oxsc9JNs42DFYBqq9KwphwaBziOyRU3XNvWzF4lkE8HjQrpt3awvV8mt0v6SlY984zqukQtMZqh+U69cA== X-Google-Smtp-Source: AGHT+IGwzyxAZ6PjKlFHb4WLuHmFBE77LxxXd1Udcd8AvxkBsdGoPhMChGEyHPlzcqp/lJcKpsKm X-Received: by 2002:a17:906:f9d6:b0:a51:b00b:45a5 with SMTP id lj22-20020a170906f9d600b00a51b00b45a5mr127811ejb.74.1713474199440; Thu, 18 Apr 2024 14:03:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713474199; cv=pass; d=google.com; s=arc-20160816; b=nhFOz+UgyINd2rSX9xvuDTnuW7WHSkq4fOJkhuFb7A8eqpkAxb7iFLirc4/t6ZFuP4 vdqY6yfSRupEafuXfDMcuukMjbY0nx9A8gKPP6RlVti7+PVJtIWLEVVvCveK+WoFPL8w K2YWdKBp9cDK5QaA8PUSIVYZJ5s73m/H+A1OGGVNphiMgZxGh3mJMn+mGXARMN90rQ1Z nEyajFT5qlyNydoO0ivwtA1DitC7uBSl6AiJDc3V1qRgRU722ts/0nQOepFx4zH7nwrM AlOfgT8PnpXo5TZuBtvptouUDGH18Mm2EiIYE6E2OVXrHkaaEFLxswdJL3QgvAjJQcFd db5A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=KGwLf/zGdjbS5m+0XqbUt5N2AjYYBbBkI6NTUeaiDj4=; fh=cJInt5iD9ejxL1bgmVA9M0G2yZlYViSKQeKcNpLm47c=; b=YgV/dlagudxs6qP8lCeRd02R33PIVjkGb/rKwA1UsZg9sHbnE/djG0BXEHs4Egj9tC 6i49eKEMdzHSZ/D1Mb/QOhYtsZMYlYfrQoaShx9hjIftnIOlQ/ohbXdFaXnGr6sHuTKb qkGgsHByqmEDulyQDhiED0IHMkgJcQXqRUZNOQVk1wYuHwONszUEfkExXIWKnf9CPJGS ZVL5UsR/IybDLlBqAXCGNrnQ29IJTNZLP5HlTbhld52PwjyeN3r3rDI9p0DuSkaufcyw gTkkzDjhvXCuc35qBacenCWY/jgV6nur4FCivDWVa05oybqPBfFiI2tA8XZK6JpQzN2u Rxlg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bd3YOWj3; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-150785-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150785-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g11-20020a170906198b00b00a53f130377bsi1299221ejd.541.2024.04.18.14.03.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 14:03:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-150785-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bd3YOWj3; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-150785-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150785-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 312AE1F212A7 for ; Thu, 18 Apr 2024 21:03:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A6887199E9A; Thu, 18 Apr 2024 21:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bd3YOWj3" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B653199E88 for ; Thu, 18 Apr 2024 21:02:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713474174; cv=none; b=UBouN2iZr3rV+cAGoYtCIchvMWHFfDb21B5nVDj3iRtn+CT/fTp5Euoa2MWeDb9ifqiebNBfGnUisHzR0RYqYIzr1dya1i+SwXaiIa1+7xkY5QmaToqxGpX7kGj1MyJUK41Yu26GUi6X2u8kxDfjzlwkcWiDiOLzMy8ja3KKOAo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713474174; c=relaxed/simple; bh=k7cNZQtqSrlv4JeV2BTf4vB+rduDV353rBwxNmUmz4A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fRG0Fdt6J5uO8CaKlA3knTNeQUZ38C6ByuI0WgpnNPuq/a0PM49tyQrXoFrP8LSBGW8EXNUMb3RUCJ6/VOAcG6UzmTb8eop0PxU9kyXN00In0CzzcrR2eMk8XJCkYkxbdoI3DFcZPZMWUdiZqaAqnFOBkBC1nXEdK8MWmfFXKbw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bd3YOWj3; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713474172; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KGwLf/zGdjbS5m+0XqbUt5N2AjYYBbBkI6NTUeaiDj4=; b=bd3YOWj3kTopdE05oYsLh3r7KTzSBAI73nf0Vjp9D7H5CRsHdkd8V9GzcVhSwDS1RpmO4o tQbcHmpFMQavuDz89Nwm9dVtdDdScKXsFyUyQWlN+6zqgvjV9qq82TOCgz2B5yfTdbA/4m 8KCUhPtD4cliR/COVOvQpG4Il5OeC/4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-tjLOU_fXNCGZ8R2giiE72Q-1; Thu, 18 Apr 2024 17:02:47 -0400 X-MC-Unique: tjLOU_fXNCGZ8R2giiE72Q-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 429D918065AA; Thu, 18 Apr 2024 21:02:47 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.16.186]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4C1140BAA2; Thu, 18 Apr 2024 21:02:44 +0000 (UTC) From: Wander Lairson Costa To: Brendan Higgins , David Gow , Rae Moar , Greg Kroah-Hartman , Shuah Khan , Maxime Ripard , Matti Vaittinen , linux-kselftest@vger.kernel.org (open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)), kunit-dev@googlegroups.com (open list:KERNEL UNIT TESTING FRAMEWORK (KUnit)), linux-kernel@vger.kernel.org (open list) Cc: Wander Lairson Costa Subject: [PATCH v3 2/2] kunit: avoid memory leak on device register error Date: Thu, 18 Apr 2024 18:02:35 -0300 Message-ID: <20240418210236.194190-3-wander@redhat.com> In-Reply-To: <20240418210236.194190-1-wander@redhat.com> References: <20240418210236.194190-1-wander@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 If the device register fails, free the allocated memory before returning. Signed-off-by: Wander Lairson Costa Fixes: d03c720e03bd ("kunit: Add APIs for managing devices") --- lib/kunit/device.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/kunit/device.c b/lib/kunit/device.c index 25c81ed465fb..bc2e2032e505 100644 --- a/lib/kunit/device.c +++ b/lib/kunit/device.c @@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test, kunit_dev->owner = test; err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name); - if (err) { - kfree(kunit_dev); - return ERR_PTR(err); - } + if (err) + goto error; kunit_dev->dev.release = kunit_device_release; kunit_dev->dev.bus = &kunit_bus_type; @@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test, err = device_register(&kunit_dev->dev); if (err) { put_device(&kunit_dev->dev); - return ERR_PTR(err); + goto error; } kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask; @@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test, kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev); return kunit_dev; +error: + kfree(kunit_dev); + return ERR_PTR(err); } /* -- 2.44.0