On Tue, Jul 1, 2014 at 12:53 PM, Lennart Poettering <[email protected]>
wrote:

> On Tue, 01.07.14 11:58, Eugene Yakubovich ([email protected])
> wrote:
>
> > +int sd_dhcp_client_set_hostname(sd_dhcp_client *client,
> > +                        const char *hostname) {
> > +
> > +        assert_return(client, -EINVAL);
> > +
> > +        if (streq_ptr(client->hostname, hostname))
> > +                return 0;
> > +
> > +        free(client->hostname);
> > +
> > +        if (hostname) {
> > +                client->hostname = strdup(hostname);
> > +                if (!client->hostname)
> > +                        return -ENOMEM;
> > +        } else
> > +                client->hostname = NULL;
>
> I'd always recommend allocating the new string first, and only then
> freeing the old string. That way the function either fails with no
> change to the object, or it succeeds, but never fails with a changed
> object.
>

Absolutely. I have no idea what I was thinking.
_______________________________________________
systemd-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Reply via email to