[PATCH libICE 12/13] add check for malloc

Emil Velikov emil.l.velikov at gmail.com
Tue Oct 31 14:47:00 UTC 2017


On 18 October 2017 at 17:07, walter harms <wharms at bfs.de> wrote:
>
>
> fix a potential null pointer deference error and
>
> IceAllocScratch() do not report size when allocation failes
>
> Signed-off-by: Walter Harms <wharms at bfs.de>
>
> ---
>  src/misc.c | 19 +++++++++++++++----
>  1 file changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/src/misc.c b/src/misc.c
> index 87d6335..fdc671d 100644
> --- a/src/misc.c
> +++ b/src/misc.c
> @@ -57,7 +57,10 @@ IceAllocScratch (
>         free (iceConn->scratch);
>
>         iceConn->scratch = malloc (size);
> -       iceConn->scratch_size = size;
> +       if ( !iceConn->scratch )
> +            iceConn->scratch_size = 0;
> +       else
> +            iceConn->scratch_size = size;
We should really update the STORE_STRING to consider IceAllocScratch
failure, scratch_size being 0.
Perhaps as a follow-up?

>      }
>
>      return (iceConn->scratch);
> @@ -415,12 +418,14 @@ _IceAddOpcodeMapping (
>  )
>  {
>      if (hisOpcode <= 0 || hisOpcode > 255)
> -    {
>         return;
Nit: please add a blank line here.

> -    }
> -    else if (iceConn->process_msg_info == NULL)
> +
> +    if (iceConn->process_msg_info == NULL)
>      {
>         iceConn->process_msg_info = malloc (sizeof (_IceProcessMsgInfo));
> +       if ( ! iceConn->process_msg_info )
> +         return;
> +
>         iceConn->his_min_opcode = iceConn->his_max_opcode = hisOpcode;
>      }
>      else if (hisOpcode < iceConn->his_min_opcode)
> @@ -433,6 +438,9 @@ _IceAddOpcodeMapping (
>         iceConn->process_msg_info = malloc (
>             newsize * sizeof (_IceProcessMsgInfo));
>
> +       if ( ! iceConn->process_msg_info )
> +         return;
> +
>         memcpy (&iceConn->process_msg_info[
>             iceConn->his_min_opcode - hisOpcode], oldVec,
>             oldsize * sizeof (_IceProcessMsgInfo));
> @@ -460,6 +468,9 @@ _IceAddOpcodeMapping (
>         iceConn->process_msg_info = malloc (
>             newsize * sizeof (_IceProcessMsgInfo));
>
> +       if ( ! iceConn->process_msg_info )
> +         return;
> +
Gut feeling says that the function can be simplified substantially...
regardless the patch looks good.

Thanks
Emil


More information about the xorg-devel mailing list