From 817934b56876866bf18905a5a70c1bbfc90ddc5e Mon Sep 17 00:00:00 2001
From: Kipp Cannon <kipp.cannon@ligo.org>
Date: Thu, 14 Feb 2019 20:22:44 -0800
Subject: [PATCH] gstlal_snglinspiral_array_from_file(): fix return value

- oops, I was zeroing the row count before returning
---
 gstlal-inspiral/lib/gstlal_snglinspiral.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/gstlal-inspiral/lib/gstlal_snglinspiral.c b/gstlal-inspiral/lib/gstlal_snglinspiral.c
index 21a7d596c2..9c1123f125 100644
--- a/gstlal-inspiral/lib/gstlal_snglinspiral.c
+++ b/gstlal-inspiral/lib/gstlal_snglinspiral.c
@@ -184,6 +184,7 @@ static int sngl_inspiral_row_callback(struct ligolw_table *table, struct ligolw_
 int gstlal_snglinspiral_array_from_file(const char *filename, SnglInspiralTable **bankarray)
 {
 	SnglInspiralTable *head = NULL;
+	SnglInspiralTable *row;
 	ezxml_t xmldoc;
 	ezxml_t elem;
 	struct ligolw_table *table;
@@ -229,6 +230,13 @@ int gstlal_snglinspiral_array_from_file(const char *filename, SnglInspiralTable
 
 	ezxml_free(xmldoc);
 
+	/*
+	 * count rows.  can't use table->n_rows because the callback
+	 * interecepted the rows, and the table object is empty
+	 */
+
+	for(num = 0, row = head; row; row = row->next, num++);
+
 	/*
 	 * copy the linked list of templates into the template array in
 	 * reverse order.  the linked list is reversed with respect to the
@@ -236,14 +244,8 @@ int gstlal_snglinspiral_array_from_file(const char *filename, SnglInspiralTable
 	 * in the order in which they appear in the file.
 	 */
 
-	{
-	/* can't use table->n_rows because the callback interecepted the
-	 * rows, and the table object is empty */
-	SnglInspiralTable *row = head;
-	for(num = 0; row; row = row->next, num++);
-	}
 	*bankarray = calloc(num, sizeof(**bankarray));
-	while(num--) {
+	for(row = &(*bankarray)[num - 1]; head; row--) {
 		SnglInspiralTable *next = head->next;
 
 		/* fix broken columns */
@@ -253,7 +255,7 @@ int gstlal_snglinspiral_array_from_file(const char *filename, SnglInspiralTable
 		head->mchirp = gstlal_mchirp(head->mass1, head->mass2);
 		head->eta = gstlal_eta(head->mass1, head->mass2);
 
-		(*bankarray)[num] = *head;
+		*row = *head;
 		LALFree(head);
 		head = next;
 	}
-- 
GitLab