[PATCH libICE 07/13] add check for malloc

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


On 18 October 2017 at 17:01, walter harms <wharms at bfs.de> wrote:
>
>  add check for malloc and a bit untangling
>  Signed-off-by: Walter Harms <wharms at bfs.de>
>
> ---
>  src/watch.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/src/watch.c b/src/watch.c
> index abbc265..c60d0c1 100644
> --- a/src/watch.c
> +++ b/src/watch.c
> @@ -48,7 +48,8 @@ IceAddConnectionWatch (
>      _IceWatchProc      *newWatchProc;
>      int                        i;
>
> -    if ((newWatchProc = malloc (sizeof (_IceWatchProc))) == NULL)
> +    newWatchProc = malloc (sizeof (_IceWatchProc));
> +    if ( !newWatchProc )
Nit: no space between the brackets and the inner contents.

>      {
>         return (0);
>      }
> @@ -75,7 +76,11 @@ IceAddConnectionWatch (
>      {
>         _IceWatchedConnection *newWatchedConn =
>             malloc (sizeof (_IceWatchedConnection));
> -
> +       if (!newWatchedConn)
> +         {
> +           IceRemoveConnectionWatch(watchProc,clientData);
Nit: space after the comma.

This and the rest of the file are kind of funny, since:
If we bail out midway, we have a bunch of leaks and there's no obvious
way to inverse the watch_proc() call.

One solution is to do all the allocation at once, but that'll require:
 - individual pointers should be aligned to multiple of sizeof(void *)
 - but above all we'll need to ensure the teardown does not cause double free

Since that's more picky, let's have the top-level *alloc checks for
now alongside a comment/how to about the rest?

> +           return (0);
> +         }
>         newWatchedConn->iceConn = _IceConnectionObjs[i];
>         newWatchedConn->next = NULL;
>
> @@ -86,6 +91,7 @@ IceAddConnectionWatch (
>      }
>
>      return (1);
> +
Please drop.

>  }
>
>
> @@ -143,6 +149,9 @@ _IceConnectionOpened (
>             malloc (sizeof (_IceWatchedConnection));
>         _IceWatchedConnection *watchedConn;
>
> +       if (!newWatchedConn)
> +         return ;
> +
No space before semicolon.

-Emil


More information about the xorg-devel mailing list