From: Jeff King <peff@peff.net>
To: Brandon Casey <bcasey@nvidia.com>
Cc: git@vger.kernel.org, gitster@pobox.com,
Brandon Casey <drafnel@gmail.com>
Subject: Re: [PATCH] http.c: don't rewrite the user:passwd string multiple times
Date: Tue, 18 Jun 2013 01:19:03 -0400 [thread overview]
Message-ID: <20130618051902.GA5916@sigill.intra.peff.net> (raw)
In-Reply-To: <1371520840-24906-1-git-send-email-bcasey@nvidia.com>
On Mon, Jun 17, 2013 at 07:00:40PM -0700, Brandon Casey wrote:
> Curl requires that we manage any strings that we pass to it as pointers.
> So, we should not be overwriting this strbuf after we've passed it to
> curl.
My understanding of curl's pointer requirements are:
1. Older versions of curl (and I do not recall which version off-hand,
but it is not important) stored just the pointer. Calling code was
required to manage the string lifetime itself.
2. Newer versions of curl will strdup the string in curl_easy_setopt.
So we do not have to worry about newer versions, as they do not care
about our pointer after curl_easy_setopt returns.
For older versions, if we were to grow the strbuf, we might free() the
pointer provided to an earlier call to curl_easy_setopt. But since we
are about to call curl_easy_setopt with the new value, I would assume
that curl will never actually look at the old one (i.e., when replacing
an old pointer, it would not dereference it, but simply overwrite it
with the new value).
So for a single curl handle, I don't think it is a problem.
It could be a problem when we have multiple handles in play
simultaneously (we invalidate the pointer that another simultaneous
handle is using, but do not immediately reset its pointer).
> Additionally, it is unnecessary since we only prompt for the user name
> and password once, so we end up overwriting the strbuf with the same
> sequence of characters each time. This is why in practice it has not
> caused any problems for git's use of curl; the internal strbuf char
> pointer does not change, and get's overwritten with the same string
> each time.
In the current code, yes, we only do this once (and if we have a
username/password from the URL, we do not re-prompt if that fails).
> diff --git a/http.c b/http.c
> index 92aba59..6828269 100644
> --- a/http.c
> +++ b/http.c
> @@ -228,8 +228,8 @@ static void init_curl_http_auth(CURL *result)
> #else
> {
> static struct strbuf up = STRBUF_INIT;
> - strbuf_reset(&up);
> - strbuf_addf(&up, "%s:%s",
> + if (!up.len)
> + strbuf_addf(&up, "%s:%s",
> http_auth.username, http_auth.password);
> curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
This is correct for the current code because of the reasoning above.
I'm slightly negative on this only because it feels like we are setting
a trap for somebody who later wants to do:
for (sanity = 0; sanity < 5; sanity++) {
int ret = http_request(...);
if (ret != HTTP_REAUTH)
return ret;
}
to give the user a few chances to input. We would continue to update
the credential struct but never actually give the new value to curl.
Another option would be to just use a static fixed-size buffer. That
removes all memory management issues.
I dunno. Maybe I am being too picky, as I do not have plans to do
anything like the above (since we don't do significant work before the
http contact, there is no reason not to just die() and let the user
re-run the shell command). I'd also be OK with just putting a comment
above the code in question to say something like "Note that we assume we
only ever have a single set of credentials in a given program run, so we
do not have to worry about updating this buffer, only setting its
initial value". Then the trap at least has a warning sign. :)
What do you think?
-Peff
next prev parent reply other threads:[~2013-06-18 5:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-18 2:00 [PATCH] http.c: don't rewrite the user:passwd string multiple times Brandon Casey
2013-06-18 4:15 ` Eric Sunshine
2013-06-18 5:19 ` Jeff King [this message]
2013-06-18 6:36 ` Daniel Stenberg
2013-06-18 15:32 ` Junio C Hamano
2013-06-18 19:29 ` Brandon Casey
2013-06-18 22:13 ` Jeff King
2013-06-19 2:41 ` Brandon Casey
2013-06-19 2:43 ` [PATCH v2] " Brandon Casey
2013-06-19 5:26 ` Jeff King
2013-06-19 7:40 ` [PATCH] " Daniel Stenberg
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=20130618051902.GA5916@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=bcasey@nvidia.com \
--cc=drafnel@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.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).