[systemd-devel] initializing _cleanup_* variables [was: [PATCH 01/12] fsckd daemon for inter-fsckd communication]

Lennart Poettering lennart at poettering.net
Wed Jan 28 12:18:02 PST 2015


On Wed, 28.01.15 16:47, Martin Pitt (martin.pitt at ubuntu.com) wrote:

> Hey Zbyszek,
> 
> Zbigniew Jędrzejewski-Szmek [2015-01-28 15:37 +0100]:
> > > +static int handle_requests(int socket_fd) {
> > > +        Clients *first = NULL;
> > > +        usec_t last_activity = 0;
> > > +        int numdevices = 0, clear = 0;
> > > +        double percent = 100;
> > > +        _cleanup_fclose_ FILE *console = NULL;
> > NULL not necessary (and you don't have it in similar circumstances above ;))
> 
> How is that not necessary? Just because the very next line initializes
> it, and there is no exit path before? Because in the general case it
> certainly is necessary, otherwise the _cleanup_ handler will try to
> free/close/clean up a random pointer value and crash.
> 
> So IMHO it's a good defensive habit to always init _cleanup_* vars
> with NULL. In the future someone might put some code with "return"
> before the fopen() below, and then get a crash.

Yeah, nowadays I tend to always initialize _cleanup_ vars at the time
i declare them. It's better to be safe than sorry...

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list