[5/5] gaiconf_init: Avoid double-free in label and precedence lists

Message ID 20210727174129.3612656-6-siddhesh@sourceware.org
State Committed
Commit 77a34079d8f3d63b61543bf3af93043f8674e4c4
Delegated to: Arjun Shankar
Headers
Series Static analysis fixes |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Siddhesh Poyarekar July 27, 2021, 5:41 p.m. UTC
  labellist and precedencelist could get freed a second time if there
are allocation failures, so set them to NULL to avoid a double-free.
---
 sysdeps/posix/getaddrinfo.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Arjun Shankar Aug. 3, 2021, 3:34 p.m. UTC | #1
Hi Siddhesh,

> labellist and precedencelist could get freed a second time if there
> are allocation failures, so set them to NULL to avoid a double-free.
> ---
>  sysdeps/posix/getaddrinfo.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
> index 838a68f022..43dfc6739e 100644
> --- a/sysdeps/posix/getaddrinfo.c
> +++ b/sysdeps/posix/getaddrinfo.c
> @@ -2008,6 +2008,7 @@ gaiconf_init (void)
>  	      l = l->next;
>  	    }
>  	  free_prefixlist (labellist);
> +	  labellist = NULL;
>  
>  	  /* Sort the entries so that the most specific ones are at
>  	     the beginning.  */
> @@ -2046,6 +2047,7 @@ gaiconf_init (void)
>  	      l = l->next;
>  	    }
>  	  free_prefixlist (precedencelist);
> +	  precedencelist = NULL;
>  
>  	  /* Sort the entries so that the most specific ones are at
>  	     the beginning.  */

Looks good to me.

Yes, a later allocation failure can trigger a `goto no_file' which
tries to free these (again, in this case). Setting to NULL avoids that.

Reviewed-by: Arjun Shankar <arjun@redhat.com>

Cheers!
  

Patch

diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index 838a68f022..43dfc6739e 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -2008,6 +2008,7 @@  gaiconf_init (void)
 	      l = l->next;
 	    }
 	  free_prefixlist (labellist);
+	  labellist = NULL;
 
 	  /* Sort the entries so that the most specific ones are at
 	     the beginning.  */
@@ -2046,6 +2047,7 @@  gaiconf_init (void)
 	      l = l->next;
 	    }
 	  free_prefixlist (precedencelist);
+	  precedencelist = NULL;
 
 	  /* Sort the entries so that the most specific ones are at
 	     the beginning.  */