[sr-dev] git:master:2ae296f5: posops: handle negative index parameter for find functions

Daniel-Constantin Mierla miconda at gmail.com
Thu Sep 23 09:31:57 CEST 2021


Module: kamailio
Branch: master
Commit: 2ae296f5d8fa2baa2d451213d668fdd8fabe06d9
URL: https://github.com/kamailio/kamailio/commit/2ae296f5d8fa2baa2d451213d668fdd8fabe06d9

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2021-09-23T09:06:51+02:00

posops: handle negative index parameter for find functions

---

Modified: src/modules/posops/doc/posops_admin.xml
Modified: src/modules/posops/posops_mod.c

---

Diff:  https://github.com/kamailio/kamailio/commit/2ae296f5d8fa2baa2d451213d668fdd8fabe06d9.diff
Patch: https://github.com/kamailio/kamailio/commit/2ae296f5d8fa2baa2d451213d668fdd8fabe06d9.patch

---

diff --git a/src/modules/posops/doc/posops_admin.xml b/src/modules/posops/doc/posops_admin.xml
index 7aa9620343..b0b67b117e 100644
--- a/src/modules/posops/doc/posops_admin.xml
+++ b/src/modules/posops/doc/posops_admin.xml
@@ -246,7 +246,8 @@ $var(pos) = pos_body_end();
 			val is at index 0, it returns the value specified by modparam idx0.
 			</para>
 			<para>
-			The idx can be an integer value or a variable holding an integer.
+			The idx can be an integer value or a variable holding an integer. If
+			the value is negative, the position is counted from the end of the buffer.
 			</para>
 			<para>
 			The val can be a static string or variables.
@@ -274,7 +275,8 @@ $var(idx) = pos_find_str("100", "kamailio");
 			by modparam idx0.
 			</para>
 			<para>
-			The idx can be an integer value or a variable holding an integer.
+			The idx can be an integer value or a variable holding an integer. If
+			the value is negative, the position is counted from the end of the buffer.
 			</para>
 			<para>
 			The val can be a static string or variables.
diff --git a/src/modules/posops/posops_mod.c b/src/modules/posops/posops_mod.c
index 8c538720ce..6e367c53aa 100644
--- a/src/modules/posops/posops_mod.c
+++ b/src/modules/posops/posops_mod.c
@@ -427,13 +427,15 @@ static int ki_posops_pos_find_str(sip_msg_t *msg, int idx, str *val)
 	char *p;
 	str text;
 
-	if(idx<0 || val==NULL || val->s==NULL || val->len<=0) {
+	if(val==NULL || val->s==NULL || val->len<=0) {
 		return -1;
 	}
-	if(idx > msg->len - val->len) {
+	if(idx<0) {
+		idx += msg->len;
+	}
+	if(idx<0 || idx > msg->len - val->len) {
 		return -1;
 	}
-
 	text.s = msg->buf + idx;
 	text.len = msg->len - idx;
 	p = str_search(&text, val);
@@ -476,10 +478,13 @@ static int ki_posops_pos_findi_str(sip_msg_t *msg, int idx, str *val)
 	char *p;
 	str text;
 
-	if(idx<0 || val==NULL || val->s==NULL || val->len<=0) {
+	if(val==NULL || val->s==NULL || val->len<=0) {
 		return -1;
 	}
-	if(idx > msg->len - val->len) {
+	if(idx<0) {
+		idx += msg->len;
+	}
+	if(idx<0 || idx > msg->len - val->len) {
 		return -1;
 	}
 




More information about the sr-dev mailing list