xquartz: replace malloc() by calloc()

It's in general safer to clear out all the memory on allocation,
in order to prevent potential leaks or undefined behaviour on
uninitialized data.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-07-04 14:21:51 +02:00
parent 160ab343ea
commit 5d30f77db2
13 changed files with 23 additions and 33 deletions

View File

@ -549,7 +549,7 @@ getGlCapabilities(struct glCapabilities *cap)
continue; continue;
} }
conf = malloc(sizeof(*conf)); conf = calloc(1, sizeof(*conf));
if (NULL == conf) { if (NULL == conf) {
FatalError("Unable to allocate memory for OpenGL capabilities\n"); FatalError("Unable to allocate memory for OpenGL capabilities\n");
} }

View File

@ -44,20 +44,17 @@
#if defined(IN_MINI_GLX) #if defined(IN_MINI_GLX)
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define _mesa_malloc(b) malloc(b)
#define _mesa_free(m) free(m) #define _mesa_free(m) free(m)
#define _mesa_memset memset #define _mesa_memset memset
#else #else
#ifdef XFree86Server #ifdef XFree86Server
#include <os.h> #include <os.h>
#include <string.h> #include <string.h>
#define _mesa_malloc(b) malloc(b)
#define _mesa_free(m) free(m) #define _mesa_free(m) free(m)
#define _mesa_memset memset #define _mesa_memset memset
#else #else
#include <X11/Xlibint.h> #include <X11/Xlibint.h>
#define _mesa_memset memset #define _mesa_memset memset
#define _mesa_malloc(b) Xmalloc(b)
#define _mesa_free(m) free(m) #define _mesa_free(m) free(m)
#endif /* XFree86Server */ #endif /* XFree86Server */
#endif /* !defined(IN_MINI_GLX) */ #endif /* !defined(IN_MINI_GLX) */
@ -431,14 +428,13 @@ _gl_context_modes_create(unsigned count, size_t minimum_size)
next = &base; next = &base;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
*next = (__GLcontextModes *)_mesa_malloc(size); *next = calloc(1, size);
if (*next == NULL) { if (*next == NULL) {
_gl_context_modes_destroy(base); _gl_context_modes_destroy(base);
base = NULL; base = NULL;
break; break;
} }
(void)_mesa_memset(*next, 0, size);
(*next)->visualID = GLX_DONT_CARE; (*next)->visualID = GLX_DONT_CARE;
(*next)->visualType = GLX_DONT_CARE; (*next)->visualType = GLX_DONT_CARE;
(*next)->visualRating = GLX_NONE; (*next)->visualRating = GLX_NONE;

View File

