From nobody Sat Apr 18 17:23:52 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 619B0C433EF for ; Tue, 12 Jul 2022 09:56:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232365AbiGLJ4p (ORCPT ); Tue, 12 Jul 2022 05:56:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229780AbiGLJ4m (ORCPT ); Tue, 12 Jul 2022 05:56:42 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F979AA836 for ; Tue, 12 Jul 2022 02:56:40 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id j23-20020a17090a061700b001e89529d397so4765317pjj.6 for ; Tue, 12 Jul 2022 02:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=x/kle2uksxTcc6x66ftiegZgKc2BrTGOuQ67dMnzc4Y=; b=kwkTWMMQwkkHAKjVLJeMpSK64OpdmOgu236oC0XBqrX0D7J98KaB6ERHvVIsxhbZCI IjZbLhv6i+RjBhueyuRc2z89NREHndLUI7P+1cS2M3rofdySIBUefE8Zx/tKIC62tGPb sBLF+iClr+Zn5Bm0RSvEka+eTWzazkVC/3O2ZFBL8jIT9WJLQsAfwBZSbJkQMZTIfI89 cORewAgFlyPdAdlkPiW7xz3O8SKElU7ZpBuFMq87gGR/aTiD/l0R19PUxl4Rt6t5Q2Qx 4aT5W6gW5nD0vJVxsHCUz3+2p5cFeMveFo94NEWcVBwoseoOsr7S11L+fg9cG/HfuMvd wVwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=x/kle2uksxTcc6x66ftiegZgKc2BrTGOuQ67dMnzc4Y=; b=ToBS4QDaQ8XSM8wonRDTiC1vlEXIKS8cjSLwsH2ORjh9liXAUdrdagcWcBO0Y3OhoU gpTPijvbCV4Knj8lhEWCFPxpheOFYhOpcLevMXfO7h1M0eOfW8/hFZmqlija3QvOSSmO bu5H1B50+QyhqrQohhuBi0/lSvGHAz/GXJMTSNF5xiv+q2vEF1UNyamYfkTZluEm6EP0 nmNZDN3QnsjMxk/TkTH0xiuth6KyP5TTeXLNNH/JRKiE61f5zeT0XcMJ0LuCCc9sLCJz POLOPfkxqZhdXtAhiUO+FfC2k7voerqPFVoLlo8Mhg8xChNRokepQaX7wBm/ezETI9Jb lj+Q== X-Gm-Message-State: AJIora8Ip08oOddzyo6jb+Kn9Fdv7xtEnxp7+T7YwDgABaXY7QpAT2XP satdpjC2GEF5RRoFCpWzVflTSyUT/Ur7QQ== X-Google-Smtp-Source: AGRyM1vvm3ysCBRhKyHvAVl7sjPZJlrlsNeD8k0D/UrZt0He9duZpcfV3smERgIMPPYL1yRGLfEhOGf5c4KqDg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a17:90a:249:b0:1e0:a8a3:3c6c with SMTP id t9-20020a17090a024900b001e0a8a33c6cmr120662pje.0.1657619799129; Tue, 12 Jul 2022 02:56:39 -0700 (PDT) Date: Tue, 12 Jul 2022 17:56:27 +0800 Message-Id: <20220712095627.1770312-1-davidgow@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.0.144.g8ac04bfd2-goog Subject: [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests From: David Gow To: Daniel Latypov , Brendan Higgins , Shuah Khan Cc: David Gow , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, Linux Kernel Mailing List , llvm@lists.linux.dev, kernel test robot Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It's possible that memory allocation for the copy will fail, but for the copy of the suite to succeed. In this case, the copy could be leaked. Properly free 'copy' in the error case for the allocation of 'filtered' failing. Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites". This was reported by clang-analyzer via the kernel test robot, here: https://lore.kernel.org/all/c8073b8e-7b9e-0830-4177-87c12f16349c@intel.com/ Fixes: a02353f49162 ("kunit: bail out of test filtering logic quicker if OO= M") Reported-by: kernel test robot Signed-off-by: David Gow Reviewed-by: Brendan Higgins Reviewed-by: Daniel Latypov --- lib/kunit/executor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c index 6c489d6c5e5d..5e223327196a 100644 --- a/lib/kunit/executor.c +++ b/lib/kunit/executor.c @@ -74,8 +74,10 @@ kunit_filter_tests(const struct kunit_suite *const suite= , const char *test_glob) return ERR_PTR(-ENOMEM); =20 filtered =3D kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); - if (!filtered) + if (!filtered) { + kfree(copy); return ERR_PTR(-ENOMEM); + } =20 n =3D 0; kunit_suite_for_each_test_case(suite, test_case) { --=20 2.37.0.144.g8ac04bfd2-goog