Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3849532rdh; Fri, 29 Sep 2023 04:28:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGhOY4SNtGLK3T7zkg0Pty4r8IoPBfE2NzNb9zObV1LcpgXuh9GTBl5NUkpp/zVqXleDixh X-Received: by 2002:a05:6808:83:b0:3a9:6400:62c6 with SMTP id s3-20020a056808008300b003a9640062c6mr4067382oic.32.1695986901943; Fri, 29 Sep 2023 04:28:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695986901; cv=none; d=google.com; s=arc-20160816; b=KyxXEZ3EBXUOjNJXRi93+SBBgPlAYTYRc654U7n8YOLHGVYHSKj0TIN5G6y//EJFUa A2r2jRfTkfbo6ZpSGEQyytS/W5HpkBAPrYkpdGpbToI2e0Nym0+eMeo7H0idV+8KRMoZ tO/5za5uP4FBZ9OvDHzY7scTMiMWmkmNtqmkMws67Q8PuJ6mmUmgFA5qpDt5Nt1C+XPm 47VfHNwoVZaAiFnvtQ8ncO3ya4UsoWPc/YxBPw9cuihdpVYrpNeXeLhokQqwhoXNQ140 QdtEwBmcRveH+HP59R7ZwZxpu8bWUV/Crhl1RbuRJXYyhPDSerOrjF71v5jwNDN8vFyj Cizg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=66YwtrVPB63m73rzfrkXNgTtu4wVWf9hd6q5sRDERfc=; fh=rfWZ5MhfUZw8dgrvo+Mn0E8mx5tu5AITXK4Ouig35hs=; b=oywq4JpMfobcQYXvPrU0GRG7kA5qNsICk6KFlR9gZdg6oinWtLKNPc1wcdTnx74SMp btp91DUmofmNh5NQNCBKlF/kZ6Wv/DsWZirpP9ceq7jTNadfDG5BJbDPf6Ocn4GcVRqY Lf0dMOEuufoNVOXJtTJTUjBimVvn1K9O+VMOxPA8AxQiaFLNhg+5EZcWQMAHCcFNlBpu nyaiVqDQ3HOq1aMQTZziIARVGIwGcLJ3qN0xwgt27t02KxZeBlj7goS9HAzw6z5ItK1s 6x+FOZMfbK7GKf/6qEMKsgGpmybS4GPMet7hRcs43InQvizYwYM4nuj1XKVJUmBS7Tcn lYsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b="qXvYGkq/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id g190-20020a636bc7000000b00578e4bff431si21061361pgc.420.2023.09.29.04.28.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 04:28:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b="qXvYGkq/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 81F68806833E; Wed, 27 Sep 2023 09:51:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229485AbjI0QvQ (ORCPT + 99 others); Wed, 27 Sep 2023 12:51:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjI0QvP (ORCPT ); Wed, 27 Sep 2023 12:51:15 -0400 Received: from mx0b-001ae601.pphosted.com (mx0b-001ae601.pphosted.com [67.231.152.168]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D28EF9C; Wed, 27 Sep 2023 09:51:13 -0700 (PDT) Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38RGf4a4009144; Wed, 27 Sep 2023 11:51:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=PODMain02222019; bh=6 6YwtrVPB63m73rzfrkXNgTtu4wVWf9hd6q5sRDERfc=; b=qXvYGkq/xQtDPr0ut UyCW1PJVEAA5IoA7TwFPqD80tzMyREgOIAMDqm5qL7EKlvbIE9UzD3hBQypP59+Y cnTPids4PNVEbe4QxCXM0KDPMHIuGYYNDJAOjVce3TFLqfBKRhstOgipajcPzJhs Iuqi0QBHmqxvfmkVNK8hS/XSJpH14GJyP4BfC0EokLKyK0XZ9TtxXMgTllemtJ+X MZ+yw9c3+wEcwxIrxOVqMFRuo6jrGXqOrGxMe3rhs6AJG0DXnY16LQA2nQyqb499 WHatROUrqOJS3r3cy9I+Si4PRuCRxa1smBII/gUKSO3gWpM92ZQ/VJu+sgWnrXRn GnQFw== Received: from ediex01.ad.cirrus.com ([84.19.233.68]) by mx0b-001ae601.pphosted.com (PPS) with ESMTPS id 3t9vejd1ag-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 27 Sep 2023 11:51:03 -0500 (CDT) Received: from ediex01.ad.cirrus.com (198.61.84.80) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.37; Wed, 27 Sep 2023 17:51:01 +0100 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by ediex01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.2.1118.37 via Frontend Transport; Wed, 27 Sep 2023 17:51:01 +0100 Received: from EDIN4L06LR3.ad.cirrus.com (EDIN4L06LR3.ad.cirrus.com [198.61.65.112]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id 5998E15B9; Wed, 27 Sep 2023 16:51:01 +0000 (UTC) From: Richard Fitzgerald To: , CC: , , , Richard Fitzgerald , Dan Carpenter Subject: [PATCH] kunit: debugfs: Handle errors from alloc_string_stream() Date: Wed, 27 Sep 2023 17:50:58 +0100 Message-ID: <20230927165058.29484-1-rf@opensource.cirrus.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-GUID: RA6cTYiBvohA6WeMlQ8Tn2N_krYxb87g X-Proofpoint-ORIG-GUID: RA6cTYiBvohA6WeMlQ8Tn2N_krYxb87g X-Proofpoint-Spam-Reason: safe X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 27 Sep 2023 09:51:30 -0700 (PDT) In kunit_debugfs_create_suite() give up and skip creating the debugfs file if any of the alloc_string_stream() calls return an error or NULL. Only put a value in the log pointer of kunit_suite and kunit_test if it is a valid pointer to a log. This prevents the potential invalid dereference reported by smatch: lib/kunit/debugfs.c:115 kunit_debugfs_create_suite() error: 'suite->log' dereferencing possible ERR_PTR() lib/kunit/debugfs.c:119 kunit_debugfs_create_suite() error: 'test_case->log' dereferencing possible ERR_PTR() Signed-off-by: Richard Fitzgerald Reported-by: Dan Carpenter Fixes: 05e2006ce493 ("kunit: Use string_stream for test log") --- lib/kunit/debugfs.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/kunit/debugfs.c b/lib/kunit/debugfs.c index 270d185737e6..73075ca6e88c 100644 --- a/lib/kunit/debugfs.c +++ b/lib/kunit/debugfs.c @@ -109,14 +109,27 @@ static const struct file_operations debugfs_results_fops = { void kunit_debugfs_create_suite(struct kunit_suite *suite) { struct kunit_case *test_case; + struct string_stream *stream; - /* Allocate logs before creating debugfs representation. */ - suite->log = alloc_string_stream(GFP_KERNEL); - string_stream_set_append_newlines(suite->log, true); + /* + * Allocate logs before creating debugfs representation. + * The log pointer must be NULL if there isn't a log so only + * set it if the log stream was created successfully. + */ + stream = alloc_string_stream(GFP_KERNEL); + if (IS_ERR_OR_NULL(stream)) + goto err; + + string_stream_set_append_newlines(stream, true); + suite->log = stream; kunit_suite_for_each_test_case(suite, test_case) { - test_case->log = alloc_string_stream(GFP_KERNEL); - string_stream_set_append_newlines(test_case->log, true); + stream = alloc_string_stream(GFP_KERNEL); + if (IS_ERR_OR_NULL(stream)) + goto err; + + string_stream_set_append_newlines(stream, true); + test_case->log = stream; } suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir); @@ -124,6 +137,12 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite) debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444, suite->debugfs, suite, &debugfs_results_fops); + return; + +err: + string_stream_destroy(suite->log); + kunit_suite_for_each_test_case(suite, test_case) + string_stream_destroy(test_case->log); } void kunit_debugfs_destroy_suite(struct kunit_suite *suite) -- 2.30.2