Message ID | 20231201161321.657684-1-visitorckw@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <newlib-bounces+patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9AE5A3857BA2 for <patchwork@sourceware.org>; Fri, 1 Dec 2023 16:13:42 +0000 (GMT) X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id 161A33858CDB for <newlib@sourceware.org>; Fri, 1 Dec 2023 16:13:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 161A33858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 161A33858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::433 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701447209; cv=none; b=HQ23hMstWlXJPsetI1xhh/r5gmb6g3GNQH5wKBNQsA+2PYh+Pt+QNVNv3mQcgUzJ4EFSm5mhBRhwjFtKyzhWsSXQZRzK8yvLM5h5WgWOU0d0qsmUufcIk3AqV/JdYKF9WB1mNQTvFcDcA5o6Th7mtgTQipw4HT9Vvn8tqRumh+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701447209; c=relaxed/simple; bh=4lPWFbhk5O3z+NVgUvYQRFz2RVOfI7pD26CG182aJxI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=IVtiT96eMlYSxrdtpKD4EDWcR9bC6sCYlwuskEee8Jw5YO0rFZA1E+V6AbeDeazMmrZbrxU2tQ8s4n22PTm1crx86xodRt3Ev+hyT/Jg3YCqz4YvGaUrjHPBZp0QzAOk+pspzHbnuLU5evbQiWGTfyAbIBMFhPVq1xWhk9ECbcg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6cde1bf77b5so459304b3a.0 for <newlib@sourceware.org>; Fri, 01 Dec 2023 08:13:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701447206; x=1702052006; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=viAp2gZlutyl0dUNVIT7A4AZoMUP2tSfHbXiijrOjLU=; b=Z73vEJUmWIiplW4cP2lQ4haMf5WHPF8NoCL6MLAcK66D66iHhiHrEG2/KB0tBDHsU1 aRYlpYdaGilUZktCQC1lX8IdsxuMT9sUjRvv5YhaoG/Zd9ADYoYGg3KMZtyFN2EHjN51 f1uaPs+3jWx8opn94V98ISNPdo95tUMsb+XXe7psy9zLbVpufUtsTk8lo7qlRoVZrdtX 9MFOD0eFwvuPX2gZZHZ2pfz3Kf3hpdUB+xCfIPVSKRMeXvbJxJU654qeok1Lh8XwnE6R OmlXEgQHzQqFAj6LSb3XIB2hnKq3Ru8N3ESzGkL6eUD/eGGTFyk3yxrcGeVf2PuwokN/ rCLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701447206; x=1702052006; 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=viAp2gZlutyl0dUNVIT7A4AZoMUP2tSfHbXiijrOjLU=; b=Jhz/wREqs/q4HLyMwIjG22okoQyrLzdCUKwHcDLApehXYKcGG2yeRKaMwnELShxotK /LYB6x8PUpJt79L2Af5EXyE1KCXqqoAB3iVaxwpzGIkuSpAgNcEEo1LobhzFjkNwFkcp 5AL0cKmqtEqpm0vmtaBkK5Zf/888QiR0vA/L9JLH8gz9gsAkH4gvj8zS2AhO33c7gU4N WB814ZWMb1wAZ+fPFMgB1o22qiJC+sVfAHHHaz6uX4fNGV+fdl4diTYat3u0nAVh5n60 x/aPRJGMPYo+Pp1FGSSYIfR6FY2CKVZInjNHEtN+/HSwJ1r04zbBUVVmMSEuilB1qpHA NHtA== X-Gm-Message-State: AOJu0YxdmbAlNi2aYcZ0y4b6nVy/QCa+c77PrT1jCL2nmAwxfSISVyLH xbzpVZblAQRsmErV4YHuKkwzAp0TV70= X-Google-Smtp-Source: AGHT+IFsvJMwKdGpXLGxAo4SKeAN2zDzF70OTKuwszTDRHl+jTF3+b2JVuehYd00vuKXW/HXPH8cTw== X-Received: by 2002:a05:6a00:1785:b0:6cd:e328:9d8e with SMTP id s5-20020a056a00178500b006cde3289d8emr7240434pfg.3.1701447206337; Fri, 01 Dec 2023 08:13:26 -0800 (PST) Received: from localhost.localdomain ([140.116.154.65]) by smtp.gmail.com with ESMTPSA id q3-20020a056a00084300b006cb9a43ae4esm3154096pfk.215.2023.12.01.08.13.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 08:13:25 -0800 (PST) From: Kuan-Wei Chiu <visitorckw@gmail.com> To: newlib@sourceware.org Cc: Kuan-Wei Chiu <visitorckw@gmail.com> Subject: [PATCH] newlib: libc: Fix memory leak in computematchjumps() Date: Sat, 2 Dec 2023 00:13:21 +0800 Message-Id: <20231201161321.657684-1-visitorckw@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> Errors-To: newlib-bounces+patchwork=sourceware.org@sourceware.org |
Series |
newlib: libc: Fix memory leak in computematchjumps()
|
|
Commit Message
Kuan-Wei Chiu
Dec. 1, 2023, 4:13 p.m. UTC
In cases where malloc fails for the 'g->matchjump' allocation, the code
path does not handle the failure gracefully, potentially leading to a
memory leak. This fix ensures proper cleanup by freeing the allocated
memory for 'pmatches' before returning.
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
newlib/libc/posix/regcomp.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
Comments
On Dec 2 00:13, Kuan-Wei Chiu wrote: > In cases where malloc fails for the 'g->matchjump' allocation, the code > path does not handle the failure gracefully, potentially leading to a > memory leak. This fix ensures proper cleanup by freeing the allocated > memory for 'pmatches' before returning. > > Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> > --- > newlib/libc/posix/regcomp.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/newlib/libc/posix/regcomp.c b/newlib/libc/posix/regcomp.c > index 002f978cd..e71bc005c 100644 > --- a/newlib/libc/posix/regcomp.c > +++ b/newlib/libc/posix/regcomp.c > @@ -2001,8 +2001,10 @@ struct re_guts *g; > } > > g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int)); > - if (g->matchjump == NULL) /* Not a fatal error */ > - return; > + if (g->matchjump == NULL) { /* Not a fatal error */ > + free(pmatches); > + return; > + } > > /* Set maximum possible jump for each character in the pattern */ > for (mindex = 0; mindex < g->mlen; mindex++) > -- > 2.25.1 Pushed. Thanks, Corinna
On 02 Dec 2023 00:13, Kuan-Wei Chiu wrote: > --- a/newlib/libc/posix/regcomp.c > +++ b/newlib/libc/posix/regcomp.c > @@ -2001,8 +2001,10 @@ struct re_guts *g; > } > > g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int)); > - if (g->matchjump == NULL) /* Not a fatal error */ > - return; > + if (g->matchjump == NULL) { /* Not a fatal error */ > + free(pmatches); > + return; > + } the code uses tabs for indentation, but you used spaces for some reason -mike
On Fri, Dec 01, 2023 at 03:08:30PM -0500, Mike Frysinger wrote: > On 02 Dec 2023 00:13, Kuan-Wei Chiu wrote: > > --- a/newlib/libc/posix/regcomp.c > > +++ b/newlib/libc/posix/regcomp.c > > @@ -2001,8 +2001,10 @@ struct re_guts *g; > > } > > > > g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int)); > > - if (g->matchjump == NULL) /* Not a fatal error */ > > - return; > > + if (g->matchjump == NULL) { /* Not a fatal error */ > > + free(pmatches); > > + return; > > + } > > the code uses tabs for indentation, but you used spaces for some reason > -mike Hi Mike, Thank you for reviewing the patch. I apologize for the oversight regarding the indentation inconsistency—using spaces instead of tabs. I will submit another patch that corrects the indentation. Best regards, Kuan-Wei Chiu
diff --git a/newlib/libc/posix/regcomp.c b/newlib/libc/posix/regcomp.c index 002f978cd..e71bc005c 100644 --- a/newlib/libc/posix/regcomp.c +++ b/newlib/libc/posix/regcomp.c @@ -2001,8 +2001,10 @@ struct re_guts *g; } g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int)); - if (g->matchjump == NULL) /* Not a fatal error */ - return; + if (g->matchjump == NULL) { /* Not a fatal error */ + free(pmatches); + return; + } /* Set maximum possible jump for each character in the pattern */ for (mindex = 0; mindex < g->mlen; mindex++)