Remove PATH_MAX sized stack allocations related to virFileBuildPath

Make virFileBuildPath operate on the heap instead of the stack. It
allocates a buffer instead of expecting a preexisting buffer.
This commit is contained in:
Matthias Bolte 2011-04-03 11:21:14 +02:00
parent bb3fa04183
commit 1573158190
6 changed files with 66 additions and 85 deletions

View File

@ -2484,7 +2484,7 @@ virNWFilterLoadAllConfigs(virConnectPtr conn,
} }
while ((entry = readdir(dir))) { while ((entry = readdir(dir))) {
char path[PATH_MAX]; char *path;
virNWFilterObjPtr nwfilter; virNWFilterObjPtr nwfilter;
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
@ -2493,17 +2493,16 @@ virNWFilterLoadAllConfigs(virConnectPtr conn,
if (!virFileHasSuffix(entry->d_name, ".xml")) if (!virFileHasSuffix(entry->d_name, ".xml"))
continue; continue;
if (virFileBuildPath(configDir, entry->d_name, if (!(path = virFileBuildPath(configDir, entry->d_name, NULL))) {
NULL, path, PATH_MAX) < 0) { virReportOOMError();
virNWFilterReportError(VIR_ERR_INTERNAL_ERROR,
_("config filename '%s/%s' is too long"),
configDir, entry->d_name);
continue; continue;
} }
nwfilter = virNWFilterObjLoad(conn, nwfilters, entry->d_name, path); nwfilter = virNWFilterObjLoad(conn, nwfilters, entry->d_name, path);
if (nwfilter) if (nwfilter)
virNWFilterObjUnlock(nwfilter); virNWFilterObjUnlock(nwfilter);
VIR_FREE(path);
} }
closedir(dir); closedir(dir);
@ -2523,7 +2522,6 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
if (!nwfilter->configFile) { if (!nwfilter->configFile) {
int err; int err;
char path[PATH_MAX];
if ((err = virFileMakePath(driver->configDir))) { if ((err = virFileMakePath(driver->configDir))) {
virReportSystemError(err, virReportSystemError(err,
@ -2532,13 +2530,8 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
return -1; return -1;
} }
if (virFileBuildPath(driver->configDir, def->name, ".xml", if (!(nwfilter->configFile = virFileBuildPath(driver->configDir,
path, sizeof(path)) < 0) { def->name, ".xml"))) {
virNWFilterReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot construct config file path"));
return -1;
}
if (!(nwfilter->configFile = strdup(path))) {
virReportOOMError(); virReportOOMError();
return -1; return -1;
} }

View File

@ -1473,8 +1473,8 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
} }
while ((entry = readdir(dir))) { while ((entry = readdir(dir))) {
char path[PATH_MAX]; char *path;
char autostartLink[PATH_MAX]; char *autostartLink;
virStoragePoolObjPtr pool; virStoragePoolObjPtr pool;
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
@ -1483,19 +1483,15 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
if (!virFileHasSuffix(entry->d_name, ".xml")) if (!virFileHasSuffix(entry->d_name, ".xml"))
continue; continue;
if (virFileBuildPath(configDir, entry->d_name, if (!(path = virFileBuildPath(configDir, entry->d_name, NULL))) {
NULL, path, PATH_MAX) < 0) { virReportOOMError();
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
_("Config filename '%s/%s' is too long"),
configDir, entry->d_name);
continue; continue;
} }
if (virFileBuildPath(autostartDir, entry->d_name, if (!(autostartLink = virFileBuildPath(autostartDir, entry->d_name,
NULL, autostartLink, PATH_MAX) < 0) { NULL))) {
virStorageReportError(VIR_ERR_INTERNAL_ERROR, virReportOOMError();
_("Autostart link path '%s/%s' is too long"), VIR_FREE(path);
autostartDir, entry->d_name);
continue; continue;
} }
@ -1503,6 +1499,9 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
autostartLink); autostartLink);
if (pool) if (pool)
virStoragePoolObjUnlock(pool); virStoragePoolObjUnlock(pool);
VIR_FREE(path);
VIR_FREE(autostartLink);
} }
closedir(dir); closedir(dir);
@ -1520,7 +1519,6 @@ virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
if (!pool->configFile) { if (!pool->configFile) {
int err; int err;
char path[PATH_MAX];
if ((err = virFileMakePath(driver->configDir))) { if ((err = virFileMakePath(driver->configDir))) {
virReportSystemError(err, virReportSystemError(err,
@ -1529,26 +1527,14 @@ virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
return -1; return -1;
} }
if (virFileBuildPath(driver->configDir, def->name, ".xml", if (!(pool->configFile = virFileBuildPath(driver->configDir,
path, sizeof(path)) < 0) { def->name, ".xml"))) {
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot construct config file path"));
return -1;
}
if (!(pool->configFile = strdup(path))) {
virReportOOMError(); virReportOOMError();
return -1; return -1;
} }
if (virFileBuildPath(driver->autostartDir, def->name, ".xml", if (!(pool->autostartLink = virFileBuildPath(driver->autostartDir,
path, sizeof(path)) < 0) { def->name, ".xml"))) {
virStorageReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot construct "
"autostart link path"));
VIR_FREE(pool->configFile);
return -1;
}
if (!(pool->autostartLink = strdup(path))) {
virReportOOMError(); virReportOOMError();
VIR_FREE(pool->configFile); VIR_FREE(pool->configFile);
return -1; return -1;

View File

@ -1842,23 +1842,24 @@ cleanup:
return err; return err;
} }
/* Build up a fully qualfiied path for a config file to be /* Build up a fully qualified path for a config file to be
* associated with a persistent guest or network */ * associated with a persistent guest or network */
int virFileBuildPath(const char *dir, char *
const char *name, virFileBuildPath(const char *dir, const char *name, const char *ext)
const char *ext,
char *buf,
unsigned int buflen)
{ {
if ((strlen(dir) + 1 + strlen(name) + (ext ? strlen(ext) : 0) + 1) >= (buflen-1)) char *path;
return -1;
strcpy(buf, dir); if (ext == NULL) {
strcat(buf, "/"); if (virAsprintf(&path, "%s/%s", dir, name) < 0) {
strcat(buf, name); return NULL;
if (ext) }
strcat(buf, ext); } else {
return 0; if (virAsprintf(&path, "%s/%s%s", dir, name, ext) < 0) {
return NULL;
}
}
return path;
} }

View File

@ -146,11 +146,9 @@ int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
unsigned int flags) ATTRIBUTE_RETURN_CHECK; unsigned int flags) ATTRIBUTE_RETURN_CHECK;
int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK; int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
int virFileBuildPath(const char *dir, char *virFileBuildPath(const char *dir,
const char *name, const char *name,
const char *ext, const char *ext) ATTRIBUTE_RETURN_CHECK;
char *buf,
unsigned int buflen) ATTRIBUTE_RETURN_CHECK;
int virFileAbsPath(const char *path, int virFileAbsPath(const char *path,
char **abspath) ATTRIBUTE_RETURN_CHECK; char **abspath) ATTRIBUTE_RETURN_CHECK;

