newlib: libc: Fix memory leak in computematchjumps()

Message ID 20231201161321.657684-1-visitorckw@gmail.com
State New
Headers
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

Corinna Vinschen Dec. 1, 2023, 6:30 p.m. UTC | #1
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
  
Mike Frysinger Dec. 1, 2023, 8:08 p.m. UTC | #2
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
  
Kuan-Wei Chiu Dec. 2, 2023, 4:13 a.m. UTC | #3
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
  

Patch

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++)