diff options
Diffstat (limited to 'xorg-server/os')
-rw-r--r-- | xorg-server/os/connection.c | 13 | ||||
-rw-r--r-- | xorg-server/os/reallocarray.c | 43 | ||||
-rw-r--r-- | xorg-server/os/utils.c | 28 | ||||
-rw-r--r-- | xorg-server/os/xdmcp.c | 4 |
4 files changed, 79 insertions, 9 deletions
diff --git a/xorg-server/os/connection.c b/xorg-server/os/connection.c index 7ff44e175..c36b125fe 100644 --- a/xorg-server/os/connection.c +++ b/xorg-server/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)); ListenTransConns = - (XtransConnInfo *) realloc(ListenTransConns, - (ListenTransCount + - 1) * sizeof(XtransConnInfo)); + xnfreallocarray(ListenTransConns, ListenTransCount + 1, + sizeof(XtransConnInfo)); /* Store it */ ListenTransConns[ListenTransCount] = ciptr; diff --git a/xorg-server/os/reallocarray.c b/xorg-server/os/reallocarray.c new file mode 100644 index 000000000..c415e09af --- /dev/null +++ b/xorg-server/os/reallocarray.c @@ -0,0 +1,43 @@ +/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ +/* + * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef HAVE_DIX_CONFIG_H +#include <dix-config.h> +#endif + +#include <sys/types.h> +#include <errno.h> +#include <stdint.h> +#include <stdlib.h> +#include "os.h" + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void * +reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} diff --git a/xorg-server/os/utils.c b/xorg-server/os/utils.c index 75769f17c..7fd395b2a 100644 --- a/xorg-server/os/utils.c +++ b/xorg-server/os/utils.c @@ -1128,10 +1128,20 @@ XNFalloc(unsigned long amount) return ptr; } +/* The original XNFcalloc was used with the xnfcalloc macro which multiplied + * the arguments at the call site without allowing calloc to check for overflow. + * XNFcallocarray was added to fix that without breaking ABI. + */ void * XNFcalloc(unsigned long amount) { - void *ret = calloc(1, amount); + return XNFcallocarray(1, amount); +} + +void * +XNFcallocarray(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); if (!ret) FatalError("XNFcalloc: Out of memory"); @@ -1148,6 +1158,16 @@ XNFrealloc(void *ptr, unsigned long amount) return ret; } +void * +XNFreallocarray(void *ptr, size_t nmemb, size_t size) +{ + void *ret = reallocarray(ptr, nmemb, size); + + if (!ret) + FatalError("XNFreallocarray: Out of memory"); + return ret; +} + char * Xstrdup(const char *s) { @@ -1640,7 +1660,7 @@ Fclose(void *iop) #include <X11/Xwindows.h> const char * -Win32TempDir() +Win32TempDir(void) { static char buffer[PATH_MAX]; @@ -1981,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)); if (!nlist) goto error; list = nlist; @@ -2091,6 +2111,7 @@ FormatUInt64Hex(uint64_t num, char *string) string[len] = '\0'; } +#if !defined(WIN32) || defined(__CYGWIN__) /* Move a file descriptor out of the way of our select mask; this * is useful for file descriptors which will never appear in the * select mask to avoid reducing the number of clients that can @@ -2114,3 +2135,4 @@ os_move_fd(int fd) close(fd); return newfd; } +#endif diff --git a/xorg-server/os/xdmcp.c b/xorg-server/os/xdmcp.c index bc5a70706..b265db338 100644 --- a/xorg-server/os/xdmcp.c +++ b/xorg-server/os/xdmcp.c @@ -19,6 +19,10 @@ #ifdef WIN32 #include <X11/Xwinsock.h> +#define XSERV_t +#define TRANS_SERVER +#define TRANS_REOPEN +#include <X11/Xtrans/Xtrans.h> #endif #include <X11/Xos.h> |