From: Simon Horman <horms@verge.net.au>
To: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Kexec Mailing List <kexec@lists.infradead.org>
Subject: Re: [PATCH] Fix --reuse-cmdline so it is usable.
Date: Tue, 2 Feb 2010 11:07:37 +1100 [thread overview]
Message-ID: <20100202000737.GA23305@verge.net.au> (raw)
In-Reply-To: <m1mxztos4s.fsf@fess.ebiederm.org>
On Mon, Feb 01, 2010 at 08:08:51AM -0800, Eric W. Biederman wrote:
> Simon Horman <horms@verge.net.au> writes:
>
> > On Mon, Jan 25, 2010 at 12:14:03AM -0800, Eric W. Biederman wrote:
> >> Simon Horman <horms@verge.net.au> writes:
> >>
> >> > On Tue, Jan 19, 2010 at 12:05:03AM -0800, Eric W. Biederman wrote:
> >> >>
> >> >> A colleague of mine implemented kdump and it used --reuse-cmdline
> >> >> with some rather interesting and unexpected results.
> >> >>
> >> >> Update the getopt specification so that --reuse-cmdline does not
> >> >> attempt to take an argument that it will not use.
> >> >>
> >> >> Update the processing of --append so that --reuse-cmdline followed
> >> >> by --append actually appends the parameters specified by --reuse-cmdline.
> >> >
> >> > Hi Eric,
> >> >
> >> > sorry for being slow. Been semi-offline for LCA and am now catching
> >> > up on things.
> >>
> >> No problem, I am pretty out of it right now as well.
> >>
> >> > [snip]
> >> >
> >> >> diff --git a/kexec/kexec.c b/kexec/kexec.c
> >> >> index a1cec86..f4c22a6 100644
> >> >> --- a/kexec/kexec.c
> >> >> +++ b/kexec/kexec.c
> >> >> @@ -994,6 +994,22 @@ void check_reuse_initrd(void)
> >> >> free(line);
> >> >> }
> >> >>
> >> >> +const char *concat_cmdline(const char *base, const char *append)
> >> >> +{
> >> >> + const char *cmdline;
> >> >> + if (!base && !append)
> >> >> + return NULL;
> >> >> + if (!base)
> >> >> + return append;
> >> >> + if (!append)
> >> >> + return base;
> >> >> + cmdline = xmalloc(strlen(base) + 1 + strlen(append) + 1);
> >> >> + strcpy(cmdline, base);
> >> >> + strcat(cmdline, " ");
> >> >> + strcat(cmdline, append);
> >> >> + return cmdline;
> >> >> +}
> >> >> +
> >> >
> >> > This introduces a memory leak.
> >>
> >> Yep.
> >>
> >> > Perhaps it should strdup append and base in the !base and !append cases
> >> > respectively and expect the caller to always call free.
> >> >
> >> > I realise that its a small leak in a programme that will soon exit anyway.
> >> > But for the sake of being able to use tools like valgrind to analyse
> >> > problems it seems to me that leaks are worth avoiding. (Not that I have
> >> > run valgrind on kexec-tools to see what happens :-)
> >>
> >> I see your point but I think we already have a memory leak here (
> >> Where does the memory that getopt uses come from? ), and I think on a
> >> trivial application like /sbin/kexec that is simply not long running
> >> it can't matter. I'm even willing to call not freeing memory
> >> explicitly a performance optimization in cases like this ;)
> >
> > Clearly this is a matter of taste. And as it happens I fall on
> > the side of the fence that thinks that the leak should be avoided.
> >
> > I propose applying the following after your patch:
> >
> > From: Simon Horman <horms@verge.net.au>
> >
> > don't leak in concat_cmdline
>
> It is a bit of a shame that we loose the const attributes.
Indeed, though it seems to be at least partially broken in your original patch.
# gcc --version
gcc (Debian 4.4.2-8) 4.4.2
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# make
kexec/kexec.c: In function ‘concat_cmdline’:
kexec/kexec.c:1007: warning: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type
/usr/include/string.h:127: note: expected ‘char * __restrict__’ but argument is of type ‘const char *’
kexec/kexec.c:1008: warning: passing argument 1 of ‘strcat’ discards qualifiers from pointer target type
/usr/include/string.h:135: note: expected ‘char * __restrict__’ but argument is of type ‘const char *’
kexec/kexec.c:1009: warning: passing argument 1 of ‘strcat’ discards qualifiers from pointer target type
/usr/include/string.h:135: note: expected ‘char * __restrict__’ but argument is of type ‘const char *’
> Beyond that the idiom
> if (xyz)
> free(xyz)
> can just become:
> free(xyz)
concat_cmdline() may return NULL in the case where both
base and append are NULL.
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
next prev parent reply other threads:[~2010-02-02 0:07 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-19 8:05 [PATCH] Fix --reuse-cmdline so it is usable Eric W. Biederman
2010-01-25 7:23 ` Simon Horman
2010-01-25 8:14 ` Eric W. Biederman
2010-01-25 21:34 ` Bernhard Walle
2010-02-01 12:10 ` Simon Horman
2010-02-01 16:08 ` Eric W. Biederman
2010-02-02 0:07 ` Simon Horman [this message]
2010-02-02 0:21 ` Eric W. Biederman
2010-02-02 3:04 ` Simon Horman
2010-02-02 3:45 ` Simon Horman
2010-02-02 3:51 ` Eric W. Biederman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100202000737.GA23305@verge.net.au \
--to=horms@verge.net.au \
--cc=ebiederm@xmission.com \
--cc=kexec@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox