fixed file descriptor leaks
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 2 Nov 2014 09:22:11 +0000 (10:22 +0100)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 2 Nov 2014 09:22:11 +0000 (10:22 +0100)
NEWS
src/pio.c
src/pio.h
src/ppastats.1

diff --git a/NEWS b/NEWS
index d3bd375..6bc4688 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ v1.3.2
 * fixed compilation against newer version of json-c (deprecation
   of the function json_object_object_get).
 * fixed layout issues due to missing box-sizing properties.
+* fixed file description leaks.
 
 v1.3.1
 ------
index 701823d..2f806b8 100644 (file)
--- a/src/pio.c
+++ b/src/pio.c
@@ -1,21 +1,23 @@
 /*
-    Copyright (C) 2010-2014 wpitchoune@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#define _LARGEFILE_SOURCE 1
+#include "config.h"
 
 #include <dirent.h>
 #include <fts.h>
@@ -80,11 +82,24 @@ char *dir_normalize(const char *dpath)
        return npath;
 }
 
-char **dir_list(const char *dpath, int (*filter) (const char *path))
+static char **paths_add(char **paths, int n, char *path)
+{
+       char **result;
+
+       result = malloc((n+1) * sizeof(void *));
+
+       memcpy(result + 1, paths, n * sizeof(void *));
+
+       *result = path;
+
+       return result;
+}
+
+char **dir_list(const char *dpath, int (*filter) (const char *))
 {
        struct dirent *ent;
        DIR *dir;
-       char **paths;
+       char **paths, *path, *name, **tmp;
        int n;
 
        dir = opendir(dpath);
@@ -97,30 +112,21 @@ char **dir_list(const char *dpath, int (*filter) (const char *path))
        *paths = NULL;
 
        while ((ent = readdir(dir)) != NULL) {
-               char *fpath;
-               char *name = ent->d_name;
+               name = ent->d_name;
 
                if (!strcmp(name, ".") || !strcmp(name, ".."))
                        continue;
 
-               fpath = malloc(strlen(dpath) + 1 + strlen(name) + 1);
+               path = path_append(dpath, name);
 
-               strcpy(fpath, dpath);
-               strcat(fpath, "/");
-               strcat(fpath, name);
-
-               if (!filter || filter(fpath)) {
-                       char **npaths;
-
-                       n++;
-                       npaths = malloc(n * sizeof(void *));
-                       memcpy(npaths + 1, paths, (n - 1) * sizeof(void *));
+               if (!filter || filter(path)) {
+                       tmp = paths_add(paths, n, path);
                        free(paths);
-                       paths = npaths;
-                       *npaths = fpath;
+                       paths = tmp;
 
+                       n++;
                } else {
-                       free(fpath);
+                       free(path);
                }
        }
 
@@ -163,11 +169,11 @@ char *file_get_content(const char *fpath)
                        page = malloc(size + 1);
                        if (!page || size != fread(page, 1, size, fp)) {
                                free(page);
-                               return NULL;
+                               page = NULL;
+                       } else {
+                               *(page + size) = '\0';
                        }
 
-                       *(page + size) = '\0';
-
                        fclose(fp);
                } else {
                        page = NULL;
@@ -180,25 +186,24 @@ char *file_get_content(const char *fpath)
 long file_get_size(const char *path)
 {
        FILE *fp;
+       long size;
 
        if (!is_file(path))
                return -1;
 
        fp = fopen(path, "rb");
        if (fp) {
-               long size;
-
                if (fseek(fp, 0, SEEK_END) == -1)
-                       return -1;
-
-               size = ftell(fp);
+                       size = -1;
+               else
+                       size = ftell(fp);
 
                fclose(fp);
-
-               return size;
+       } else {
+               size = -1;
        }
 
-       return -1;
+       return size;
 }
 
 #define FCOPY_BUF_SZ 4096
index 7b38a41..385ad33 100644 (file)
--- a/src/pio.h
+++ b/src/pio.h
@@ -20,7 +20,7 @@
 #ifndef _P_IO_H
 #define _P_IO_H
 
-#define P_IO_VER 2
+#define P_IO_VER 4
 
 /* Returns '1' if a given 'path' denotates a directory else returns
    0 */
index 4b17424..782ec64 100644 (file)
@@ -1,10 +1,10 @@
-.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.44.1.
-.TH PPASTATS "1" "April 2014" "ppastats 1.3.1" "User Commands"
+.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.46.2.
+.TH PPASTATS "1" "November 2014" "ppastats 1.3.2" "User Commands"
 .SH NAME
 ppastats \- PPA Statistics command line tool
 .SH SYNOPSIS
 .B ppastats
-[\fIOPTION\fR]... \fIPPA_OWNER PPA_NAME\fR
+[\fI\,OPTION\/\fR]... \fI\,PPA_OWNER PPA_NAME\/\fR
 .SH DESCRIPTION
 ppastats is a command application for generating PPA statistics.
 .PP
@@ -18,13 +18,13 @@ display this help and exit
 \fB\-v\fR, \fB\-\-version\fR
 display version information and exit
 .TP
-\fB\-o\fR, \fB\-\-output\-dir\fR=\fI[PATH]\fR
+\fB\-o\fR, \fB\-\-output\-dir\fR=\fI\,[PATH]\/\fR
 generates HTML pages into 'PATH'
 .TP
-\fB\-t\fR, \fB\-\-theme\-dir\fR=\fI[PATH]\fR
+\fB\-t\fR, \fB\-\-theme\-dir\fR=\fI\,[PATH]\/\fR
 set theme dir to 'PATH'
 .TP
-\fB\-s\fR, \fB\-\-status\fR=\fI[STATUS]\fR
+\fB\-s\fR, \fB\-\-status\fR=\fI\,[STATUS]\/\fR
 retrieves only package of the given status
 (possible values are: Pending, Published,
 Superseded, Deleted or Obsolete)
@@ -32,7 +32,7 @@ Superseded, Deleted or Obsolete)
 \fB\-S\fR, \fB\-\-skip\-js\-css\fR
 skip installation of js and css files
 .TP
-\fB\-\-get\-bpph\-size\fR=\fI[s]\fR
+\fB\-\-get\-bpph\-size\fR=\fI\,[s]\/\fR
 size of the replies of webservice requests to get
 the list of binary packages. Between 1 and 300.
 .SH "REPORTING BUGS"