tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
* read.c does not close gzipped files; leaks memory
@ 2018-02-23 19:28 Wolfgang Müller
  2018-02-23 22:13 ` Ingo Schwarze
  0 siblings, 1 reply; 2+ messages in thread
From: Wolfgang Müller @ 2018-02-23 19:28 UTC (permalink / raw)
  To: tech


[-- Attachment #1.1: Type: text/plain, Size: 361 bytes --]

Hi,

I'm using mandoc on a system with a significant quantity of gzipped
manpages, and noticed its memory usage being unusually high. After
investigating, I found that in read_whole_file, mandoc opens the gzipped
file, but then never closes it before returning from the function.

Find attached a patch which solves this issue.

-- 
Wolfgang Müller

[-- Attachment #1.2: mandoc-gzleak.patch --]
[-- Type: text/x-diff, Size: 777 bytes --]

--- read.c.orig	2018-02-18 07:27:07.124135724 +0000
+++ read.c	2018-02-18 07:34:02.832965838 +0000
@@ -556,6 +556,7 @@
 	gzFile		 gz;
 	size_t		 off;
 	ssize_t		 ssz;
+	int		 end;
 
 	if (fstat(fd, &st) == -1) {
 		mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0,
@@ -598,6 +599,7 @@
 
 	*with_mmap = 0;
 	off = 0;
+	end = 0;
 	fb->sz = 0;
 	fb->buf = NULL;
 	for (;;) {
@@ -614,7 +616,8 @@
 		    read(fd, fb->buf + (int)off, fb->sz - off);
 		if (ssz == 0) {
 			fb->sz = off;
-			return 1;
+			end = 1;
+			break;
 		}
 		if (ssz == -1) {
 			mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0,
@@ -624,6 +627,12 @@
 		off += (size_t)ssz;
 	}
 
+	if (curp->gzip)
+		gzclose(gz);
+
+	if (end)
+		return 1;
+
 	free(fb->buf);
 	fb->buf = NULL;
 	return 0;

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-02-23 22:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-23 19:28 read.c does not close gzipped files; leaks memory Wolfgang Müller
2018-02-23 22:13 ` Ingo Schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).