kdrive: 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 19:57:07 +02:00
parent e87d41a91d
commit 30d65cd9f1
4 changed files with 6 additions and 10 deletions

View File

@ -76,9 +76,7 @@ ephyrInitialize(KdCardInfo * card, EphyrPriv * priv)
Bool Bool
ephyrCardInit(KdCardInfo * card) ephyrCardInit(KdCardInfo * card)
{ {
EphyrPriv *priv; EphyrPriv *priv = calloc(1, sizeof(EphyrPriv));
priv = (EphyrPriv *) malloc(sizeof(EphyrPriv));
if (!priv) if (!priv)
return FALSE; return FALSE;

View File

@ -144,11 +144,10 @@ ephyr_glamor_build_glsl_prog(GLuint vs, GLuint fs)
glLinkProgram(prog); glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &ok); glGetProgramiv(prog, GL_LINK_STATUS, &ok);
if (!ok) { if (!ok) {
GLchar *info;
GLint size; GLint size;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &size); glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &size);
info = malloc(size); GLchar *info = calloc(1, size);
glGetProgramInfoLog(prog, size, NULL, info); glGetProgramInfoLog(prog, size, NULL, info);
ErrorF("Failed to link: %s\n", info); ErrorF("Failed to link: %s\n", info);
@ -352,7 +351,7 @@ ephyr_glamor_screen_init(xcb_window_t win, xcb_visualid_t vid)
glamor = calloc(1, sizeof(struct ephyr_glamor)); glamor = calloc(1, sizeof(struct ephyr_glamor));
if (!glamor) { if (!glamor) {
FatalError("malloc"); FatalError("calloc");
return NULL; return NULL;
} }

View File

@ -533,7 +533,6 @@ hostx_init(void)
uint32_t pixel; uint32_t pixel;
int index; int index;
char *tmpstr; char *tmpstr;
char *class_hint;
size_t class_len; size_t class_len;
xcb_screen_t *xscreen; xcb_screen_t *xscreen;
xcb_rectangle_t rect = { 0, 0, 1, 1 }; xcb_rectangle_t rect = { 0, 0, 1, 1 };
@ -658,7 +657,7 @@ hostx_init(void)
if (tmpstr && (!ephyrResNameFromCmd)) if (tmpstr && (!ephyrResNameFromCmd))
ephyrResName = tmpstr; ephyrResName = tmpstr;
class_len = strlen(ephyrResName) + 1 + strlen("Xephyr") + 1; class_len = strlen(ephyrResName) + 1 + strlen("Xephyr") + 1;
class_hint = malloc(class_len); char *class_hint = calloc(1, class_len);
if (class_hint) { if (class_hint) {
strcpy(class_hint, ephyrResName); strcpy(class_hint, ephyrResName);
strcpy(class_hint + strlen(ephyrResName) + 1, "Xephyr"); strcpy(class_hint + strlen(ephyrResName) + 1, "Xephyr");

View File

@ -135,7 +135,7 @@ KdXVScreenInit(ScreenPtr pScreen, KdVideoAdaptorPtr adaptors, int num)
KdXvScreenKey = XvGetScreenKey(); KdXvScreenKey = XvGetScreenKey();
PortResource = XvGetRTPort(); PortResource = XvGetRTPort();
ScreenPriv = malloc(sizeof(KdXVScreenRec)); ScreenPriv = calloc(1, sizeof(KdXVScreenRec));
dixSetPrivate(&pScreen->devPrivates, &KdXVScreenPrivateKey, ScreenPriv); dixSetPrivate(&pScreen->devPrivates, &KdXVScreenPrivateKey, ScreenPriv);
if (!ScreenPriv) if (!ScreenPriv)
@ -723,7 +723,7 @@ KdXVEnlistPortInWindow(WindowPtr pWin, XvPortRecPrivatePtr portPriv)
} }
if (!winPriv) { if (!winPriv) {
winPriv = malloc(sizeof(KdXVWindowRec)); winPriv = calloc(1, sizeof(KdXVWindowRec));
if (!winPriv) if (!winPriv)
return BadAlloc; return BadAlloc;
winPriv->PortRec = portPriv; winPriv->PortRec = portPriv;