mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 14:45:24 +00:00
Fix off-by-1 in virFileAbsPath.
The virFileAbsPath was not taking into account the '/' directory separator when allocating memory for combining cwd + path. Convert to use virAsprintf to avoid this type of bug completely. * src/util/util.c: Convert virFileAbsPath to use virAsprintf
This commit is contained in:
parent
08fb2a9ce8
commit
9f5bbe3b92
@ -1990,30 +1990,22 @@ cleanup:
|
|||||||
int virFileAbsPath(const char *path, char **abspath)
|
int virFileAbsPath(const char *path, char **abspath)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
int cwdlen;
|
|
||||||
|
|
||||||
if (path[0] == '/') {
|
if (path[0] == '/') {
|
||||||
buf = strdup(path);
|
if (!(*abspath = strdup(path)))
|
||||||
if (buf == NULL)
|
return -1;
|
||||||
return(-1);
|
|
||||||
} else {
|
} else {
|
||||||
buf = getcwd(NULL, 0);
|
buf = getcwd(NULL, 0);
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
return(-1);
|
return -1;
|
||||||
|
|
||||||
cwdlen = strlen(buf);
|
if (virAsprintf(abspath, "%s/%s", buf, path) < 0) {
|
||||||
/* cwdlen includes the null terminator */
|
|
||||||
if (VIR_REALLOC_N(buf, cwdlen + strlen(path) + 1) < 0) {
|
|
||||||
VIR_FREE(buf);
|
VIR_FREE(buf);
|
||||||
errno = ENOMEM;
|
return -1;
|
||||||
return(-1);
|
|
||||||
}
|
}
|
||||||
|
VIR_FREE(buf);
|
||||||
buf[cwdlen] = '/';
|
|
||||||
strcpy(&buf[cwdlen + 1], path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*abspath = buf;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user