shithub: asif

Download patch

ref: 4c3d65037517a27151f9961ac359fcbdc62bbc1d
parent: 5a09545cf50764292013ea4d3156569e108aea85
author: qwx <qwx@sciops.net>
date: Tue Oct 4 20:57:00 EDT 2022

path/dijkstra: fix leak

--- a/path/dijkstra.c
+++ b/path/dijkstra.c
@@ -23,18 +23,6 @@
 static Zpool *zpool;
 
 static void
-cleanup(Pairheap **queue)
-{
-	Pairheap *p;
-
-	while((p = popqueue(queue)) != nil){
-		memset(p, 0, sizeof *p);
-		free(p);
-	}
-	znuke(zpool);
-}
-
-static void
 backtrack(Node *a, Node *b)
 {
 	Node *n;
@@ -111,7 +99,6 @@
 	nukequeue(&queue);
 	if(u != b)
 		return -1;
-	backtrack(a, b);
 	return 0;
 }
 
@@ -118,6 +105,8 @@
 int
 dijkstrafindpath(Node *a, Node *b)
 {
+	int r;
+
 	assert(a != nil && b != nil && a != b);
 	clearpath();
 	if(zpool == nil)
@@ -124,10 +113,12 @@
 		zpool = znew(sizeof(PNode));
 	dprint(Logdebug, "grid::dijkstrafindpath: dijkstra from [%#p,%P] to [%#p,%P]\n",
 		a, n2p(a), b, n2p(b));
-	if(dijkstra(a, b) < 0){
+	if((r = dijkstra(a, b)) < 0)
 		dprint(Logdebug, "grid::dijkstrafindpath: failed to find a path\n");
-		return -1;
-	}
-	dprintpath(a, b);
-	return 0;
+	else
+		backtrack(a, b);
+	znuke(zpool);
+	if(r >= 0)
+		dprintpath(a, b);
+	return r;
 }