@ -551,7 +551,7 @@ __glXAquaScreenCreateDrawable(ClientPtr client,
{ {
__GLXAquaDrawable *glxPriv; __GLXAquaDrawable *glxPriv;
glxPriv = malloc(sizeof *glxPriv); glxPriv = calloc(1, sizeof *glxPriv);
if (glxPriv == NULL) if (glxPriv == NULL)
return NULL; return NULL;

View File

@ -246,7 +246,7 @@ ProcAppleWMSelectInput(register ClientPtr client)
} }
/* build the entry */ /* build the entry */
pNewEvent = (WMEventPtr)malloc(sizeof(WMEventRec)); pNewEvent = calloc(1, sizeof(WMEventRec));
if (!pNewEvent) if (!pNewEvent)
return BadAlloc; return BadAlloc;
pNewEvent->next = 0; pNewEvent->next = 0;
@ -267,7 +267,7 @@ ProcAppleWMSelectInput(register ClientPtr client)
* done through the resource database. * done through the resource database.
*/ */
if (i != Success || !pHead) { if (i != Success || !pHead) {
pHead = (WMEventPtr *)malloc(sizeof(WMEventPtr)); pHead = calloc(1, sizeof(WMEventPtr));
if (!pHead || if (!pHead ||
!AddResource(eventResource, EventType, (void *)pHead)) { !AddResource(eventResource, EventType, (void *)pHead)) {
FreeResource(clientResource, X11_RESTYPE_NONE); FreeResource(clientResource, X11_RESTYPE_NONE);
@ -368,16 +368,15 @@ ProcAppleWMReenableUpdate(register ClientPtr client)
static int static int
ProcAppleWMSetWindowMenu(register ClientPtr client) ProcAppleWMSetWindowMenu(register ClientPtr client)
{ {
const char *bytes, **items; const char *bytes;
char *shortcuts;
int max_len, nitems, i, j; int max_len, nitems, i, j;
REQUEST(xAppleWMSetWindowMenuReq); REQUEST(xAppleWMSetWindowMenuReq);
REQUEST_AT_LEAST_SIZE(xAppleWMSetWindowMenuReq); REQUEST_AT_LEAST_SIZE(xAppleWMSetWindowMenuReq);
nitems = stuff->nitems; nitems = stuff->nitems;
items = malloc(sizeof(char *) * nitems); char **items = calloc(nitems, sizeof(char *));
shortcuts = malloc(sizeof(char) * nitems); char *shortcuts = calloc(nitems, sizeof(char));
if (!items || !shortcuts) { if (!items || !shortcuts) {
free(items); free(items);

View File

@ -188,7 +188,6 @@ DarwinScreenInit(ScreenPtr pScreen, int argc, char **argv)
int dpi; int dpi;
static int foundIndex = 0; static int foundIndex = 0;
Bool ret; Bool ret;
DarwinFramebufferPtr dfb;
if (!dixRegisterPrivateKey(&darwinScreenKeyRec, PRIVATE_SCREEN, 0)) if (!dixRegisterPrivateKey(&darwinScreenKeyRec, PRIVATE_SCREEN, 0))
return FALSE; return FALSE;
@ -202,7 +201,7 @@ DarwinScreenInit(ScreenPtr pScreen, int argc, char **argv)
} }
// allocate space for private per screen storage // allocate space for private per screen storage
dfb = malloc(sizeof(DarwinFramebufferRec)); DarwinFramebufferPtr dfb = calloc(1, sizeof(DarwinFramebufferRec));
// SCREEN_PRIV(pScreen) = dfb; // SCREEN_PRIV(pScreen) = dfb;
dixSetPrivate(&pScreen->devPrivates, darwinScreenKey, dfb); dixSetPrivate(&pScreen->devPrivates, darwinScreenKey, dfb);

View File

@ -879,7 +879,7 @@ ucs2keysym(long ucs)
int mid; int mid;
if (reverse_keysymtab == NULL) { if (reverse_keysymtab == NULL) {
reverse_keysymtab = malloc(sizeof(keysymtab)); reverse_keysymtab = calloc(1, sizeof(keysymtab));
memcpy(reverse_keysymtab, keysymtab, sizeof(keysymtab)); memcpy(reverse_keysymtab, keysymtab, sizeof(keysymtab));
qsort(reverse_keysymtab, qsort(reverse_keysymtab,

View File

@ -528,7 +528,6 @@ setup_console_redirect(const char *bundle_id)
static void static void
setup_env(void) setup_env(void)
{ {
char *temp;
const char *pds = NULL; const char *pds = NULL;
const char *disp = getenv("DISPLAY"); const char *disp = getenv("DISPLAY");
size_t len; size_t len;
@ -559,7 +558,7 @@ setup_env(void)
setenv("X11_PREFS_DOMAIN", server_bootstrap_name, 1); setenv("X11_PREFS_DOMAIN", server_bootstrap_name, 1);
len = strlen(server_bootstrap_name); len = strlen(server_bootstrap_name);
bundle_id_prefix = malloc(sizeof(char) * (len - 3)); bundle_id_prefix = calloc((len-3), sizeof(char));
if (!bundle_id_prefix) { if (!bundle_id_prefix) {
ErrorF("X11.app: Memory allocation error.\n"); ErrorF("X11.app: Memory allocation error.\n");
exit(1); exit(1);
@ -582,7 +581,7 @@ setup_env(void)
"X11.app: Detected old style launchd DISPLAY, please update xinit.\n"); "X11.app: Detected old style launchd DISPLAY, please update xinit.\n");
} }
else { else {
temp = (char *)malloc(sizeof(char) * len); char *temp = calloc(len, sizeof(char));
if (!temp) { if (!temp) {
ErrorF( ErrorF(
"X11.app: Memory allocation error creating space for socket name test.\n"); "X11.app: Memory allocation error creating space for socket name test.\n");
@ -613,7 +612,7 @@ setup_env(void)
ensure_path(X11BINDIR); ensure_path(X11BINDIR);
/* cd $HOME */ /* cd $HOME */
temp = getenv("HOME"); char *temp = getenv("HOME");
if (temp != NULL && temp[0] != '\0') if (temp != NULL && temp[0] != '\0')
chdir(temp); chdir(temp);
} }
@ -781,14 +780,14 @@ command_from_prefs(const char *key, const char *default_value)
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication); CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
CFRelease(cfDefaultValue); CFRelease(cfDefaultValue);
command = (char *)malloc(len * sizeof(char)); command = calloc(len, sizeof(char));
if (!command) if (!command)
goto command_from_prefs_out; goto command_from_prefs_out;
strcpy(command, default_value); strcpy(command, default_value);
} }
else { else {
int len = CFStringGetLength((CFStringRef)PlistRef) + 1; int len = CFStringGetLength((CFStringRef)PlistRef) + 1;
command = (char *)malloc(len * sizeof(char)); command = calloc(len, sizeof(char));
if (!command) if (!command)
goto command_from_prefs_out; goto command_from_prefs_out;
CFStringGetCString((CFStringRef)PlistRef, command, len, CFStringGetCString((CFStringRef)PlistRef, command, len,

View File

@ -532,7 +532,7 @@ QuartzCopyDisplayIDs(ScreenPtr pScreen,
free(pQuartzScreen->displayIDs); free(pQuartzScreen->displayIDs);
if (displayCount) { if (displayCount) {
size_t size = displayCount * sizeof(CGDirectDisplayID); size_t size = displayCount * sizeof(CGDirectDisplayID);
pQuartzScreen->displayIDs = malloc(size); pQuartzScreen->displayIDs = calloc(1, size);
memcpy(pQuartzScreen->displayIDs, displayIDs, size); memcpy(pQuartzScreen->displayIDs, displayIDs, size);
} }
else { else {

View File

@ -81,7 +81,7 @@ create_thread(void *func, void *arg)
void void
QuartzInitServer(int argc, char **argv, char **envp) QuartzInitServer(int argc, char **argv, char **envp)
{ {
struct arg *args = (struct arg *)malloc(sizeof(struct arg)); struct arg *args = calloc(1, sizeof(struct arg));
if (!args) if (!args)
FatalError("Could not allocate memory.\n"); FatalError("Could not allocate memory.\n");

View File

@ -265,7 +265,7 @@ CreateSurfaceForWindow(ScreenPtr pScreen, WindowPtr pWin,
xp_window_changes wc; xp_window_changes wc;
/* allocate a DRI Window Private record */ /* allocate a DRI Window Private record */
if (!(pDRIDrawablePriv = malloc(sizeof(*pDRIDrawablePriv)))) { if (!(pDRIDrawablePriv = calloc(1, sizeof(*pDRIDrawablePriv)))) {
return NULL; return NULL;
} }
@ -681,7 +681,6 @@ DRICreatePixmap(ScreenPtr pScreen, Drawable id,
DrawablePtr pDrawable, char *path, DrawablePtr pDrawable, char *path,
size_t pathmax) size_t pathmax)
{ {
DRIPixmapBufferPtr shared;
PixmapPtr pPix; PixmapPtr pPix;
if (pDrawable->type != DRAWABLE_PIXMAP) if (pDrawable->type != DRAWABLE_PIXMAP)
@ -689,7 +688,7 @@ DRICreatePixmap(ScreenPtr pScreen, Drawable id,
pPix = (PixmapPtr)pDrawable; pPix = (PixmapPtr)pDrawable;
shared = malloc(sizeof(*shared)); DRIPixmapBufferPtr shared = calloc(1, sizeof(*shared));
if (NULL == shared) { if (NULL == shared) {
FatalError("failed to allocate DRIPixmapBuffer in %s\n", __func__); FatalError("failed to allocate DRIPixmapBuffer in %s\n", __func__);
} }

View File

@ -87,10 +87,9 @@ X_PFX(list_prepend) (x_list * lst, void *data) {
pthread_mutex_lock(&freelist_lock); pthread_mutex_lock(&freelist_lock);
if (freelist == NULL) { if (freelist == NULL) {
x_list_block *b;
int i; int i;
b = malloc(sizeof(x_list_block)); x_list_block *b = calloc(1, sizeof(x_list_block));
assert(b != NULL); assert(b != NULL);
for (i = 0; i < NODES_PER_BLOCK - 1; i++) for (i = 0; i < NODES_PER_BLOCK - 1; i++)

View File

@ -93,7 +93,7 @@ load_cursor(CursorPtr src, int screen)
const uint32_t *be_data = (uint32_t *)src->bits->argb; const uint32_t *be_data = (uint32_t *)src->bits->argb;
unsigned i; unsigned i;
rowbytes = src->bits->width * sizeof(CARD32); rowbytes = src->bits->width * sizeof(CARD32);
data = malloc(rowbytes * src->bits->height); data = calloc(rowbytes, src->bits->height);
free_data = TRUE; free_data = TRUE;
if (!data) { if (!data) {
FatalError("Failed to allocate memory in %s\n", __func__); FatalError("Failed to allocate memory in %s\n", __func__);
@ -119,7 +119,7 @@ load_cursor(CursorPtr src, int screen)
/* round up to 8 pixel boundary so we can convert whole bytes */ /* round up to 8 pixel boundary so we can convert whole bytes */
rowbytes = ((src->bits->width * 4) + 31) & ~31; rowbytes = ((src->bits->width * 4) + 31) & ~31;
data = malloc(rowbytes * src->bits->height); data = calloc(rowbytes, src->bits->height);
free_data = TRUE; free_data = TRUE;
if (!data) { if (!data) {
FatalError("Failed to allocate memory in %s\n", __func__); FatalError("Failed to allocate memory in %s\n", __func__);

View File

@ -199,7 +199,6 @@ xprAddPseudoramiXScreens(int *x, int *y, int *width, int *height,
ScreenPtr pScreen) ScreenPtr pScreen)
{ {
CGDisplayCount i, displayCount; CGDisplayCount i, displayCount;
CGDirectDisplayID *displayList = NULL;
CGRect unionRect = CGRectNull, frame; CGRect unionRect = CGRectNull, frame;
// Find all the CoreGraphics displays // Find all the CoreGraphics displays
@ -224,7 +223,7 @@ xprAddPseudoramiXScreens(int *x, int *y, int *width, int *height,
if (CGDisplayIsCaptured(kCGDirectMainDisplay)) if (CGDisplayIsCaptured(kCGDirectMainDisplay))
displayCount = 1; displayCount = 1;
displayList = malloc(displayCount * sizeof(CGDirectDisplayID)); CGDirectDisplayID *displayList = calloc(displayCount, sizeof(CGDirectDisplayID));
if (!displayList) if (!displayList)
FatalError("Unable to allocate memory for list of displays.\n"); FatalError("Unable to allocate memory for list of displays.\n");
CGGetActiveDisplayList(displayCount, displayList, &displayCount); CGGetActiveDisplayList(displayCount, displayList, &displayCount);