Skip to content

Commit 4a4e0aa

Browse files
committed
http.c: avoid danging pointer to local variable finished
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 baa7b67 (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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8963c6f commit 4a4e0aa

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

http-walker.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,9 @@ static void process_alternates_response(void *callback_data)
225225
alt_req->url->buf);
226226
active_requests++;
227227
slot->in_use = 1;
228-
if (slot->finished != NULL)
229-
(*slot->finished) = 0;
230228
if (!start_active_slot(slot)) {
231229
cdata->got_alternates = -1;
232230
slot->in_use = 0;
233-
if (slot->finished != NULL)
234-
(*slot->finished) = 1;
235231
}
236232
return;
237233
}

http.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ static void finish_active_slot(struct active_request_slot *slot)
197197
closedown_active_slot(slot);
198198
curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
199199

200-
if (slot->finished != NULL)
201-
(*slot->finished) = 1;
200+
slot->in_use = 0;
202201

203202
/* Store slot results so they can be read after the slot is reused */
204203
if (slot->results != NULL) {
@@ -1176,13 +1175,14 @@ struct active_request_slot *get_active_slot(void)
11761175
process_curl_messages();
11771176
}
11781177

1179-
while (slot != NULL && slot->in_use)
1178+
while (slot != NULL && (slot->in_use || slot->reserved_for_use))
11801179
slot = slot->next;
11811180

11821181
if (slot == NULL) {
11831182
newslot = xmalloc(sizeof(*newslot));
11841183
newslot->curl = NULL;
11851184
newslot->in_use = 0;
1185+
newslot->reserved_for_use = 0;
11861186
newslot->next = NULL;
11871187

11881188
slot = active_queue_head;
@@ -1204,7 +1204,6 @@ struct active_request_slot *get_active_slot(void)
12041204
active_requests++;
12051205
slot->in_use = 1;
12061206
slot->results = NULL;
1207-
slot->finished = NULL;
12081207
slot->callback_data = NULL;
12091208
slot->callback_func = NULL;
12101209
curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
@@ -1296,7 +1295,7 @@ void fill_active_slots(void)
12961295
}
12971296

12981297
while (slot != NULL) {
1299-
if (!slot->in_use && slot->curl != NULL
1298+
if (!slot->in_use && !slot->reserved_for_use && slot->curl
13001299
&& curl_session_count > min_curl_sessions) {
13011300
curl_easy_cleanup(slot->curl);
13021301
slot->curl = NULL;
@@ -1327,10 +1326,9 @@ void run_active_slot(struct active_request_slot *slot)
13271326
fd_set excfds;
13281327
int max_fd;
13291328
struct timeval select_timeout;
1330-
int finished = 0;
13311329

1332-
slot->finished = &finished;
1333-
while (!finished) {
1330+
slot->reserved_for_use = 1;
1331+
while (slot->in_use) {
13341332
step_active_slots();
13351333

13361334
if (slot->in_use) {
@@ -1367,6 +1365,7 @@ void run_active_slot(struct active_request_slot *slot)
13671365
select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
13681366
}
13691367
}
1368+
slot->reserved_for_use = 0;
13701369
}
13711370

13721371
static void release_active_slot(struct active_request_slot *slot)

http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ struct slot_results {
2222
struct active_request_slot {
2323
CURL *curl;
2424
int in_use;
25+
int reserved_for_use;
2526
CURLcode curl_result;
2627
long http_code;
27-
int *finished;
2828
struct slot_results *results;
2929
void *callback_data;
3030
void (*callback_func)(void *data);

0 commit comments

Comments
 (0)