[PATCH libICE 06/13] check malloc return

Emil Velikov emil.l.velikov at gmail.com
Tue Oct 31 13:40:44 UTC 2017


On 18 October 2017 at 16:58, walter harms <wharms at 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 at 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


More information about the xorg-devel mailing list