Convert alloc+sprintf pairs into asprintf() & XNFasprintf() calls

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Reviewed-by: Mikhail Gusarov <dottedmag@dottedmag.net>
This commit is contained in:
Alan Coopersmith 2010-11-27 22:38:27 -08:00
parent 2416255f7e
commit 3a9bb93dd1
11 changed files with 36 additions and 62 deletions

View File

@ -63,10 +63,8 @@ device_removed(LibHalContext *ctx, const char *udi)
{ {
char *value; char *value;
value = malloc(strlen(udi) + 5); /* "hal:" + NULL */ if (asprintf (&value, "hal:%s", udi) == -1)
if (!value)
return; return;
sprintf(value, "hal:%s", udi);
remove_devices("hal", value); remove_devices("hal", value);
@ -228,12 +226,11 @@ device_added(LibHalContext *hal_ctx, const char *udi)
add_option(&options, "driver", driver); add_option(&options, "driver", driver);
add_option(&options, "name", name); add_option(&options, "name", name);
config_info = malloc(strlen(udi) + 5); /* "hal:" and NULL */ if (asprintf (&config_info, "hal:%s", udi) == -1) {
if (!config_info) { config_info = NULL;
LogMessage(X_ERROR, "config/hal: couldn't allocate name\n"); LogMessage(X_ERROR, "config/hal: couldn't allocate name\n");
goto unwind; goto unwind;
} }
sprintf(config_info, "hal:%s", udi);
/* Check for duplicate devices */ /* Check for duplicate devices */
if (device_is_duplicate(config_info)) if (device_is_duplicate(config_info))

View File

@ -205,12 +205,9 @@ configureScreenSection (int screennum)
int depths[] = { 1, 4, 8, 15, 16, 24/*, 32*/ }; int depths[] = { 1, 4, 8, 15, 16, 24/*, 32*/ };
parsePrologue (XF86ConfScreenPtr, XF86ConfScreenRec) parsePrologue (XF86ConfScreenPtr, XF86ConfScreenRec)
ptr->scrn_identifier = malloc(18); XNFasprintf(&ptr->scrn_identifier, "Screen%d", screennum);
sprintf(ptr->scrn_identifier, "Screen%d", screennum); XNFasprintf(&ptr->scrn_monitor_str, "Monitor%d", screennum);
ptr->scrn_monitor_str = malloc(19); XNFasprintf(&ptr->scrn_device_str, "Card%d", screennum);
sprintf(ptr->scrn_monitor_str, "Monitor%d", screennum);
ptr->scrn_device_str = malloc(16);
sprintf(ptr->scrn_device_str, "Card%d", screennum);
for (i=0; i<sizeof(depths)/sizeof(depths[0]); i++) for (i=0; i<sizeof(depths)/sizeof(depths[0]); i++)
{ {
@ -256,14 +253,13 @@ optionTypeToString(OptionValueType type)
static XF86ConfDevicePtr static XF86ConfDevicePtr
configureDeviceSection (int screennum) configureDeviceSection (int screennum)
{ {
char identifier[16];
OptionInfoPtr p; OptionInfoPtr p;
int i = 0; int i = 0;
parsePrologue (XF86ConfDevicePtr, XF86ConfDeviceRec) parsePrologue (XF86ConfDevicePtr, XF86ConfDeviceRec)
/* Move device info to parser structure */ /* Move device info to parser structure */
sprintf(identifier, "Card%d", screennum); if (asprintf(&ptr->dev_identifier, "Card%d", screennum) == -1)
ptr->dev_identifier = strdup(identifier); ptr->dev_identifier = NULL;
ptr->dev_chipset = DevToConfig[screennum].GDev.chipset; ptr->dev_chipset = DevToConfig[screennum].GDev.chipset;
ptr->dev_busid = DevToConfig[screennum].GDev.busID; ptr->dev_busid = DevToConfig[screennum].GDev.busID;
ptr->dev_driver = DevToConfig[screennum].GDev.driver; ptr->dev_driver = DevToConfig[screennum].GDev.driver;
@ -306,10 +302,8 @@ configureDeviceSection (int screennum)
int len = strlen(ptr->dev_comment) + strlen(prefix) + int len = strlen(ptr->dev_comment) + strlen(prefix) +
strlen(middle) + strlen(suffix) + 1; strlen(middle) + strlen(suffix) + 1;
optname = malloc(strlen(p->name) + 2 + 1); if (asprintf(&optname, "\"%s\"", p->name) == -1)
if (!optname)
break; break;
sprintf(optname, "\"%s\"", p->name);
len += max(20, strlen(optname)); len += max(20, strlen(optname));
len += strlen(opttype); len += strlen(opttype);
@ -370,16 +364,14 @@ configureLayoutSection (void)
aptr->adj_x = 0; aptr->adj_x = 0;
aptr->adj_y = 0; aptr->adj_y = 0;
aptr->adj_scrnum = scrnum; aptr->adj_scrnum = scrnum;
aptr->adj_screen_str = xnfalloc(18); XNFasprintf(&aptr->adj_screen_str, "Screen%d", scrnum);
sprintf(aptr->adj_screen_str, "Screen%d", scrnum);
if (scrnum == 0) { if (scrnum == 0) {
aptr->adj_where = CONF_ADJ_ABSOLUTE; aptr->adj_where = CONF_ADJ_ABSOLUTE;
aptr->adj_refscreen = NULL; aptr->adj_refscreen = NULL;
} }
else { else {
aptr->adj_where = CONF_ADJ_RIGHTOF; aptr->adj_where = CONF_ADJ_RIGHTOF;
aptr->adj_refscreen = xnfalloc(18); XNFasprintf(&aptr->adj_refscreen, "Screen%d", scrnum - 1);
sprintf(aptr->adj_refscreen, "Screen%d", scrnum - 1);
} }
ptr->lay_adjacency_lst = ptr->lay_adjacency_lst =
(XF86ConfAdjacencyPtr)xf86addListItem((glp)ptr->lay_adjacency_lst, (XF86ConfAdjacencyPtr)xf86addListItem((glp)ptr->lay_adjacency_lst,
@ -443,8 +435,7 @@ configureMonitorSection (int screennum)
{ {
parsePrologue (XF86ConfMonitorPtr, XF86ConfMonitorRec) parsePrologue (XF86ConfMonitorPtr, XF86ConfMonitorRec)
ptr->mon_identifier = malloc(19); XNFasprintf(&ptr->mon_identifier, "Monitor%d", screennum);
sprintf(ptr->mon_identifier, "Monitor%d", screennum);
ptr->mon_vendor = strdup("Monitor Vendor"); ptr->mon_vendor = strdup("Monitor Vendor");
ptr->mon_modelname = strdup("Monitor Model"); ptr->mon_modelname = strdup("Monitor Model");
@ -491,11 +482,9 @@ configureDDCMonitorSection (int screennum)
parsePrologue (XF86ConfMonitorPtr, XF86ConfMonitorRec) parsePrologue (XF86ConfMonitorPtr, XF86ConfMonitorRec)
ptr->mon_identifier = malloc(19); XNFasprintf(&ptr->mon_identifier, "Monitor%d", screennum);
sprintf(ptr->mon_identifier, "Monitor%d", screennum);
ptr->mon_vendor = strdup(ConfiguredMonitor->vendor.name); ptr->mon_vendor = strdup(ConfiguredMonitor->vendor.name);
ptr->mon_modelname = malloc(12); XNFasprintf(&ptr->mon_modelname, "%x", ConfiguredMonitor->vendor.prod_id);
sprintf(ptr->mon_modelname, "%x", ConfiguredMonitor->vendor.prod_id);
/* features in centimetres, we want millimetres */ /* features in centimetres, we want millimetres */
mon_width = 10 * ConfiguredMonitor->features.hsize ; mon_width = 10 * ConfiguredMonitor->features.hsize ;

View File

@ -1284,11 +1284,8 @@ xf86LogInit(void)
/* Get the log file name */ /* Get the log file name */
if (xf86LogFileFrom == X_DEFAULT) { if (xf86LogFileFrom == X_DEFAULT) {
/* Append the display number and ".log" */ /* Append the display number and ".log" */
lf = malloc(strlen(xf86LogFile) + strlen("%s") + if (asprintf(&lf, "%s%%s" LOGSUFFIX, xf86LogFile) == -1)
strlen(LOGSUFFIX) + 1);
if (!lf)
FatalError("Cannot allocate space for the log file name\n"); FatalError("Cannot allocate space for the log file name\n");
sprintf(lf, "%s%%s" LOGSUFFIX, xf86LogFile);
xf86LogFile = lf; xf86LogFile = lf;
} }

View File

@ -536,8 +536,7 @@ InitOutput(ScreenInfo *pScreenInfo, int argc, char **argv)
for (i = 0; i < xf86NumScreens; i++) { for (i = 0; i < xf86NumScreens; i++) {
if (xf86Screens[i]->name == NULL) { if (xf86Screens[i]->name == NULL) {
xf86Screens[i]->name = xnfalloc(strlen("screen") + 10 + 1); XNFasprintf(&xf86Screens[i]->name, "screen%d", i);
sprintf(xf86Screens[i]->name, "screen%d", i);
xf86MsgVerb(X_WARNING, 0, xf86MsgVerb(X_WARNING, 0,
"Screen driver %d has no name set, using `%s'.\n", "Screen driver %d has no name set, using `%s'.\n",
i, xf86Screens[i]->name); i, xf86Screens[i]->name);

View File

@ -111,6 +111,9 @@ void DoShowOptions (void) {
); );
for (p = pOption; p->name != NULL; p++) { for (p = pOption; p->name != NULL; p++) {
const char *opttype = optionTypeToSting(p->type); const char *opttype = optionTypeToSting(p->type);
/* XXX: Why overallocate by 2 bytes?
* Otherwise, this would be strdup()
*/
char *optname = malloc(strlen(p->name) + 2 + 1); char *optname = malloc(strlen(p->name) + 2 + 1);
if (!optname) { if (!optname) {
continue; continue;

View File

@ -1347,9 +1347,9 @@ xf86PciConfigureNewDev(void *busData, struct pci_device *pVideo,
pVideo = (struct pci_device *) busData; pVideo = (struct pci_device *) busData;
GDev->busID = xnfalloc(16);
xf86FormatPciBusNumber(pVideo->bus, busnum); xf86FormatPciBusNumber(pVideo->bus, busnum);
sprintf(GDev->busID, "PCI:%s:%d:%d", busnum, pVideo->dev, pVideo->func); XNFasprintf(&GDev->busID, "PCI:%s:%d:%d",
busnum, pVideo->dev, pVideo->func);
GDev->chipID = pVideo->device_id; GDev->chipID = pVideo->device_id;
GDev->chipRev = pVideo->revision; GDev->chipRev = pVideo->revision;

View File

@ -706,11 +706,9 @@ xf86SbusConfigureNewDev(void *busData, sbusDevicePtr sBus, GDevRec *GDev)
sparcPromClose(); sparcPromClose();
} }
if (promPath) { if (promPath) {
GDev->busID = xnfalloc(strlen(promPath) + 6); XNFasprintf(&GDev->busID, "SBUS:%s", promPath);
sprintf(GDev->busID, "SBUS:%s", promPath);
free(promPath); free(promPath);
} else { } else {
GDev->busID = xnfalloc(12); XNFsprintf(&GDev->busID, "SBUS:fb%d", sBus->fbNum);
sprintf(GDev->busID, "SBUS:fb%d", sBus->fbNum);
} }
} }

View File

@ -2426,13 +2426,10 @@ DRICreatePCIBusID(const struct pci_device * dev)
{ {
char *busID; char *busID;
busID = malloc(20); if (asprintf(&busID, "pci:%04x:%02x:%02x.%d",
if (busID == NULL) dev->domain, dev->bus, dev->dev, dev->func) == -1)
return NULL; return NULL;
snprintf(busID, 20, "pci:%04x:%02x:%02x.%d", dev->domain, dev->bus,
dev->dev, dev->func);
return busID; return busID;
} }

View File

@ -406,22 +406,22 @@ FindModuleInSubdir(const char *dirpath, const char *module)
snprintf(tmpBuf, PATH_MAX, "lib%s.so", module); snprintf(tmpBuf, PATH_MAX, "lib%s.so", module);
if (strcmp(direntry->d_name, tmpBuf) == 0) { if (strcmp(direntry->d_name, tmpBuf) == 0) {
ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1); if (asprintf(&ret, "%s%s", dirpath, tmpBuf) == -1)
sprintf(ret, "%s%s", dirpath, tmpBuf); ret = NULL;
break; break;
} }
snprintf(tmpBuf, PATH_MAX, "%s_drv.so", module); snprintf(tmpBuf, PATH_MAX, "%s_drv.so", module);
if (strcmp(direntry->d_name, tmpBuf) == 0) { if (strcmp(direntry->d_name, tmpBuf) == 0) {
ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1); if (asprintf(&ret, "%s%s", dirpath, tmpBuf) == -1)
sprintf(ret, "%s%s", dirpath, tmpBuf); ret = NULL;
break; break;
} }
snprintf(tmpBuf, PATH_MAX, "%s.so", module); snprintf(tmpBuf, PATH_MAX, "%s.so", module);
if (strcmp(direntry->d_name, tmpBuf) == 0) { if (strcmp(direntry->d_name, tmpBuf) == 0) {
ret = malloc(strlen(tmpBuf) + strlen(dirpath) + 1); if (asprintf(&ret, "%s%s", dirpath, tmpBuf) == -1)
sprintf(ret, "%s%s", dirpath, tmpBuf); ret = NULL;
break; break;
} }
} }

View File

@ -356,8 +356,8 @@ VBESetModeNames(DisplayModePtr pMode)
pMode->VDisplay > 10000 || pMode->VDisplay < 0) { pMode->VDisplay > 10000 || pMode->VDisplay < 0) {
pMode->name = strdup("BADMODE"); pMode->name = strdup("BADMODE");
} else { } else {
pMode->name = xnfalloc(4 + 1 + 4 + 1); XNFasprintf(&pMode->name, "%dx%d",
sprintf(pMode->name, "%dx%d", pMode->HDisplay, pMode->VDisplay); pMode->HDisplay, pMode->VDisplay);
} }
} }
pMode = pMode->next; pMode = pMode->next;

View File

@ -177,10 +177,8 @@ LogInit(const char *fname, const char *backup)
char *logFileName = NULL; char *logFileName = NULL;
if (fname && *fname) { if (fname && *fname) {
logFileName = malloc(strlen(fname) + strlen(display) + 1); if (asprintf(&logFileName, fname, display) == -1)
if (!logFileName)
FatalError("Cannot allocate space for the log file name\n"); FatalError("Cannot allocate space for the log file name\n");
sprintf(logFileName, fname, display);
if (backup && *backup) { if (backup && *backup) {
struct stat buf; struct stat buf;
@ -189,13 +187,9 @@ LogInit(const char *fname, const char *backup)
char *suffix; char *suffix;
char *oldLog; char *oldLog;
oldLog = malloc(strlen(logFileName) + strlen(backup) + if ((asprintf(&suffix, backup, display) == -1) ||
strlen(display) + 1); (asprintf(&oldLog, "%s%s", logFileName, suffix) == -1))
suffix = malloc(strlen(backup) + strlen(display) + 1);
if (!oldLog || !suffix)
FatalError("Cannot allocate space for the log file name\n"); FatalError("Cannot allocate space for the log file name\n");
sprintf(suffix, backup, display);
sprintf(oldLog, "%s%s", logFileName, suffix);
free(suffix); free(suffix);
if (rename(logFileName, oldLog) == -1) { if (rename(logFileName, oldLog) == -1) {
FatalError("Cannot move old log file \"%s\" to \"%s\"\n", FatalError("Cannot move old log file \"%s\" to \"%s\"\n",