aboutsummaryrefslogtreecommitdiffstats
path: root/src/image.c
diff options
context:
space:
mode:
authorPaul Eggert <[email protected]>2013-07-19 11:09:23 -0700
committerPaul Eggert <[email protected]>2013-07-19 11:09:23 -0700
commit3f5bef16fab0ba83cb2298f8137fec831af1aec4 (patch)
tree8f825fe5834080db213452ee3bb1b291f1923a6d /src/image.c
parent4195afc389bb0e5ed5aa749e7606a710e07a72d1 (diff)
Fix some minor file descriptor leaks and related glitches.
* filelock.c (create_lock_file) [!O_CLOEXEC]: Use fcntl with FD_CLOEXEC. (create_lock_file): Use write, not emacs_write. * image.c (slurp_file, png_load_body): * process.c (Fnetwork_interface_list, Fnetwork_interface_info) (server_accept_connection): Don't leak an fd on memory allocation failure. * image.c (slurp_file): Add a cheap heuristic for growing files. * xfaces.c (Fx_load_color_file): Block input around the fopen too, as that's what the other routines do. Maybe input need not be blocked at all, but it's better to be consistent. Avoid undefined behavior when strlen is zero.
Diffstat (limited to 'src/image.c')
-rw-r--r--src/image.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/image.c b/src/image.c
index 95d385dc9e..1e3944ac1a 100644
--- a/src/image.c
+++ b/src/image.c
@@ -2276,23 +2276,28 @@ slurp_file (char *file, ptrdiff_t *size)
unsigned char *buf = NULL;
struct stat st;
- if (fp && fstat (fileno (fp), &st) == 0
- && 0 <= st.st_size && st.st_size <= min (PTRDIFF_MAX, SIZE_MAX)
- && (buf = xmalloc (st.st_size),
- fread (buf, 1, st.st_size, fp) == st.st_size))
- {
- *size = st.st_size;
- fclose (fp);
- }
- else
+ if (fp)
{
- if (fp)
- fclose (fp);
- if (buf)
+ ptrdiff_t count = SPECPDL_INDEX ();
+ record_unwind_protect_ptr (fclose_unwind, fp);
+
+ if (fstat (fileno (fp), &st) == 0
+ && 0 <= st.st_size && st.st_size < min (PTRDIFF_MAX, SIZE_MAX))
{
- xfree (buf);
- buf = NULL;
+ /* Report an error if we read past the purported EOF.
+ This can happen if the file grows as we read it. */
+ ptrdiff_t buflen = st.st_size;
+ buf = xmalloc (buflen + 1);
+ if (fread (buf, 1, buflen + 1, fp) == buflen)
+ *size = buflen;
+ else
+ {
+ xfree (buf);
+ buf = NULL;
+ }
}
+
+ unbind_to (count, Qnil);
}
return buf;
@@ -5732,8 +5737,8 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
if (fread (sig, 1, sizeof sig, fp) != sizeof sig
|| fn_png_sig_cmp (sig, 0, sizeof sig))
{
- image_error ("Not a PNG file: `%s'", file, Qnil);
fclose (fp);
+ image_error ("Not a PNG file: `%s'", file, Qnil);
return 0;
}
}