From: Sipwise Development Team <support@sipwise.com>
Date: Thu, 26 Mar 2020 10:06:46 +0100
Subject: fix_lcr_rate_wrong_id

---
 src/modules/lcr_rate/lcr_rate_mod.c | 38 ++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/modules/lcr_rate/lcr_rate_mod.c b/src/modules/lcr_rate/lcr_rate_mod.c
index f3a9ab2..f35e577 100644
--- a/src/modules/lcr_rate/lcr_rate_mod.c
+++ b/src/modules/lcr_rate/lcr_rate_mod.c
@@ -153,7 +153,7 @@ static struct peer *load_peers(int *num, char *src_user, char *src_domain, char
 	struct usr_avp *avp;
 	int_str val;
 	struct peer *ret, *j;
-	int len, i;
+	int len, i, k;
 	char *c;
 	str s;
 	swr_rate_t rate;
@@ -198,15 +198,39 @@ static struct peer *load_peers(int *num, char *src_user, char *src_domain, char
 			return NULL;
 		}
 		memcpy(j->s.s, val.s.s, val.s.len);
-		c = memrchr(j->s.s, '|', val.s.len);
-		if (!c) {
-			LM_ERR("separator not found in string <%.*s>\n", val.s.len, j->s.s);
-			goto next;
+
+		/* lcr gw fields:
+		 *  0: gw index
+		 *  1: scheme
+		 *  2: strip
+		 *  3: prefix
+		 *  4: tag
+		 *  5: ip adr
+		 *  6: hostname
+		 *  7: port
+		 *  8: params
+		 *  9: transport
+		 * 10: flags
+		 * 11: rule id
+		 */
+		c = j->s.s;
+		for (k = 0; k < 10; k++) {
+			c = memchr(c, '|', val.s.len - (c - j->s.s));
+			if (!c) {
+				LM_ERR("separator not found in string <%.*s> (field #%i)\n",
+						val.s.len, j->s.s, k);
+				goto next;
+			}
+			c++;
 		}
 
-		c++;
 		s.s = c;
-		s.len = val.s.len - (c - j->s.s);
+
+		/* find terminator */
+		c = memchr(c, '|', val.s.len - (c - j->s.s));
+		if (!c)
+			c = j->s.s + val.s.len;
+		s.len = c - s.s;
 		if (str2int(&s, &j->id)) {
 			LM_ERR("could not convert string <%.*s> to int\n", s.len, s.s);
 			goto next;
