From: "Richard W.M. Jones" <rjones(a)redhat.com>
Although g->tmpdir is always set after launch, and none of these calls
should be used before launch, it's always possible that a caller would
use one of these APIs before launching the handle.
If guestfs___lazy_make_tmpdir is not called, then g->tmpdir is NULL,
and the constructed filename would look like "(null)/...".
In fact it's possible to observe this error (although it seems to have
no especially bad effects):
$ strace guestfish ls / |& fgrep '(null)'
unlink("(null)/ls1") = -1 ENOENT (No such file or directory)
(cherry picked from commit 1937108a4a6070750f0d636f7dcc8725441cc4eb)
---
src/file.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/src/file.c b/src/file.c
index 7051936..d21a61d 100644
--- a/src/file.c
+++ b/src/file.c
@@ -88,6 +88,9 @@ guestfs__read_file (guestfs_h *g, const char *path, size_t *size_r)
char *ret = NULL;
struct stat statbuf;
+ if (guestfs___lazy_make_tmpdir (g) == -1)
+ goto err;
+
tmpfile = safe_asprintf (g, "%s/cat%d", g->tmpdir, ++g->unique);
if (guestfs_download (g, path, tmpfile) == -1)
@@ -212,6 +215,9 @@ guestfs__find (guestfs_h *g, const char *directory)
char **ret = NULL;
size_t i, count, size;
+ if (guestfs___lazy_make_tmpdir (g) == -1)
+ goto err;
+
tmpfile = safe_asprintf (g, "%s/find%d", g->tmpdir, ++g->unique);
if (guestfs_find0 (g, directory, tmpfile) == -1)
@@ -313,6 +319,9 @@ write_or_append (guestfs_h *g, const char *path,
(!append ? guestfs_internal_write : guestfs_internal_write_append)
(g, path, content, size);
+ if (guestfs___lazy_make_tmpdir (g) == -1)
+ goto err;
+
/* Write the content out to a temporary file. */
tmpfile = safe_asprintf (g, "%s/write%d", g->tmpdir, ++g->unique);
@@ -508,6 +517,9 @@ guestfs__ls (guestfs_h *g, const char *directory)
char **ret = NULL;
size_t i, count, size;
+ if (guestfs___lazy_make_tmpdir (g) == -1)
+ goto err;
+
tmpfile = safe_asprintf (g, "%s/ls%d", g->tmpdir, ++g->unique);
if (guestfs_ls0 (g, directory, tmpfile) == -1)
--
1.8.3.1