summaryrefslogtreecommitdiffstats
path: root/fish/copy.c
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-09-15 13:01:10 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-09-15 13:01:10 +0100
commit2383d7a78e8abc308e36b2045087a9785c893f49 (patch)
tree919b434bcb07c99f6c815e6e03b1394a78858ff4 /fish/copy.c
parenta67129b0fb45b2f83eb711c6c599569d0f53e580 (diff)
downloadlibguestfs-2383d7a78e8abc308e36b2045087a9785c893f49.tar.gz
libguestfs-2383d7a78e8abc308e36b2045087a9785c893f49.tar.xz
libguestfs-2383d7a78e8abc308e36b2045087a9785c893f49.zip
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'.
Diffstat (limited to 'fish/copy.c')
-rw-r--r--fish/copy.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/fish/copy.c b/fish/copy.c
index f29d6ef2..5b1bf4c1 100644
--- a/fish/copy.c
+++ b/fish/copy.c
@@ -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);
+ 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;
}