[sr-dev] git:master:aded00bb: dialog: safer version for getting dlg variable value

Daniel-Constantin Mierla miconda at gmail.com
Tue Aug 30 13:35:21 CEST 2022


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

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2022-08-30T13:32:16+02:00

dialog: safer version for getting dlg variable value

- clone to ensure that reference is not becoming invalid over the time

---

Modified: src/modules/dialog/dlg_var.c

---

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

---

diff --git a/src/modules/dialog/dlg_var.c b/src/modules/dialog/dlg_var.c
index cd151fd115..04e48fa636 100644
--- a/src/modules/dialog/dlg_var.c
+++ b/src/modules/dialog/dlg_var.c
@@ -279,22 +279,46 @@ void print_lists(struct dlg_cell *dlg) {
 	}
 }
 
+static str _dlg_var_strval = STR_NULL;
+
 str * get_dlg_variable(struct dlg_cell *dlg, str *key)
 {
-    str* var = NULL;
+	str* var = NULL;
 
-    if( !dlg || !key || key->len > strlen(key->s))
-    {
-        LM_ERR("BUG - bad parameters\n");
+	if( !dlg || !key || key->len > strlen(key->s))
+	{
+		LM_ERR("BUG - bad parameters\n");
 
-        return NULL;
-    }
+		return NULL;
+	}
 
-    dlg_lock(d_table, &(d_table->entries[dlg->h_entry]));
-    var = get_dlg_variable_unsafe( dlg, key);
-    dlg_unlock(d_table, &(d_table->entries[dlg->h_entry]));
+	dlg_lock(d_table, &(d_table->entries[dlg->h_entry]));
+	var = get_dlg_variable_unsafe( dlg, key);
+	if(var) {
+		_dlg_var_strval.len = pv_get_buffer_size();
+		if(_dlg_var_strval.len < var->len+1) {
+			LM_ERR("pv buffer too small (%d) - needed %d\n",
+					_dlg_var_strval.len, var->len);
+			_dlg_var_strval.s = NULL;
+			_dlg_var_strval.len = 0;
+			var = NULL;
+		} else {
+			_dlg_var_strval.s = pv_get_buffer();
+			strncpy(_dlg_var_strval.s, var->s, var->len);
+			_dlg_var_strval.len = var->len;
+			_dlg_var_strval.s[_dlg_var_strval.len] = '\0';
+		}
+	} else {
+		_dlg_var_strval.s = NULL;
+		_dlg_var_strval.len = 0;
+	}
+
+	dlg_unlock(d_table, &(d_table->entries[dlg->h_entry]));
 
-    return var;
+	if(var) {
+		return &_dlg_var_strval;
+	}
+	return NULL;
 }
 
 int get_dlg_variable_uintval(struct dlg_cell *dlg, str *key, unsigned int *uval)




More information about the sr-dev mailing list