[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freedesktop-xorg-devel
Subject:    Re: [PATCH libICE 06/13] check malloc return
From:       Emil Velikov <emil.l.velikov () gmail ! com>
Date:       2017-10-31 13:40:44
Message-ID: CACvgo50+GE28o906L55NSzXD-TbyFQnoOoOnJOmMAcN4SCch5g () mail ! gmail ! com
[Download RAW message or body]

On 18 October 2017 at 16:58, walter harms <wharms@bfs.de> wrote:
>
>
>  check malloc return
>  failed mallocs will cause segfaults, so add check
>  also free already allocated memory
>
>  Signed-off-by: Walter Harms <wharms@bfs.de>
> ---
>  src/connect.c | 19 +++++++++++++++----
>  1 file changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/src/connect.c b/src/connect.c
> index 276a356..b61449e 100644
> --- a/src/connect.c
> +++ b/src/connect.c
> @@ -193,8 +193,8 @@ IceOpenConnection (
>
>      iceConn->connect_to_me = NULL;
>      iceConn->protosetup_to_me = NULL;
> -
> -    if ((iceConn->inbuf = iceConn->inbufptr = malloc (ICE_INBUFSIZE)) == NULL)
> +    iceConn->inbuf = iceConn->inbufptr = malloc (ICE_INBUFSIZE);
> +    if ( iceConn->inbuf == NULL)
>      {
>         _IceFreeConnection (iceConn);
>         strncpy (errorStringRet, "Can't malloc", errorLength);
> @@ -202,9 +202,10 @@ IceOpenConnection (
>      }
>
>      iceConn->inbufmax = iceConn->inbuf + ICE_INBUFSIZE;
> -
> -    if ((iceConn->outbuf = iceConn->outbufptr = calloc (1, ICE_OUTBUFSIZE)) == NULL)
> +    iceConn->outbuf = iceConn->outbufptr = calloc (1, ICE_OUTBUFSIZE);
> +    if ( iceConn->outbuf == NULL)
Unrelated cleanup, but it should be fine.

>      {
> +        free(iceConn->inbuf);
Not needed/unrelated _IceFreeConnection does it for us.

Mildly related: iceConn should be allocated with calloc, otherwise
_IceFreeConnection() will explode... badly.

-Emil
_______________________________________________
xorg-devel@lists.x.org: X.Org development
Archives: http://lists.x.org/archives/xorg-devel
Info: https://lists.x.org/mailman/listinfo/xorg-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic