From: Jeff King <peff@peff.net>
To: Erik Faye-Lund <kusmabite@gmail.com>
Cc: git@vger.kernel.org, jwa@urbancode.com, drew.northup@maine.edu
Subject: Re: [PATCH] config: support values longer than 1024 bytes
Date: Tue, 5 Apr 2011 20:52:23 -0400 [thread overview]
Message-ID: <20110406005223.GA10374@sigill.intra.peff.net> (raw)
In-Reply-To: <1302046203-4408-1-git-send-email-kusmabite@gmail.com>
On Wed, Apr 06, 2011 at 01:30:03AM +0200, Erik Faye-Lund wrote:
> The rather awkward return statement with strdup("") is because
> strbuf_detach returns NULL when there's nothing allocated. Even
> worse, it returns an uninitialized string if the string has been
> initialized with a non-zero 'hint'.
That seems like two bugs in the strbuf code.
I would expect strbuf_detach to _always_ return an allocated buffer,
even if it is xstrdup(""). Though the code in strbuf_detach is explicit
about returning NULL rather than slopbuf, so perhaps there is a case
where it is useful.
But for the other, one of the invariants of strbuf is that the string is
always NUL-terminated. So I would expect strbuf_init to properly
NUL-terminate after growing based on the hint.
> static char *parse_value(void)
> {
> - static char value[1024];
> - int quote = 0, comment = 0, len = 0, space = 0;
> + struct strbuf value = STRBUF_INIT;
> + int quote = 0, comment = 0, space = 0;
One thing you could about the strbuf_detach thing is to just use a
single static strbuf instead of the static array, and just reset its
length back to zero on each call. So:
static struct strbuf value = STRBUF_INIT;
...
strbuf_reset(&value);
> - value[len] = 0;
> - return value;
> + return value.len ?
> + strbuf_detach(&value, NULL) :
> + strdup("");
Then this just becomes:
return value.buf;
> default:
> + strbuf_release(&value);
> return NULL;
And you can drop the release.
> @@ -226,7 +226,9 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
> if (!value)
> return -1;
> }
> - return fn(name, value, data);
> + ret = fn(name, value, data);
> + free(value);
> + return ret;
And drop this hunk, since callers no longer need to free.
I do wonder, though, if we could be reusing the unquote_c_style()
function in quote.c. They are obviously similar, but I haven't checked
if there is more going on in the config code.
-Peff
next prev parent reply other threads:[~2011-04-06 0:52 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-05 23:30 [PATCH] config: support values longer than 1024 bytes Erik Faye-Lund
2011-04-05 23:38 ` Erik Faye-Lund
2011-04-06 0:52 ` Jeff King [this message]
2011-04-06 9:10 ` Erik Faye-Lund
2011-04-06 15:35 ` Jeff King
2011-04-06 16:16 ` Erik Faye-Lund
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=20110406005223.GA10374@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=drew.northup@maine.edu \
--cc=git@vger.kernel.org \
--cc=jwa@urbancode.com \
--cc=kusmabite@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).