]> git.ipfire.org Git - people/ms/dma.git/commitdiff
mailnamefile: fix parsing
authorPeter Pentchev <roam@ringlet.net>
Tue, 22 Jun 2010 23:09:01 +0000 (01:09 +0200)
committerSimon Schubert <2@0x2c.org>
Mon, 28 Jun 2010 21:18:04 +0000 (23:18 +0200)
Fix a file descriptor leak in the MAILNAMEFILE code.
Fix a mistake in my patch submitted to DFBSD - "initialized" should be static!

util.c

diff --git a/util.c b/util.c
index 8808d02ee30d3e46a81f4c14d921f6bd8d98f093..e6d43e9928fea86b3db12ef67cc1ab9e4b839cb2 100644 (file)
--- a/util.c
+++ b/util.c
@@ -49,8 +49,9 @@ const char *
 hostname(void)
 {
        static char name[MAXHOSTNAMELEN+1];
-       int initialized = 0;
+       static int initialized = 0;
        FILE *fp;
+       char *res;
        size_t len;
 
        if (initialized)
@@ -64,7 +65,9 @@ hostname(void)
        if (config.mailnamefile != NULL && config.mailnamefile[0] != '\0') {
                fp = fopen(config.mailnamefile, "r");
                if (fp != NULL) {
-                       if (fgets(name, sizeof(name), fp) != NULL) {
+                       res = fgets(name, sizeof(name), fp);
+                       fclose(fp);
+                       if (res != NULL) {
                                len = strlen(name);
                                while (len > 0 &&
                                    (name[len - 1] == '\r' ||
@@ -75,7 +78,6 @@ hostname(void)
                                        return (name);
                                }
                        }
-                       fclose(fp);
                }
        }
        if (gethostname(name, sizeof(name)) != 0)