[systemd-devel] [PATCH 2/2] journald: fix fd leak in journal_file_empty

Harald Hoyer harald.hoyer at gmail.com
Thu Sep 5 22:13:22 PDT 2013


On 09/04/2013 02:12 PM, George McCollister wrote:
> Before my previous patch, journal_file_empty wasn't be called with the
> correct filename. Now that it's being called with the correct filename
> it leaks file descriptors. This patch closes the file descriptors before
> returning.
> 
> Signed-off-by: George McCollister <george.mccollister at gmail.com>
> ---
>  src/journal/journal-vacuum.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
> index ace7722..2cfc73c 100644
> --- a/src/journal/journal-vacuum.c
> +++ b/src/journal/journal-vacuum.c
> @@ -136,12 +136,18 @@ static int journal_file_empty(int dir_fd, const char *name) {
>          if (fd < 0)
>                  return -errno;
>  
> -        if (lseek(fd, offsetof(Header, n_entries), SEEK_SET) < 0)
> +        if (lseek(fd, offsetof(Header, n_entries), SEEK_SET) < 0) {
> +                close(fd);
>                  return -errno;
> +        }
>  
>          r = read(fd, &n_entries, sizeof(n_entries));
> -        if (r != sizeof(n_entries))
> +        if (r != sizeof(n_entries)) {
> +                close(fd);
>                  return r == 0 ? -EINVAL : -errno;
> +        }
> +
> +        close(fd);
>  
>          return le64toh(n_entries) == 0;
>  }
> 

Thanks! Pushed, but with the use of _cleanup_close_


More information about the systemd-devel mailing list