From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 635EE3858C2C; Fri, 1 Dec 2023 18:29:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 635EE3858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1701455381; bh=fr7EyOZzLLOMWn6kcbmSSEooCeE8LUfPSMrDMZAqwk4=; h=From:To:Subject:Date:From; b=jeBbMFwMFWUqhRD87ZfUgdrZJfsqh918nDTI+Tw12n55cshOfvVHbB0UFNa1w5aUa QqnGh2ojnfh4PfOdG8BEzeJX5Vrs5YqIrFFaCM8PaRJKXXR7cNeWT3+qs3ogjWwioD KvZEl4R1hOnhNkTw/wYbcNrxb3hsQbKFnQRKUxsU= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: newlib-cvs@sourceware.org Subject: [newlib-cygwin/main] newlib: libc: Fix memory leak in computematchjumps() X-Act-Checkin: newlib-cygwin X-Git-Author: Kuan-Wei Chiu X-Git-Refname: refs/heads/main X-Git-Oldrev: 10da64688016c0146e049eef3533a35ff19e6b6e X-Git-Newrev: 65f7ab0bb928162f6a6ea60e2803d6b9e5d95e8b Message-Id: <20231201182941.635EE3858C2C@sourceware.org> Date: Fri, 1 Dec 2023 18:29:41 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D65f7ab0bb92= 8162f6a6ea60e2803d6b9e5d95e8b commit 65f7ab0bb928162f6a6ea60e2803d6b9e5d95e8b Author: Kuan-Wei Chiu AuthorDate: Sat Dec 2 00:13:21 2023 +0800 Commit: Corinna Vinschen CommitDate: Fri Dec 1 19:28:55 2023 +0100 newlib: libc: Fix memory leak in computematchjumps() =20 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. =20 Signed-off-by: Kuan-Wei Chiu Diff: --- 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 002f978cd07c..e71bc005c920 100644 --- a/newlib/libc/posix/regcomp.c +++ b/newlib/libc/posix/regcomp.c @@ -2001,8 +2001,10 @@ struct re_guts *g; } =20 g->matchjump =3D (int*) malloc(g->mlen * sizeof(unsigned int)); - if (g->matchjump =3D=3D NULL) /* Not a fatal error */ - return; + if (g->matchjump =3D=3D NULL) { /* Not a fatal error */ + free(pmatches); + return; + } =20 /* Set maximum possible jump for each character in the pattern */ for (mindex =3D 0; mindex < g->mlen; mindex++)