From a31c51f337c238c12d007bb4694b949b423677e8 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 6 Nov 2025 16:35:53 +0100 Subject: [PATCH] xfree86: common: replace XNFasprintf() by standard libc asprintf() Signed-off-by: Enrico Weigelt, metux IT consult --- hw/xfree86/common/xf86Config.c | 6 ++-- hw/xfree86/common/xf86Configure.c | 40 +++++++++++++-------- hw/xfree86/common/xf86pciBus.c | 7 ++-- hw/xfree86/common/xf86platformBus.c | 55 +++++++++++++++++------------ hw/xfree86/common/xf86sbusBus.c | 6 ++-- 5 files changed, 68 insertions(+), 46 deletions(-) diff --git a/hw/xfree86/common/xf86Config.c b/hw/xfree86/common/xf86Config.c index 79d46569a7..ba2ed4fa19 100644 --- a/hw/xfree86/common/xf86Config.c +++ b/hw/xfree86/common/xf86Config.c @@ -206,12 +206,12 @@ xf86ValidateFontPath(char *path) continue; } else { - XNFasprintf(&p1, "%s%s", dir_elem, DIR_FILE); - flag = stat(p1, &stat_buf); + char pbuf1[PATH_MAX] = { 0 }; + snprintf(pbuf1, sizeof(pbuf1)-1, "%s%s", dir_elem, DIR_FILE); + flag = stat(pbuf1, &stat_buf); if (flag == 0) if (!S_ISREG(stat_buf.st_mode)) flag = -1; - free(p1); if (flag != 0) { LogMessageVerb(X_WARNING, 1, "`fonts.dir' not found (or not valid) in \"%s\".\n", diff --git a/hw/xfree86/common/xf86Configure.c b/hw/xfree86/common/xf86Configure.c index 9d1d333fb9..9089372761 100644 --- a/hw/xfree86/common/xf86Configure.c +++ b/hw/xfree86/common/xf86Configure.c @@ -203,14 +203,17 @@ configureScreenSection(int screennum) { int i; int depths[] = { 1, 4, 8, 15, 16, 24 /*, 32 */ }; - char *tmp; + char *tmp = NULL; parsePrologue(XF86ConfScreenPtr, XF86ConfScreenRec); - XNFasprintf(&tmp, "Screen%d", screennum); + if (asprintf(&tmp, "Screen%d", screennum) == -1) + return NULL; ptr->scrn_identifier = tmp; - XNFasprintf(&tmp, "Monitor%d", screennum); + if (asprintf(&tmp, "Monitor%d", screennum) == -1) + return NULL; ptr->scrn_monitor_str = tmp; - XNFasprintf(&tmp, "Card%d", screennum); + if (asprintf(&tmp, "Card%d", screennum) == -1) + return NULL; ptr->scrn_device_str = tmp; for (i = 0; i < ARRAY_SIZE(depths); i++) { @@ -366,7 +369,7 @@ configureLayoutSection(void) } for (scrnum = 0; scrnum < nDevToConfig; scrnum++) { - char *tmp; + char *tmp = NULL; XF86ConfAdjacencyPtr aptr = calloc(1, sizeof(XF86ConfAdjacencyRec)); assert(aptr); @@ -374,16 +377,17 @@ configureLayoutSection(void) aptr->adj_x = 0; aptr->adj_y = 0; aptr->adj_scrnum = scrnum; - XNFasprintf(&tmp, "Screen%d", scrnum); - aptr->adj_screen_str = tmp; + if (asprintf(&tmp, "Screen%d", scrnum) != -1) + aptr->adj_screen_str = tmp; if (scrnum == 0) { aptr->adj_where = CONF_ADJ_ABSOLUTE; aptr->adj_refscreen = NULL; } else { aptr->adj_where = CONF_ADJ_RIGHTOF; - XNFasprintf(&tmp, "Screen%d", scrnum - 1); - aptr->adj_refscreen = tmp; + tmp = NULL; + if (asprintf(&tmp, "Screen%d", scrnum - 1) != -1) + aptr->adj_refscreen = tmp; } ptr->lay_adjacency_lst = (XF86ConfAdjacencyPtr) xf86addListItem((glp) ptr->lay_adjacency_lst, @@ -441,10 +445,11 @@ configureFilesSection(void) static XF86ConfMonitorPtr configureMonitorSection(int screennum) { - char *tmp; + char *tmp = NULL; parsePrologue(XF86ConfMonitorPtr, XF86ConfMonitorRec); - XNFasprintf(&tmp, "Monitor%d", screennum); + if (asprintf(&tmp, "Monitor%d", screennum) == -1) + return NULL; ptr->mon_identifier = tmp; ptr->mon_vendor = XNFstrdup("Monitor Vendor"); ptr->mon_modelname = XNFstrdup("Monitor Model"); @@ -490,10 +495,12 @@ configureDDCMonitorSection(int screennum) parsePrologue(XF86ConfMonitorPtr, XF86ConfMonitorRec); - XNFasprintf(&tmp, "Monitor%d", screennum); + if (asprintf(&tmp, "Monitor%d", screennum) == -1) + return NULL; ptr->mon_identifier = tmp; ptr->mon_vendor = XNFstrdup(ConfiguredMonitor->vendor.name); - XNFasprintf(&ptr->mon_modelname, "%x", ConfiguredMonitor->vendor.prod_id); + if (asprintf(&ptr->mon_modelname, "%x", ConfiguredMonitor->vendor.prod_id) == -1) + FatalError("malloc failed\n"); /* features in centimetres, we want millimetres */ mon_width = 10 * ConfiguredMonitor->features.hsize; @@ -864,8 +871,11 @@ DoShowOptions(void) xf86DriverList[i]->driverName); continue; } - XNFasprintf(&pSymbol, "%sModuleData", - xf86DriverList[i]->driverName); + if (asprintf(&pSymbol, "%sModuleData", + xf86DriverList[i]->driverName) == -1) { + ErrorF("(EE) malloc failed\n"); + continue; + } initData = LoaderSymbol(pSymbol); if (initData) { XF86ModuleVersionInfo *vers = initData->vers; diff --git a/hw/xfree86/common/xf86pciBus.c b/hw/xfree86/common/xf86pciBus.c index d13d15ad6c..697ffb6570 100644 --- a/hw/xfree86/common/xf86pciBus.c +++ b/hw/xfree86/common/xf86pciBus.c @@ -1427,14 +1427,15 @@ xf86PciConfigureNewDev(void *busData, struct pci_device *pVideo, GDevRec * GDev, int *chipset) { char busnum[8]; - char *tmp; + char *tmp = NULL; pVideo = (struct pci_device *) busData; snprintf(busnum, sizeof(busnum), "%d", pVideo->bus); - XNFasprintf(&tmp, "PCI:%s:%d:%d", - busnum, pVideo->dev, pVideo->func); + if (asprintf(&tmp, "PCI:%s:%d:%d", + busnum, pVideo->dev, pVideo->func) == -1) + FatalError("malloc failed\n"); GDev->busID = tmp; GDev->chipID = pVideo->device_id; diff --git a/hw/xfree86/common/xf86platformBus.c b/hw/xfree86/common/xf86platformBus.c index 6ef72c9e8d..4d0e78447c 100644 --- a/hw/xfree86/common/xf86platformBus.c +++ b/hw/xfree86/common/xf86platformBus.c @@ -255,8 +255,6 @@ xf86platformProbe(void) Bool pci = TRUE; XF86ConfOutputClassPtr cl, cl_head = (xf86configptr) ? xf86configptr->conf_outputclass_lst : NULL; - char *driver_path, *path = NULL; - char *curr, *next, *copy; config_odev_probe(xf86PlatformDeviceProbe); @@ -264,6 +262,7 @@ xf86platformProbe(void) pci = FALSE; } + char *path = NULL; for (i = 0; i < xf86_num_platform_devices; i++) { char *busid = xf86_platform_odev_attributes(i)->busid; @@ -281,18 +280,18 @@ xf86platformProbe(void) if (xf86ModPathFrom != X_CMDLINE) { if (cl->driver) { + char driver_path[PATH_MAX] = { 0 }; if (cl->modulepath) { if (*(cl->modulepath)) { - XNFasprintf(&driver_path, "%s,%s", cl->modulepath, xf86ModulePath); + snprintf(driver_path, sizeof(driver_path)-1, "%s,%s", cl->modulepath, xf86ModulePath); LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" ModulePath for driver %s overridden with \"%s\"\n", cl->identifier, cl->driver, driver_path); } else { - XNFasprintf(&driver_path, "%s", xf86ModulePath); + snprintf(driver_path, sizeof(driver_path)-1, "%s", xf86ModulePath); LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" ModulePath for driver %s reset to standard \"%s\"\n", cl->identifier, cl->driver, driver_path); } } else { - driver_path = NULL; LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" ModulePath for driver %s reset to default\n", cl->identifier, cl->driver); } @@ -300,51 +299,63 @@ xf86platformProbe(void) if (cl->modules) { LogMessageVerb(X_CONFIG, 1, " and for modules \"%s\" as well\n", cl->modules); - XNFasprintf(©, "%s", cl->modules); - curr = copy; + char *copy = strdup(cl->modules ? cl->modules : ""); + char *curr = copy; + char *next; while ((curr = strtok_r(curr, ",", &next))) { if (*curr) LoaderSetPath(curr, driver_path); curr = NULL; } free(copy); } - free(driver_path); } else if (cl->modules) { + char driver_path[PATH_MAX] = { 0 }; if (cl->modulepath) { if (*(cl->modulepath)) { - XNFasprintf(&driver_path, "%s,%s", cl->modulepath, xf86ModulePath); + snprintf(driver_path, sizeof(driver_path)-1, "%s,%s", cl->modulepath, xf86ModulePath); LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" ModulePath for modules %s overridden with \"%s\"\n", cl->identifier, cl->modules, driver_path); } else { - XNFasprintf(&driver_path, "%s", xf86ModulePath); + snprintf(driver_path, sizeof(driver_path)-1, "%s", xf86ModulePath); LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" ModulePath for modules %s reset to standard \"%s\"\n", cl->identifier, cl->modules, driver_path); } } else { - driver_path = NULL; LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" ModulePath for modules %s reset to default\n", cl->identifier, cl->modules); } - XNFasprintf(©, "%s", cl->modules); - curr = copy; + char *copy = strdup(cl->modules ? cl->modules : ""); + char *curr = copy; + char *next; while ((curr = strtok_r(curr, ",", &next))) { if (*curr) LoaderSetPath(curr, driver_path); curr = NULL; } free(copy); } else { - driver_path = path; /* Reuse for temporary storage */ - if (*(cl->modulepath)) { - XNFasprintf(&path, "%s,%s", cl->modulepath, - path ? path : xf86ModulePath); + if (*(cl->modulepath)) { + char *path2; + if (asprintf(&path2, "%s,%s", cl->modulepath, + path ? path : xf86ModulePath) == -1) + LogMessageVerb(X_ERROR, 1, "memory allocation failed\n"); + else { LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" default ModulePath extended to \"%s\"\n", - cl->identifier, path); - } else { - XNFasprintf(&path, "%s", xf86ModulePath); - LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" default ModulePath reset to standard \"%s\"\n", - cl->identifier, path); + cl->identifier, path2); + free(path); + path = path2; } + } else { + char *path2 = strdup(xf86ModulePath); + if (!path2) + LogMessageVerb(X_ERROR, 1, "memory allocation failed\n"); + else { + LogMessageVerb(X_CONFIG, 1, "OutputClass \"%s\" default ModulePath reset to standard \"%s\"\n", + cl->identifier, path2); + free(path); + path = path2; + } + } } /* Otherwise global module search path is left unchanged */ } diff --git a/hw/xfree86/common/xf86sbusBus.c b/hw/xfree86/common/xf86sbusBus.c index 1fa9a3f9fc..adda4a22b7 100644 --- a/hw/xfree86/common/xf86sbusBus.c +++ b/hw/xfree86/common/xf86sbusBus.c @@ -714,7 +714,7 @@ void xf86SbusConfigureNewDev(void *busData, sbusDevicePtr sBus, GDevRec * GDev) { char *promPath = NULL; - char *tmp; + char *tmp = NULL; sBus = (sbusDevicePtr) busData; GDev->identifier = sBus->descr; @@ -723,11 +723,11 @@ xf86SbusConfigureNewDev(void *busData, sbusDevicePtr sBus, GDevRec * GDev) sparcPromClose(); } if (promPath) { - XNFasprintf(&tmp, "SBUS:%s", promPath); + asprintf(&tmp, "SBUS:%s", promPath); free(promPath); } else { - XNFasprintf(&tmp, "SBUS:fb%d", sBus->fbNum); + asprintf(&tmp, "SBUS:fb%d", sBus->fbNum); } GDev->busID = tmp; }