Skip to content

Commit e72a452

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 cc18d82 commit e72a452

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
@@ -229,13 +229,9 @@ static void process_alternates_response(void *callback_data)
229229
alt_req->url->buf);
230230
active_requests++;
231231
slot->in_use = 1;
232-
if (slot->finished != NULL)
233-
(*slot->finished) = 0;
234232
if (!start_active_slot(slot)) {
235233
cdata->got_alternates = -1;
236234
slot->in_use = 0;
237-
if (slot->finished != NULL)
238-
(*slot->finished) = 1;
239235
}
240236
return;
241237
}

http.c

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

233-
if (slot->finished != NULL)
234-
(*slot->finished) = 1;
233+
slot->in_use = 0;
235234

236235
/* Store slot results so they can be read after the slot is reused */
237236
if (slot->results != NULL) {
@@ -1308,13 +1307,14 @@ struct active_request_slot *get_active_slot(void)
13081307
}
13091308
#endif
13101309

1311-
while (slot != NULL && slot->in_use)
1310+
while (slot != NULL && (slot->in_use || slot->reserved_for_use))
13121311
slot = slot->next;
13131312

13141313
if (slot == NULL) {
13151314
newslot = xmalloc(sizeof(*newslot));
13161315
newslot->curl = NULL;
13171316
newslot->in_use = 0;
1317+
newslot->reserved_for_use = 0;
13181318
newslot->next = NULL;
13191319

13201320
slot = active_queue_head;
@@ -1340,7 +1340,6 @@ struct active_request_slot *get_active_slot(void)
13401340
active_requests++;
13411341
slot->in_use = 1;
13421342
slot->results = NULL;
1343-
slot->finished = NULL;
13441343
slot->callback_data = NULL;
13451344
slot->callback_func = NULL;
13461345
curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
@@ -1439,7 +1438,7 @@ void fill_active_slots(void)
14391438
}
14401439

14411440
while (slot != NULL) {
1442-
if (!slot->in_use && slot->curl != NULL
1441+
if (!slot->in_use && !slot->reserved_for_use && slot->curl
14431442
&& curl_session_count > min_curl_sessions) {
14441443
curl_easy_cleanup(slot->curl);
14451444
slot->curl = NULL;
@@ -1472,10 +1471,9 @@ void run_active_slot(struct active_request_slot *slot)
14721471
fd_set excfds;
14731472
int max_fd;
14741473
struct timeval select_timeout;
1475-
int finished = 0;
14761474

1477-
slot->finished = &finished;
1478-
while (!finished) {
1475+
slot->reserved_for_use = 1;
1476+
while (slot->in_use) {
14791477
step_active_slots();
14801478

14811479
if (slot->in_use) {
@@ -1517,6 +1515,7 @@ void run_active_slot(struct active_request_slot *slot)
15171515
select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
15181516
}
15191517
}
1518+
slot->reserved_for_use = 0;
15201519
#else
15211520
while (slot->in_use) {
15221521
slot->curl_result = curl_easy_perform(slot->curl);

http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ struct slot_results {
6464
struct active_request_slot {
6565
CURL *curl;
6666
int in_use;
67+
int reserved_for_use;
6768
CURLcode curl_result;
6869
long http_code;
69-
int *finished;
7070
struct slot_results *results;
7171
void *callback_data;
7272
void (*callback_func)(void *data);

0 commit comments

Comments
 (0)