From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH 3/4] http.c: avoid danging pointer to local variable `finished`
Date: Tue, 24 May 2022 00:23:05 +0000 [thread overview]
Message-ID: <4a4e0aa0a49a54eea88f9c2d8e1db6a697012718.1653351786.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1238.git.1653351786.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>
In http.c, the run_active_slot() function allows the given "slot" to
make progress by calling step_active_slots() in a loop repeatedly, and
the loop is not left until the request held in the slot completes.
Ages ago, we used to use the slot->in_use member to get out of the loop,
which misbehaved when the request in "slot" completes (at which time,
the result of the request is copied away from the slot, and the in_use
member is cleared, making the slot ready to be reused), and the "slot"
gets reused to service a different request (at which time, the "slot"
becomes in_use again, even though it is for a different request). The
loop terminating condition mistakenly thought that the original request
has yet to be completed.
Today's code, after baa7b67d (HTTP slot reuse fixes, 2006-03-10) fixed
this issue, uses a separate "slot->finished" member that is set in
run_active_slot() to point to an on-stack variable, and the code that
completes the request in finish_active_slot() clears the on-stack
variable via the pointer to signal that the particular request held by
the slot has completed. It also clears the in_use member (as before
that fix), so that the slot itself can safely be reused for an unrelated
request.
One thing that is not quite clean in this arrangement is that, unless
the slot gets reused, at which point the finished member is reset to
NULL, the member keeps the value of &finished, which becomes a dangling
pointer into the stack when run_active_slot() returns.
Let's drop that local variable and introduce a new flag in the slot that
is used to indicate that even while the slot is no longer in use, it is
still reserved until further notice. It is the responsibility of
`run_active_slot()` to clear that flag once it is done with that slot.
Initial-patch-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
http-walker.c | 4 ----
http.c | 15 +++++++--------
http.h | 2 +-
3 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/http-walker.c b/http-walker.c
index 910fae539b8..5cc369dea85 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -225,13 +225,9 @@ static void process_alternates_response(void *callback_data)
alt_req->url->buf);
active_requests++;
slot->in_use = 1;
- if (slot->finished != NULL)
- (*slot->finished) = 0;
if (!start_active_slot(slot)) {
cdata->got_alternates = -1;
slot->in_use = 0;
- if (slot->finished != NULL)
- (*slot->finished) = 1;
}
return;
}
diff --git a/http.c b/http.c
index f92859f43fa..00206676597 100644
--- a/http.c
+++ b/http.c
@@ -197,8 +197,7 @@ static void finish_active_slot(struct active_request_slot *slot)
closedown_active_slot(slot);
curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
- if (slot->finished != NULL)
- (*slot->finished) = 1;
+ slot->in_use = 0;
/* Store slot results so they can be read after the slot is reused */
if (slot->results != NULL) {
@@ -1176,13 +1175,14 @@ struct active_request_slot *get_active_slot(void)
process_curl_messages();
}
- while (slot != NULL && slot->in_use)
+ while (slot != NULL && (slot->in_use || slot->reserved_for_use))
slot = slot->next;
if (slot == NULL) {
newslot = xmalloc(sizeof(*newslot));
newslot->curl = NULL;
newslot->in_use = 0;
+ newslot->reserved_for_use = 0;
newslot->next = NULL;
slot = active_queue_head;
@@ -1204,7 +1204,6 @@ struct active_request_slot *get_active_slot(void)
active_requests++;
slot->in_use = 1;
slot->results = NULL;
- slot->finished = NULL;
slot->callback_data = NULL;
slot->callback_func = NULL;
curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
@@ -1296,7 +1295,7 @@ void fill_active_slots(void)
}
while (slot != NULL) {
- if (!slot->in_use && slot->curl != NULL
+ if (!slot->in_use && !slot->reserved_for_use && slot->curl
&& curl_session_count > min_curl_sessions) {
curl_easy_cleanup(slot->curl);
slot->curl = NULL;
@@ -1327,10 +1326,9 @@ void run_active_slot(struct active_request_slot *slot)
fd_set excfds;
int max_fd;
struct timeval select_timeout;
- int finished = 0;
- slot->finished = &finished;
- while (!finished) {
+ slot->reserved_for_use = 1;
+ while (slot->in_use) {
step_active_slots();
if (slot->in_use) {
@@ -1367,6 +1365,7 @@ void run_active_slot(struct active_request_slot *slot)
select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
}
}
+ slot->reserved_for_use = 0;
}
static void release_active_slot(struct active_request_slot *slot)
diff --git a/http.h b/http.h
index df1590e53a4..3b2f6da570c 100644
--- a/http.h
+++ b/http.h
@@ -22,9 +22,9 @@ struct slot_results {
struct active_request_slot {
CURL *curl;
int in_use;
+ int reserved_for_use;
CURLcode curl_result;
long http_code;
- int *finished;
struct slot_results *results;
void *callback_data;
void (*callback_func)(void *data);
--
gitgitgadget
next prev parent reply other threads:[~2022-05-24 0:23 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-24 0:23 [PATCH 0/4] ci: fix windows-build with GCC v12.x Johannes Schindelin via GitGitGadget
2022-05-24 0:23 ` [PATCH 1/4] compat/win32/syslog: fix use-after-realloc Johannes Schindelin via GitGitGadget
2022-05-24 12:39 ` Johannes Schindelin
2022-05-24 20:58 ` Junio C Hamano
2022-05-24 0:23 ` [PATCH 2/4] nedmalloc: avoid new compile error Johannes Schindelin via GitGitGadget
2022-05-24 8:00 ` Ævar Arnfjörð Bjarmason
2022-05-24 15:59 ` René Scharfe
2022-05-24 20:46 ` Johannes Schindelin
2022-05-24 21:33 ` Ævar Arnfjörð Bjarmason
2022-05-24 0:23 ` Johannes Schindelin via GitGitGadget [this message]
2022-05-24 7:58 ` [PATCH 3/4] http.c: avoid danging pointer to local variable `finished` Ævar Arnfjörð Bjarmason
2022-05-24 20:06 ` Junio C Hamano
2022-05-24 21:15 ` Johannes Schindelin
2022-05-24 21:45 ` Ævar Arnfjörð Bjarmason
2022-05-24 22:38 ` Junio C Hamano
2022-05-25 10:16 ` Johannes Schindelin
2022-05-25 12:48 ` Ævar Arnfjörð Bjarmason
2022-05-24 0:23 ` [PATCH 4/4] dir.c: avoid "exceeds maximum object size" error with GCC v12.x Johannes Schindelin via GitGitGadget
2022-05-24 5:53 ` Ævar Arnfjörð Bjarmason
2022-05-24 21:05 ` Johannes Schindelin
2022-05-25 13:39 ` Derrick Stolee
2022-05-25 18:27 ` Junio C Hamano
2022-05-24 15:54 ` [PATCH 0/4] ci: fix windows-build " Jeff Hostetler
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=4a4e0aa0a49a54eea88f9c2d8e1db6a697012718.1653351786.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=johannes.schindelin@gmx.de \
/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).