pespin has submitted this change. ( 
https://gerrit.osmocom.org/c/libosmo-sigtran/+/42534?usp=email )

Change subject: osmo_ss7_find_free_{rctx,l_rk_id}(): Make more robust to 
wrap-around
......................................................................

osmo_ss7_find_free_{rctx,l_rk_id}(): Make more robust to wrap-around

Previous versions of those functions would end up broken over time after
~2^32 were allocated, turning the program into an unusuable stte.

Change-Id: Ie10f4048d0af384dc84c79e6fc441c8537649d55
---
M src/ss7_instance.c
1 file changed, 24 insertions(+), 8 deletions(-)

Approvals:
  pespin: Looks good to me, approved
  laforge: Looks good to me, but someone else must approve
  osmith: Looks good to me, but someone else must approve
  Jenkins Builder: Verified




diff --git a/src/ss7_instance.c b/src/ss7_instance.c
index 4aadc65..05f6be8 100644
--- a/src/ss7_instance.c
+++ b/src/ss7_instance.c
@@ -240,25 +240,41 @@
        return false;
 }

+/* Next RCTX, skipping special value "0" meaning no context. */
+static inline uint32_t rctx_inc(uint32_t rctx)
+{
+       return (rctx == UINT32_MAX) ? 1 : rctx + 1;
+}
+
 int osmo_ss7_find_free_rctx(struct osmo_ss7_instance *inst)
 {
-       int32_t rctx;
+       uint32_t rctx = next_rctx;

-       for (rctx = next_rctx; rctx; rctx = ++next_rctx) {
-               if (!osmo_ss7_as_find_by_rctx(inst, next_rctx))
+       OSMO_ASSERT(next_rctx != 0);
+
+       do {
+               if (!osmo_ss7_as_find_by_rctx(inst, rctx)) {
+                       next_rctx = rctx_inc(rctx);
                        return rctx;
-       }
+               }
+               rctx = rctx_inc(rctx);
+       } while (rctx != next_rctx);
+
        return -1;
 }

 uint32_t ss7_find_free_l_rk_id(struct osmo_ss7_instance *inst)
 {
-       uint32_t l_rk_id;
+       uint32_t l_rk_id = next_l_rk_id;

-       for (l_rk_id = next_l_rk_id; next_l_rk_id; l_rk_id = ++next_l_rk_id) {
-               if (!osmo_ss7_as_find_by_l_rk_id(inst, next_l_rk_id))
+       do {
+               if (!osmo_ss7_as_find_by_l_rk_id(inst, l_rk_id)) {
+                       next_l_rk_id = l_rk_id + 1;
                        return l_rk_id;
-       }
+               }
+               l_rk_id++;
+       } while (l_rk_id != next_l_rk_id);
+
        return -1;
 }


--
To view, visit https://gerrit.osmocom.org/c/libosmo-sigtran/+/42534?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: libosmo-sigtran
Gerrit-Branch: master
Gerrit-Change-Id: Ie10f4048d0af384dc84c79e6fc441c8537649d55
Gerrit-Change-Number: 42534
Gerrit-PatchSet: 2
Gerrit-Owner: pespin <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: osmith <[email protected]>
Gerrit-Reviewer: pespin <[email protected]>

Reply via email to