Message ID | 20221028144241.634012-1-her0gyugyu@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp872222wru; Fri, 28 Oct 2022 07:47:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6RSjWu790cq4uQPIv+Ybqk/aHBXvJB52nLFFgkV8WSevV8EtmiFvaMQVarc5KI8zHvhVKy X-Received: by 2002:a17:907:8690:b0:791:9f83:6872 with SMTP id qa16-20020a170907869000b007919f836872mr45328191ejc.386.1666968444964; Fri, 28 Oct 2022 07:47:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666968444; cv=none; d=google.com; s=arc-20160816; b=hLb+WUteo+uX084xVpt3ZjCAwc6/iGKPjSNPeCnC1ljskHb8EKXWHj2wykpaGTd7vS Un1Vf2FdiuePgGTZ0t+12rGTkg5Sr/w/1dF+Ml0edWJLf6EOcEDGc2pS7dA8ICTMtm5f Tv5DDBSOqej7C8d5ZwdW3/XgI497roThuAug8/Z4K7cgA4LGe9IgsFVs6yOZfktV0ZH+ BdNtxP1Qq2197DtXJ1SAgmrz8SIX3tkYwR7oeA/EsO8DF5wme8ui3Vh1ShcPjwOxFBjS O5LyGwOzx+XjsopJt1Aq1vldQEizS15YgnbtZ8KtA2T8DGGzxRFvsfl8H5cmV6plvvYR OpGw== 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=zRwJKEcQ0Hj/RT3YqJ8AoMPqLaWpCYHYaDCqc/JUl2s=; b=SvkaX0JoyhEbzq6zH8UlBBWV+qm5qrCRSh7angzIQ0qxeh0oyMQMw+dO8xeu7BNh2E X4nyTfY5FqN79SfN81BExDiPWcEKZvWlSNzkCbgX18cdowW3+7QSuocdn2YBkAAnUhOA 6jxJpSxhSmC2nIChNG0k1vpm0nBqc5FBaUMBZEC5SFlL2waOmMx6CEO56s1ZJPRShpeW XzNFmrbgR4STbI2S2RBBZOHdoNFXMeoy1pPqZnIwq4lbHPH+EwJfWPJFfQwjvrsyQB25 lLisGlA3N9XeSz4qXZsoTIyfVe6NjE2t02n8n2dgIn1baNuoMkyZ1Pysiv+OAvxQUyWj lmpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="bS/EmyhI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sb42-20020a1709076daa00b00741c0bd7061si4692411ejc.644.2022.10.28.07.47.00; Fri, 28 Oct 2022 07:47:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="bS/EmyhI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbiJ1One (ORCPT <rfc822;chrisfriedt@gmail.com> + 99 others); Fri, 28 Oct 2022 10:43:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230370AbiJ1OnU (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 28 Oct 2022 10:43:20 -0400 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25C411F8128; Fri, 28 Oct 2022 07:43:10 -0700 (PDT) Received: by mail-qv1-xf2d.google.com with SMTP id n18so4170317qvt.11; Fri, 28 Oct 2022 07:43:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zRwJKEcQ0Hj/RT3YqJ8AoMPqLaWpCYHYaDCqc/JUl2s=; b=bS/EmyhIlDXTTzU2EwcUthfcDkvObqeh3OWmsvxHBclDyuHQYMSCUVFco4PBybMGPu UBqzSQFg8fLPeY7kuO04h2nL95CDqqb8tM+F8ocWtrYoLZdnnizDgBkVJ6Km4CqidEF4 h9KYEQZ40WTqf/P6SS2c5qTCx3WYkF9KTr11eJAnxAj1nispUFS1g/QQTN+1i2wQwuW0 BKclEI2DeAz3HSMkidbgacW2+qSVgJj9VC799qMUCS5fJ38H57ESCgedL9JDpiNLxgkG wcs0SWEgkakMWf5LyiJAiK0vJVFecfQarQW0itSSbhuMNkms1wsAX9qmO+iD/BGOSELR ZRrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zRwJKEcQ0Hj/RT3YqJ8AoMPqLaWpCYHYaDCqc/JUl2s=; b=OGJcK6SgJr433bO5aOuuaTFXlEdEkYUTKXMO/IOU1DkjjGGkvctZU3arRC6vg5gq9d A083UlBWim/jktHZp+lvb7kbUyTaNIaM+zqoMOszMTSvlBxJpiWjjX4TcKAoeb/byk72 Jc4YvvGTnfuGAOY0CVgajR/w61uwWa9u+sP9FDL/kNJvcdzst1h3J4ptUcquwI/1kPsR iGFG8+bqUMx+8lXk12bf+5Tu3mK5bVkGdTyvq+CD4yljDBMrXuF5rYmamAJMVBnLS1A1 mAfzq6Qk0oaPzZaSGUYqwfyktNcDIf2EabWT2Gc5Do3zr8QFOtcddsfAiSGxnO1EV0bQ tVwA== X-Gm-Message-State: ACrzQf1VfopyHNPtBptWKlPqC3flaxd9MeKQgWcXWLPjbYUza0Dx+Q44 BHzI3lRosXoFqJmJmoE6bjQyUPIYMzE= X-Received: by 2002:a17:902:b402:b0:179:e5b0:96d3 with SMTP id x2-20020a170902b40200b00179e5b096d3mr54453723plr.142.1666968178448; Fri, 28 Oct 2022 07:42:58 -0700 (PDT) Received: from ubuntu.localdomain ([1.221.137.166]) by smtp.gmail.com with ESMTPSA id z13-20020a170902d54d00b00186ad73e2d5sm3137922plf.208.2022.10.28.07.42.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 07:42:57 -0700 (PDT) From: "YoungJun.park" <her0gyugyu@gmail.com> To: Brendan Higgins <brendan.higgins@linux.dev> Cc: David Gow <davidgow@google.com>, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, "YoungJun.park" <her0gyugyu@gmail.com> Subject: [PATCH] kunit: alloc_string_stream_fragment error handling bug fix Date: Fri, 28 Oct 2022 07:42:41 -0700 Message-Id: <20221028144241.634012-1-her0gyugyu@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747943104408250187?= X-GMAIL-MSGID: =?utf-8?q?1747943104408250187?= |
Series |
kunit: alloc_string_stream_fragment error handling bug fix
|
|
Commit Message
YoungJun.park
Oct. 28, 2022, 2:42 p.m. UTC
When it fails to allocate fragment, it does not free and return error.
And check the pointer inappropriately.
Signed-off-by: YoungJun.park <her0gyugyu@gmail.com>
---
lib/kunit/string-stream.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
Comments
On 10/28/22 11:42, YoungJun.park wrote: > When it fails to allocate fragment, it does not free and return error. > And check the pointer inappropriately. > > Signed-off-by: YoungJun.park <her0gyugyu@gmail.com> > --- > lib/kunit/string-stream.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c > index 72659a9773e3..0228fe814e96 100644 > --- a/lib/kunit/string-stream.c > +++ b/lib/kunit/string-stream.c > @@ -23,8 +23,10 @@ static struct string_stream_fragment *alloc_string_stream_fragment( > return ERR_PTR(-ENOMEM); > > frag->fragment = kunit_kmalloc(test, len, gfp); > - if (!frag->fragment) > + if (!frag->fragment) { > + kunit_kfree(test, frag); I don't believe that kunit_kfree is necessary here, because kunit_kmalloc is like kmalloc, but the allocation is test managed, which means that the allocation is managed by the test case and is automatically cleaned up after the test case concludes. So, the original version seems correct: if the allocation fails, alloc_string_stream_fragment will return NULL and string_stream_vadd will check if frag_container is different than NULL. Best Regards, - Maíra Canal > return ERR_PTR(-ENOMEM); > + } > > return frag; > } > @@ -56,7 +58,7 @@ int string_stream_vadd(struct string_stream *stream, > frag_container = alloc_string_stream_fragment(stream->test, > len, > stream->gfp); > - if (!frag_container) > + if (IS_ERR(frag_container)) > return -ENOMEM; > > len = vsnprintf(frag_container->fragment, len, fmt, args);
On Fri, Oct 28, 2022 at 10:43 PM YoungJun.park <her0gyugyu@gmail.com> wrote: > > When it fails to allocate fragment, it does not free and return error. > And check the pointer inappropriately. > > Signed-off-by: YoungJun.park <her0gyugyu@gmail.com> > --- Thanks! As Maíra points out, the added kunit_kfree() call isn't strictly necessary, though it definitely doesn't hurt (and it's probably a nice thing to free memory early if we're already in a pretty dire memory situation). So I think it's an improvement. The IS_ERR check is definitely a fix, though. Reviewed-by: David Gow <davidgow@google.com> Cheers, -- David > lib/kunit/string-stream.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c > index 72659a9773e3..0228fe814e96 100644 > --- a/lib/kunit/string-stream.c > +++ b/lib/kunit/string-stream.c > @@ -23,8 +23,10 @@ static struct string_stream_fragment *alloc_string_stream_fragment( > return ERR_PTR(-ENOMEM); > > frag->fragment = kunit_kmalloc(test, len, gfp); > - if (!frag->fragment) > + if (!frag->fragment) { > + kunit_kfree(test, frag); > return ERR_PTR(-ENOMEM); > + } > > return frag; > } > @@ -56,7 +58,7 @@ int string_stream_vadd(struct string_stream *stream, > frag_container = alloc_string_stream_fragment(stream->test, > len, > stream->gfp); > - if (!frag_container) > + if (IS_ERR(frag_container)) > return -ENOMEM; > > len = vsnprintf(frag_container->fragment, len, fmt, args); > -- > 2.25.1 > > -- > You received this message because you are subscribed to the Google Groups "KUnit Development" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20221028144241.634012-1-her0gyugyu%40gmail.com.
On Sat, Oct 29, 2022 at 8:20 PM 'David Gow' via KUnit Development <kunit-dev@googlegroups.com> wrote: > > On Fri, Oct 28, 2022 at 10:43 PM YoungJun.park <her0gyugyu@gmail.com> wrote: > > > > When it fails to allocate fragment, it does not free and return error. > > And check the pointer inappropriately. > > > > Signed-off-by: YoungJun.park <her0gyugyu@gmail.com> > > --- > > Thanks! As Maíra points out, the added kunit_kfree() call isn't > strictly necessary, though it definitely doesn't hurt (and it's > probably a nice thing to free memory early if we're already in a > pretty dire memory situation). So I think it's an improvement. > > The IS_ERR check is definitely a fix, though. Note: the IS_ERR check was fixed already in https://patchwork.kernel.org/project/linux-kselftest/patch/Y0kt1aCTHO4r2CmL@kili/ That change has made its way into torvalds/master. So we could rebase this patch and reword it to talk just about the early kfree(). Re free memory early: It'll save us sizeof(struct string_stream_fragment) + sizeof(struct kunit_resource), i.e. 24 + 56 = 80 bytes (on UML/x86_64). So it's not much, but I guess it could help in edge cases. Daniel
diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 72659a9773e3..0228fe814e96 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -23,8 +23,10 @@ static struct string_stream_fragment *alloc_string_stream_fragment( return ERR_PTR(-ENOMEM); frag->fragment = kunit_kmalloc(test, len, gfp); - if (!frag->fragment) + if (!frag->fragment) { + kunit_kfree(test, frag); return ERR_PTR(-ENOMEM); + } return frag; } @@ -56,7 +58,7 @@ int string_stream_vadd(struct string_stream *stream, frag_container = alloc_string_stream_fragment(stream->test, len, stream->gfp); - if (!frag_container) + if (IS_ERR(frag_container)) return -ENOMEM; len = vsnprintf(frag_container->fragment, len, fmt, args);