[PATCH 07/17] Convert os/* to new *allocarray functions

Matt Turner mattst88 at gmail.com
Tue Mar 31 20:03:21 PDT 2015


On Tue, Mar 31, 2015 at 5:50 PM, Alan Coopersmith
<alan.coopersmith at oracle.com> wrote:
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
>  os/connection.c |   13 +++++++------
>  os/utils.c      |    2 +-
>  2 files changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/os/connection.c b/os/connection.c
> index 7ff44e1..5dec02f 100644
> --- a/os/connection.c
> +++ b/os/connection.c
> @@ -309,7 +309,7 @@ InitConnectionLimits(void)
>
>  #if !defined(WIN32)
>      if (!ConnectionTranslation)
> -        ConnectionTranslation = (int *) xnfalloc(sizeof(int) * (lastfdesc + 1));
> +        ConnectionTranslation = xnfallocarray(lastfdesc + 1, sizeof(int));
>  #else
>      InitConnectionTranslation();
>  #endif
> @@ -429,7 +429,9 @@ CreateWellKnownSockets(void)
>          display = dynamic_display;
>      }
>
> -    ListenTransFds = malloc(ListenTransCount * sizeof (int));
> +    ListenTransFds = xallocarray(ListenTransCount, sizeof (int));
> +    if (ListenTransFds == NULL)
> +        FatalError ("Failed to create listening socket array");
>
>      for (i = 0; i < ListenTransCount; i++) {
>          int fd = _XSERVTransGetConnectionNumber(ListenTransConns[i]);
> @@ -1291,11 +1293,10 @@ ListenOnOpenFD(int fd, int noxauth)
>
>      /* Allocate space to store it */
>      ListenTransFds =
> -        (int *) realloc(ListenTransFds, (ListenTransCount + 1) * sizeof(int));
> +        xnfreallocarray(ListenTransFds, (ListenTransCount + 1), sizeof(int));

Probably remove the now useless parentheses here.

>      ListenTransConns =
> -        (XtransConnInfo *) realloc(ListenTransConns,
> -                                   (ListenTransCount +
> -                                    1) * sizeof(XtransConnInfo));
> +        xnfreallocarray(ListenTransConns, (ListenTransCount + 1),

And here.

> +                        sizeof(XtransConnInfo));
>
>      /* Store it */
>      ListenTransConns[ListenTransCount] = ciptr;
> diff --git a/os/utils.c b/os/utils.c
> index 23f4117..cd1cf42 100644
> --- a/os/utils.c
> +++ b/os/utils.c
> @@ -2001,7 +2001,7 @@ xstrtokenize(const char *str, const char *separators)
>      if (!tmp)
>          goto error;
>      for (tok = strtok(tmp, separators); tok; tok = strtok(NULL, separators)) {
> -        nlist = realloc(list, (num + 2) * sizeof(*list));
> +        nlist = reallocarray(list, (num + 2), sizeof(*list));

And here.

>          if (!nlist)
>              goto error;
>          list = nlist;
> --
> 1.7.9.2


More information about the xorg-devel mailing list