[systemd-devel] [PATCH] dbus1-generator: Always close file in create_dbus_files

Lennart Poettering lennart at poettering.net
Fri Aug 22 03:45:13 PDT 2014


On Fri, 22.08.14 08:19, Simon Danner (danner.simon at gmail.com) wrote:

> In the !service case, the first file doesn't get closed automatically,
> since the second one uses the same FILE*. Close it explicitly.
> Found by cppcheck
> 
> Signed-off-by: Simon Danner <danner.simon at gmail.com>
> ---
>  src/dbus1-generator/dbus1-generator.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/src/dbus1-generator/dbus1-generator.c b/src/dbus1-generator/dbus1-generator.c
> index e1ffc55..e401471 100644
> --- a/src/dbus1-generator/dbus1-generator.c
> +++ b/src/dbus1-generator/dbus1-generator.c
> @@ -100,8 +100,7 @@ static int create_dbus_files(
>                          }
>                  }
>  
> -                fflush(f);
> -                if (ferror(f)) {
> +                if (fclose(f)) {
>                          log_error("Failed to write %s: %m", a);
>                          return -errno;
>                  }

It's actually more complicated. We need to set f to NULL too, otherwise
the cleanup logic might invoke fclose() again, on an invalidated FILE*.

I have now commited a patch that reworks this to use fflush_and_check()
and then closes the thing and resets f to NULL.

Thanks for the pointer!

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list