libcody: Avoid double-free

Message ID 20211021132714.636039-1-jwakely@redhat.com
State Committed
Commit e6d100354317475412161a487ffbcf36582115c5
Headers
Series libcody: Avoid double-free |

Commit Message

Jonathan Wakely Oct. 21, 2021, 1:27 p.m. UTC
  If the listen call fails then 'goto fail' will jump to that label and
use freeaddrinfo again. Set the pointer to null to prevent that.

libcody/ChangeLog:

	* netserver.cc (ListenInet6): Set pointer to null after
	deallocation.
---
 libcody/netserver.cc | 1 +
 1 file changed, 1 insertion(+)
  

Comments

Jonathan Wakely Oct. 21, 2021, 1:30 p.m. UTC | #1
Oops, this patch and the next one were only meant for gcc-patches, not the
libstdc++ list as well - sorry!


On Thu, 21 Oct 2021 at 14:27, Jonathan Wakely via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:

> If the listen call fails then 'goto fail' will jump to that label and
> use freeaddrinfo again. Set the pointer to null to prevent that.
>
> libcody/ChangeLog:
>
>         * netserver.cc (ListenInet6): Set pointer to null after
>         deallocation.
> ---
>  libcody/netserver.cc | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/libcody/netserver.cc b/libcody/netserver.cc
> index 30202c5106a..0499b5790b4 100644
> --- a/libcody/netserver.cc
> +++ b/libcody/netserver.cc
> @@ -140,6 +140,7 @@ int ListenInet6 (char const **e, char const *name, int
> port, unsigned backlog)
>
>   listen:;
>    freeaddrinfo (addrs);
> +  addrs = nullptr;
>
>    if (listen (fd, backlog ? backlog : 17) < 0)
>      {
> --
> 2.31.1
>
>
  
Jason Merrill Oct. 21, 2021, 7:34 p.m. UTC | #2
On 10/21/21 09:27, Jonathan Wakely wrote:
> If the listen call fails then 'goto fail' will jump to that label and
> use freeaddrinfo again. Set the pointer to null to prevent that.

OK.

> libcody/ChangeLog:
> 
> 	* netserver.cc (ListenInet6): Set pointer to null after
> 	deallocation.
> ---
>   libcody/netserver.cc | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/libcody/netserver.cc b/libcody/netserver.cc
> index 30202c5106a..0499b5790b4 100644
> --- a/libcody/netserver.cc
> +++ b/libcody/netserver.cc
> @@ -140,6 +140,7 @@ int ListenInet6 (char const **e, char const *name, int port, unsigned backlog)
>   
>    listen:;
>     freeaddrinfo (addrs);
> +  addrs = nullptr;
>   
>     if (listen (fd, backlog ? backlog : 17) < 0)
>       {
>
  

Patch

diff --git a/libcody/netserver.cc b/libcody/netserver.cc
index 30202c5106a..0499b5790b4 100644
--- a/libcody/netserver.cc
+++ b/libcody/netserver.cc
@@ -140,6 +140,7 @@  int ListenInet6 (char const **e, char const *name, int port, unsigned backlog)
 
  listen:;
   freeaddrinfo (addrs);
+  addrs = nullptr;
 
   if (listen (fd, backlog ? backlog : 17) < 0)
     {