syntax: Remove PATH_MAX-sized buffers allocated on the stack.

On Linux PATH_MAX is 4096, but on some platforms it can be much larger
or even not defined (ie. unlimited).  Therefore using a PATH_MAX-sized
stack buffer is not a great idea for portable programs.

This change removes use of PATH_MAX-sized stack-allocated buffers.

This change only applies to the library and standalone programs.
Inside the daemon, memory allocation is much more complicated so I
have not changed those (yet).

Found by 'make syntax-check'.
This commit is contained in:
Richard W.M. Jones
2012-09-15 13:01:10 +01:00
parent a67129b0fb
commit 2383d7a78e
4 changed files with 34 additions and 16 deletions

View File

@@ -582,12 +582,14 @@ do_output_vgs (void)
exit (EXIT_FAILURE);
for (i = 0; i < vgs->len; ++i) {
char name[PATH_MAX];
char *name;
char uuid[33];
char **parents;
strcpy (name, "/dev/");
strcpy (&name[5], vgs->val[i].vg_name);
if (asprintf (&name, "/dev/%s", vgs->val[i].vg_name) == -1) {
perror ("asprintf");
exit (EXIT_FAILURE);
}
memcpy (uuid, vgs->val[i].vg_uuid, 32);
uuid[32] = '\0';
@@ -597,6 +599,7 @@ do_output_vgs (void)
write_row (name, "vg",
NULL, NULL, -1, (int64_t) vgs->val[i].vg_size, parents, uuid);
free (name);
free_strings (parents);
}

View File

@@ -145,9 +145,10 @@ make_tar_from_local (const char *local)
dup2 (fd[1], 1);
close (fd[1]);
char buf[PATH_MAX];
size_t buf_len = strlen (local) + 1;
char buf[buf_len];
const char *dirname, *basename;
if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
if (split_path (buf, buf_len, local, &dirname, &basename) == -1)
_exit (EXIT_FAILURE);
tar_create (dirname, basename);
@@ -242,19 +243,26 @@ run_copy_out (const char *cmd, size_t argc, char *argv[])
return -1;
}
if (r == 1) { /* is file */
char buf[PATH_MAX];
size_t buf_len = strlen (remote) + 1;
char buf[buf_len];
const char *basename;
if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
if (split_path (buf, buf_len, remote, NULL, &basename) == -1) {
free (remote);
return -1;
}
char filename[PATH_MAX];
snprintf (filename, sizeof filename, "%s/%s", local, basename);
if (guestfs_download (g, remote, filename) == -1) {
char *filename;
if (asprintf (&filename, "%s/%s", local, basename) == -1) {
perror ("asprintf");
free (remote);
return -1;
}
if (guestfs_download (g, remote, filename) == -1) {
free (remote);
free (filename);
return -1;
}
free (filename);
}
else { /* not a regular file */
r = guestfs_is_dir (g, remote);
@@ -270,9 +278,10 @@ run_copy_out (const char *cmd, size_t argc, char *argv[])
return -1;
}
char buf[PATH_MAX];
size_t buf_len = strlen (remote) + 1;
char buf[buf_len];
const char *basename;
if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
if (split_path (buf, buf_len, remote, NULL, &basename) == -1) {
free (remote);
return -1;
}

View File

@@ -294,9 +294,10 @@ launch_appliance (guestfs_h *g, const char *arg)
cachemode = ",cache=writeback";
}
char buf2[PATH_MAX + 64];
size_t buf2_len = strlen (appliance) + 64;
char buf2[buf2_len];
add_cmdline (g, "-drive");
snprintf (buf2, sizeof buf2, "file=%s,snapshot=on,id=appliance,if=%s%s",
snprintf (buf2, buf2_len, "file=%s,snapshot=on,id=appliance,if=%s%s",
appliance, virtio_scsi ? "none" : "virtio", cachemode);
add_cmdline (g, buf2);

View File

@@ -293,7 +293,7 @@ cleanup_wrapper (void)
static void
set_qemu (const char *path, int use_wrapper)
{
char buffer[PATH_MAX];
char *buffer;
struct stat statbuf;
int fd;
FILE *fp;
@@ -318,14 +318,19 @@ set_qemu (const char *path, int use_wrapper)
}
/* This should be a source directory, so check it. */
snprintf (buffer, sizeof buffer, "%s/pc-bios", path);
if (asprintf (&buffer, "%s/pc-bios", path) == -1) {
perror ("asprintf");
exit (EXIT_FAILURE);
}
if (stat (buffer, &statbuf) == -1 ||
!S_ISDIR (statbuf.st_mode)) {
fprintf (stderr,
_("%s: does not look like a qemu source directory\n"),
path);
free (buffer);
exit (EXIT_FAILURE);
}
free (buffer);
/* Make a wrapper script. */
fd = mkstemp (qemuwrapper);