From 085919667b93d2b63a7200e491e6a3d6f052b62a Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:17:36 +0200 Subject: [PATCH] glx: 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 --- glx/single2.c | 8 ++++---- glx/vndcmds.c | 3 +-- glx/xfont.c | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/glx/single2.c b/glx/single2.c index 18fd2c1bc..2848f73c5 100644 --- a/glx/single2.c +++ b/glx/single2.c @@ -272,15 +272,15 @@ __glXcombine_strings(const char *cext_string, const char *sext_string) clen = strlen(cext_string); slen = strlen(sext_string); if (clen > slen) { - combo_string = (char *) malloc(slen + 2); - s1 = (char *) malloc(slen + 2); + combo_string = (char *) calloc(1, slen + 2); + s1 = (char *) calloc(1, slen + 2); if (s1) strcpy(s1, sext_string); s2 = cext_string; } else { - combo_string = (char *) malloc(clen + 2); - s1 = (char *) malloc(clen + 2); + combo_string = (char *) calloc(1, clen + 2); + s1 = (char *) calloc(1, clen + 2); if (s1) strcpy(s1, cext_string); s2 = sext_string; diff --git a/glx/vndcmds.c b/glx/vndcmds.c index 4ffa5f19f..3f9587a88 100644 --- a/glx/vndcmds.c +++ b/glx/vndcmds.c @@ -126,7 +126,6 @@ static int dispatch_GLXQueryVersion(ClientPtr client) static int dispatch_GLXClientInfo(ClientPtr client) { GlxServerVendor *vendor; - void *requestCopy = NULL; size_t requestSize = client->req_len * 4; if (client->minorOp == X_GLXClientInfo) { @@ -142,7 +141,7 @@ static int dispatch_GLXClientInfo(ClientPtr client) // We'll forward this request to each vendor library. Since a vendor might // modify the request data in place (e.g., for byte swapping), make a copy // of the request first. - requestCopy = malloc(requestSize); + void *requestCopy = calloc(1, requestSize); if (requestCopy == NULL) { return BadAlloc; } diff --git a/glx/xfont.c b/glx/xfont.c index 3e84369e8..05927f76c 100644 --- a/glx/xfont.c +++ b/glx/xfont.c @@ -64,7 +64,7 @@ __glXMakeBitmapFromGlyph(FontPtr font, CharInfoPtr pci) widthPadded = GLYPHWIDTHBYTESPADDED(pci); /* - ** Use the local buf if possible, otherwise malloc. + ** Use the local buf if possible, otherwise calloc. */ allocBytes = widthPadded * h; if (allocBytes <= __GL_CHAR_BUF_SIZE) { @@ -72,7 +72,7 @@ __glXMakeBitmapFromGlyph(FontPtr font, CharInfoPtr pci) allocbuf = 0; } else { - p = (unsigned char *) malloc(allocBytes); + p = calloc(1, allocBytes); if (!p) return BadAlloc; allocbuf = p;