xquartz: use calloc() instead of malloc()

Using calloc() instead of malloc() as preventive measure, so there
never can be any hidden bugs or leaks due uninitialized memory.

The extra cost of using this compiler intrinsic should be practically
impossible to measure - in many cases a good compiler can even deduce
if certain areas really don't need to be zero'd (because they're written
to right after allocation) and create more efficient machine code.

The code pathes in question are pretty cold anyways, so it's probably
not worth even thinking about potential extra runtime costs.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 20:18:42 +02:00
parent 71a2617c28
commit 87c5b5bf42
13 changed files with 29 additions and 47 deletions

View File

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

View File

@ -44,21 +44,12 @@
#if defined(IN_MINI_GLX)
#include <stdlib.h>
#include <string.h>
#define _mesa_malloc(b) malloc(b)
#define _mesa_free(m) free(m)
#define _mesa_memset memset
#else
#ifdef XFree86Server
#include <os.h>
#include <string.h>
#define _mesa_malloc(b) malloc(b)
#define _mesa_free(m) free(m)
#define _mesa_memset memset
#else
#include <X11/Xlibint.h>
#define _mesa_memset memset
#define _mesa_malloc(b) Xmalloc(b)
#define _mesa_free(m) free(m)
#endif /* XFree86Server */
#endif /* !defined(IN_MINI_GLX) */
@ -129,7 +120,7 @@ _gl_copy_visual_to_context_mode(__GLcontextModes * mode,
{
__GLcontextModes * const next = mode->next;
(void)_mesa_memset(mode, 0, sizeof(__GLcontextModes));
(void)memset(mode, 0, sizeof(__GLcontextModes));
mode->next = next;
mode->visualID = config->vid;
@ -431,14 +422,14 @@ _gl_context_modes_create(unsigned count, size_t minimum_size)
next = &base;
for (i = 0; i < count; i++) {
*next = (__GLcontextModes *)_mesa_malloc(size);
*next = calloc(1, size);
if (*next == NULL) {
_gl_context_modes_destroy(base);
base = NULL;
break;
}
(void)_mesa_memset(*next, 0, size);
(void)memset(*next, 0, size);
(*next)->visualID = GLX_DONT_CARE;
(*next)->visualType = GLX_DONT_CARE;
(*next)->visualRating = GLX_NONE;
@ -476,7 +467,7 @@ _gl_context_modes_destroy(__GLcontextModes * modes)
while (modes != NULL) {
__GLcontextModes * const next = modes->next;
_mesa_free(modes);
free(modes);
modes = next;
}
}

View File

@ -549,10 +549,7 @@ __glXAquaScreenCreateDrawable(ClientPtr client,
XID glxDrawId,
__GLXconfig *conf)
{
__GLXAquaDrawable *glxPriv;
glxPriv = malloc(sizeof *glxPriv);
__GLXAquaDrawable *glxPriv = calloc(1, sizeof *glxPriv);
if (glxPriv == NULL)
return NULL;

View File

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

View File

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

View File

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

View File

@ -321,11 +321,9 @@ static int launchd_socket_handed_off = 0;
kern_return_t
do_request_fd_handoff_socket(mach_port_t port, string_t filename)
{
socket_handoff_t *handoff_data;
launchd_socket_handed_off = 1;
handoff_data = (socket_handoff_t *)calloc(1, sizeof(socket_handoff_t));
socket_handoff_t *handoff_data = calloc(1, sizeof(socket_handoff_t));
if (!handoff_data) {
ErrorF("X11.app: Error allocating memory for handoff_data\n");
return KERN_FAILURE;
@ -528,7 +526,6 @@ setup_console_redirect(const char *bundle_id)
static void
setup_env(void)
{
char *temp;
const char *pds = NULL;
const char *disp = getenv("DISPLAY");
size_t len;
@ -559,7 +556,7 @@ setup_env(void)
setenv("X11_PREFS_DOMAIN", server_bootstrap_name, 1);
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) {
ErrorF("X11.app: Memory allocation error.\n");
exit(1);
@ -582,7 +579,7 @@ setup_env(void)
"X11.app: Detected old style launchd DISPLAY, please update xinit.\n");
}
else {
temp = (char *)malloc(sizeof(char) * len);
char *temp = calloc(len, sizeof(char));
if (!temp) {
ErrorF(
"X11.app: Memory allocation error creating space for socket name test.\n");
@ -613,7 +610,7 @@ setup_env(void)
ensure_path(X11BINDIR);
/* cd $HOME */
temp = getenv("HOME");
char *temp = getenv("HOME");
if (temp != NULL && temp[0] != '\0')
chdir(temp);
}
@ -781,14 +778,14 @@ command_from_prefs(const char *key, const char *default_value)
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
CFRelease(cfDefaultValue);
command = (char *)malloc(len * sizeof(char));
command = calloc(len, sizeof(char));
if (!command)
goto command_from_prefs_out;
strcpy(command, default_value);
}
else {
int len = CFStringGetLength((CFStringRef)PlistRef) + 1;
command = (char *)malloc(len * sizeof(char));
command = calloc(len, sizeof(char));
if (!command)
goto command_from_prefs_out;
CFStringGetCString((CFStringRef)PlistRef, command, len,

View File

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

View File

@ -81,7 +81,7 @@ create_thread(void *func, void *arg)
void
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)
FatalError("Could not allocate memory.\n");

View File

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

View File

@ -90,7 +90,7 @@ X_PFX(list_prepend) (x_list * lst, void *data) {
x_list_block *b;
int i;
b = malloc(sizeof(x_list_block));
b = calloc(1, sizeof(x_list_block));
assert(b != NULL);
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;
unsigned i;
rowbytes = src->bits->width * sizeof(CARD32);
data = malloc(rowbytes * src->bits->height);
data = calloc(src->bits->height, rowbytes);
free_data = TRUE;
if (!data) {
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 */
rowbytes = ((src->bits->width * 4) + 31) & ~31;
data = malloc(rowbytes * src->bits->height);
data = calloc(src->bits->height, rowbytes);
free_data = TRUE;
if (!data) {
FatalError("Failed to allocate memory in %s\n", __func__);

View File

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