git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] negotiator/skipping: avoid stack overflow
@ 2022-10-25 23:29 Jonathan Tan
  2022-10-25 23:52 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Tan @ 2022-10-25 23:29 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan

mark_common() in negotiator/skipping.c may overflow the stack due to
recursive function calls. Avoid this by instead recursing using a
heap-allocated data structure.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
This was observed at $DAYJOB.

To reviewers, you can check the diff with:
  git show --color-moved-ws=allow-indentation-change --color-moved
to more clearly show which lines are new and which lines have only
changed indentation.
---
 negotiator/skipping.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/negotiator/skipping.c b/negotiator/skipping.c
index c4398f5ae1..0f5ac48e87 100644
--- a/negotiator/skipping.c
+++ b/negotiator/skipping.c
@@ -86,21 +86,26 @@ static int clear_marks(const char *refname, const struct object_id *oid,
 /*
  * Mark this SEEN commit and all its SEEN ancestors as COMMON.
  */
-static void mark_common(struct data *data, struct commit *c)
+static void mark_common(struct data *data, struct commit *seen_commit)
 {
-	struct commit_list *p;
+	struct prio_queue queue = { NULL };
+	struct commit *c;
 
-	if (c->object.flags & COMMON)
-		return;
-	c->object.flags |= COMMON;
-	if (!(c->object.flags & POPPED))
-		data->non_common_revs--;
+	prio_queue_put(&queue, seen_commit);
+	while ((c = prio_queue_get(&queue))) {
+		struct commit_list *p;
+		if (c->object.flags & COMMON)
+			return;
+		c->object.flags |= COMMON;
+		if (!(c->object.flags & POPPED))
+			data->non_common_revs--;
 
-	if (!c->object.parsed)
-		return;
-	for (p = c->parents; p; p = p->next) {
-		if (p->item->object.flags & SEEN)
-			mark_common(data, p->item);
+		if (!c->object.parsed)
+			return;
+		for (p = c->parents; p; p = p->next) {
+			if (p->item->object.flags & SEEN)
+				prio_queue_put(&queue, p->item);
+		}
 	}
 }
 
-- 
2.38.0.135.g90850a2211-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] negotiator/skipping: avoid stack overflow
  2022-10-25 23:29 [PATCH] negotiator/skipping: avoid stack overflow Jonathan Tan
@ 2022-10-25 23:52 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2022-10-25 23:52 UTC (permalink / raw)
  To: Jonathan Tan; +Cc: git

Jonathan Tan <jonathantanmy@google.com> writes:

> mark_common() in negotiator/skipping.c may overflow the stack due to
> recursive function calls. Avoid this by instead recursing using a
> heap-allocated data structure.

Very straight-forward.  I think we've done quite a many of these in
various places by now ;-)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-25 23:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-25 23:29 [PATCH] negotiator/skipping: avoid stack overflow Jonathan Tan
2022-10-25 23:52 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).