Skip to content

Commit a94cf2c

Browse files
drafnelgitster
authored andcommitted
http.c: don't rewrite the user:passwd string multiple times
Curl older than 7.17 (RHEL 4.X provides 7.12 and RHEL 5.X provides 7.15) requires that we manage any strings that we pass to it as pointers. So, we really shouldn't be modifying this strbuf after we have passed it to curl. Our interaction with curl is currently safe (before or after this patch) since the pointer that is passed to curl is never invalidated; it is repeatedly rewritten with the same sequence of characters but the strbuf functions never need to allocate a larger string, so the same memory buffer is reused. This "guarantee" of safety is somewhat subtle and could be overlooked by someone who may want to add a more complex handling of the username and password. So, let's stop modifying this strbuf after we have passed it to curl, but also leave a note to describe the assumptions that have been made about username/password lifetime and to draw attention to the code. Signed-off-by: Brandon Casey <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 04a74b6 commit a94cf2c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

http.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,15 @@ static void init_curl_http_auth(CURL *result)
222222
#else
223223
{
224224
static struct strbuf up = STRBUF_INIT;
225-
strbuf_reset(&up);
226-
strbuf_addf(&up, "%s:%s",
227-
http_auth.username, http_auth.password);
225+
/*
226+
* Note that we assume we only ever have a single set of
227+
* credentials in a given program run, so we do not have
228+
* to worry about updating this buffer, only setting its
229+
* initial value.
230+
*/
231+
if (!up.len)
232+
strbuf_addf(&up, "%s:%s",
233+
http_auth.username, http_auth.password);
228234
curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
229235
}
230236
#endif

0 commit comments

Comments
 (0)