[PATCH] openbsc[master]: libmsc: db_subscriber_alloc_exten() remove infinite loop

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Keith Whyte gerrit-no-reply at lists.osmocom.org
Thu Sep 14 12:35:36 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/3910

to look at the new patch set (#4).

libmsc: db_subscriber_alloc_exten() remove infinite loop

This patch  prevents entering an infinite loop in the
case that you actually have exhasuted all available
extensions.
FIXME: How best to check for free extensions in the pool.
TODO: If there is no extension available, then do not
create the subscriber.

Change-Id: Icf0f1e5a7f360bc27592a55890f74a9a12bc9f42
---
M openbsc/src/libmsc/db.c
1 file changed, 42 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/10/3910/4

diff --git a/openbsc/src/libmsc/db.c b/openbsc/src/libmsc/db.c
index 4ba12ca..329fa98 100644
--- a/openbsc/src/libmsc/db.c
+++ b/openbsc/src/libmsc/db.c
@@ -1407,19 +1407,49 @@
 	return 0;
 }
 
+uint64_t extension_alloc(uint64_t smin, uint64_t smax) {
+	return smin + (rand() % (smax - smin));
+}
+
 int db_subscriber_alloc_exten(struct gsm_subscriber *subscriber, uint64_t smin,
 			      uint64_t smax)
 {
 	dbi_result result = NULL;
 	uint64_t try;
+	unsigned long long total;
+	int i;
+	int available;
 
-	for (;;) {
-		try = (rand() % (smax - smin + 1) + smin);
+	result = dbi_conn_query(conn,
+		"SELECT count(*) AS total FROM Subscriber "
+		"WHERE extension IS NOT NULL");
+
+	if (!result) {
+		LOGP(DDB, LOGL_ERROR, "Failed to get subscriber count "
+			"while allocating new extension.\n");
+		return 1;
+	}
+
+	dbi_result_next_row(result);
+	total = dbi_result_get_ulonglong(result, "total");
+	available = smax - smin - total;
+
+	if (available < 0) {
+		LOGP(DDB, LOGL_ERROR, "Only %llu/%llu extensions available\n", available, total);
+		return 1;
+	}
+
+	for (i = 0; i < (smax - smin)*2; i++) {
+
+		if (!(i % 100) || try > smax) {
+			try = extension_alloc(smin, smax);
+		}
+
 		result = dbi_conn_queryf(conn,
 			"SELECT * FROM Subscriber "
 			"WHERE extension = %"PRIu64,
-			try
-		);
+			try);
+
 		if (!result) {
 			LOGP(DDB, LOGL_ERROR, "Failed to query Subscriber "
 				"while allocating new extension.\n");
@@ -1427,6 +1457,7 @@
 		}
 		if (dbi_result_get_numrows(result)){
 			dbi_result_free(result);
+			try++;
 			continue;
 		}
 		if (!dbi_result_next_row(result)) {
@@ -1435,6 +1466,13 @@
 		}
 		dbi_result_free(result);
 	}
+
+	if (i > (smax - smin)*2 ) {
+		DEBUGP(DDB, "Out of Loop, no extension available for IMSI %s.\n",
+			subscriber->imsi);
+		return 1;
+		}
+
 	sprintf(subscriber->extension, "%"PRIu64, try);
 	DEBUGP(DDB, "Allocated extension %"PRIu64 " for IMSI %s.\n", try, subscriber->imsi);
 	return db_sync_subscriber(subscriber);

-- 
To view, visit https://gerrit.osmocom.org/3910
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: Icf0f1e5a7f360bc27592a55890f74a9a12bc9f42
Gerrit-PatchSet: 4
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Keith Whyte <keith at rhizomatica.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Keith Whyte <keith at rhizomatica.org>
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Pablo Neira Ayuso <pablo at gnumonks.org>



More information about the gerrit-log mailing list