View File

@ -387,7 +387,7 @@ xenInotifyOpen(virConnectPtr conn,
{ {
DIR *dh; DIR *dh;
struct dirent *ent; struct dirent *ent;
char path[PATH_MAX]; char *path;
xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData; xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;
if (priv->configDir) { if (priv->configDir) {
@ -414,19 +414,21 @@ xenInotifyOpen(virConnectPtr conn,
continue; continue;
/* Build the full file path */ /* Build the full file path */
if ((strlen(priv->configDir) + 1 + if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
strlen(ent->d_name) + 1) > PATH_MAX) virReportOOMError();
continue; closedir(dh);
strcpy(path, priv->configDir); return -1;
strcat(path, "/"); }
strcat(path, ent->d_name);
if (xenInotifyAddDomainConfigInfo(conn, path) < 0 ) { if (xenInotifyAddDomainConfigInfo(conn, path) < 0 ) {
virXenInotifyError(VIR_ERR_INTERNAL_ERROR, virXenInotifyError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error adding file to config list")); "%s", _("Error adding file to config list"));
closedir(dh); closedir(dh);
VIR_FREE(path);
return -1; return -1;
} }
VIR_FREE(path);
} }
closedir(dh); closedir(dh);
} }

View File

@ -358,7 +358,7 @@ int xenXMConfigCacheRefresh (virConnectPtr conn) {
while ((ent = readdir(dh))) { while ((ent = readdir(dh))) {
struct stat st; struct stat st;
char path[PATH_MAX]; char *path;
/* /*
* Skip a bunch of crufty files that clearly aren't config files * Skip a bunch of crufty files that clearly aren't config files
@ -387,15 +387,16 @@ int xenXMConfigCacheRefresh (virConnectPtr conn) {
continue; continue;
/* Build the full file path */ /* Build the full file path */
if ((strlen(priv->configDir) + 1 + strlen(ent->d_name) + 1) > PATH_MAX) if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
continue; virReportOOMError();
strcpy(path, priv->configDir); closedir(dh);
strcat(path, "/"); return -1;
strcat(path, ent->d_name); }
/* Skip anything which isn't a file (takes care of scripts/ subdir */ /* Skip anything which isn't a file (takes care of scripts/ subdir */
if ((stat(path, &st) < 0) || if ((stat(path, &st) < 0) ||
(!S_ISREG(st.st_mode))) { (!S_ISREG(st.st_mode))) {
VIR_FREE(path);
continue; continue;
} }
@ -404,6 +405,8 @@ int xenXMConfigCacheRefresh (virConnectPtr conn) {
if (xenXMConfigCacheAddFile(conn, path) < 0) { if (xenXMConfigCacheAddFile(conn, path) < 0) {
/* Ignoring errors, since alot of stuff goes wrong in /etc/xen */ /* Ignoring errors, since alot of stuff goes wrong in /etc/xen */
} }
VIR_FREE(path);
} }
/* Reap all entries which were not changed, by comparing /* Reap all entries which were not changed, by comparing
@ -1046,10 +1049,11 @@ int xenXMDomainCreate(virDomainPtr domain) {
* Create a config file for a domain, based on an XML * Create a config file for a domain, based on an XML
* document describing its config * document describing its config
*/ */
virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml) { virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml)
{
virDomainPtr ret; virDomainPtr ret;
char filename[PATH_MAX]; char *filename;
const char * oldfilename; const char *oldfilename;
virDomainDefPtr def = NULL; virDomainDefPtr def = NULL;
xenXMConfCachePtr entry = NULL; xenXMConfCachePtr entry = NULL;
xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData; xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;
@ -1130,16 +1134,11 @@ virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml) {
entry = NULL; entry = NULL;
} }
if ((strlen(priv->configDir) + 1 + strlen(def->name) + 1) > PATH_MAX) { if (!(filename = virFileBuildPath(priv->configDir, def->name, NULL))) {
xenXMError(VIR_ERR_INTERNAL_ERROR, virReportOOMError();
"%s", _("config file name is too long"));
goto error; goto error;
} }
strcpy(filename, priv->configDir);
strcat(filename, "/");
strcat(filename, def->name);
if (xenXMConfigSaveFile(conn, filename, def) < 0) if (xenXMConfigSaveFile(conn, filename, def) < 0)
goto error; goto error;
@ -1172,9 +1171,11 @@ virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml) {
ret = virGetDomain(conn, def->name, def->uuid); ret = virGetDomain(conn, def->name, def->uuid);
xenUnifiedUnlock(priv); xenUnifiedUnlock(priv);
VIR_FREE(filename);
return (ret); return (ret);
error: error:
VIR_FREE(filename);
VIR_FREE(entry); VIR_FREE(entry);
virDomainDefFree(def); virDomainDefFree(def);
xenUnifiedUnlock(priv); xenUnifiedUnlock(priv);