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

Author: Donat Zenichev <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2026-01-06T12:44:27+01:00

db_redis: add support of `max_key_length`

Introduce the modparam `max_key_length`
which, if used, controls the max possible
key length to be used when adding/prepending
strings into the redis key.

By default disabled (means 0).
If used will be always compared against the
given string key length.

This change provides full backwards compatibility.

---

Modified: src/modules/db_redis/db_redis_mod.c
Modified: src/modules/db_redis/doc/db_redis.xml
Modified: src/modules/db_redis/doc/db_redis_admin.xml
Modified: src/modules/db_redis/redis_table.c

---

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

---

diff --git a/src/modules/db_redis/db_redis_mod.c 
b/src/modules/db_redis/db_redis_mod.c
index 90464dd97ab..ec3fe962d3b 100644
--- a/src/modules/db_redis/db_redis_mod.c
+++ b/src/modules/db_redis/db_redis_mod.c
@@ -37,6 +37,7 @@ int db_redis_opt_tls = 0;
 char *db_redis_ca_path = 0;
 #endif
 char *db_redis_db_pass = 0;
+unsigned int db_redis_max_key_len = 0; /* 0 means disabled */
 
 MODULE_VERSION
 
@@ -78,6 +79,7 @@ static param_export_t params[] = {
                {"schema_path", PARAM_STR, &redis_schema_path},
                {"verbosity", PARAM_INT, &db_redis_verbosity},
                {"mapping_struct_type", PARAM_INT, &mapping_struct_type},
+               {"max_key_length", PARAM_INT, &db_redis_max_key_len},
                {"hash_value", PARAM_STRING, &db_redis_hash_value},
                {"hash_expires", PARAM_INT, &db_redis_hash_expires},
                {"with_sentinels", PARAM_INT, &db_redis_with_sentinels},
diff --git a/src/modules/db_redis/doc/db_redis.xml 
b/src/modules/db_redis/doc/db_redis.xml
index 6837a4b5a68..070e68153a8 100644
--- a/src/modules/db_redis/doc/db_redis.xml
+++ b/src/modules/db_redis/doc/db_redis.xml
@@ -32,6 +32,11 @@
                <surname>Centelles Martin</surname>
                <email>[email protected]</email>
            </editor>
+           <editor>
+               <firstname>Donat</firstname>
+               <surname>Zenichev</surname>
+               <email>[email protected]</email>
+           </editor>
        </authorgroup>
        <copyright>
            <year>2018</year>
diff --git a/src/modules/db_redis/doc/db_redis_admin.xml 
b/src/modules/db_redis/doc/db_redis_admin.xml
index 75b78edbff5..7a9a60b6c09 100644
--- a/src/modules/db_redis/doc/db_redis_admin.xml
+++ b/src/modules/db_redis/doc/db_redis_admin.xml
@@ -266,6 +266,25 @@ modparam("db_redis", "opt_tls", 1)
                        </example>
                </section>
 
+               <section id="db_redis.p.max_key_length">
+                       <title><varname>max_key_length</varname> (int)</title>
+                       <para>
+                               Controls the max possible key length to be used 
when adding/prepending strings into the redis key.
+                               If used will be always compared against the 
given string key length.
+                       </para>
+                       <para>
+                               Default value: 0. Means disabled.
+                       </para>
+                       <example>
+                               <title>Limiting the max key length</title>
+                               <programlisting format="linespecific">
+...
+modparam("db_redis", "max_key_length", 4096)
+...
+                               </programlisting>
+                       </example>
+               </section>
+
                <section id="db_redis.p.mapping_struct_type">
                        <title><varname>mapping_struct_type</varname> 
(int)</title>
                        <para>
diff --git a/src/modules/db_redis/redis_table.c 
b/src/modules/db_redis/redis_table.c
index 068484e0557..2147ff16608 100644
--- a/src/modules/db_redis/redis_table.c
+++ b/src/modules/db_redis/redis_table.c
@@ -28,6 +28,8 @@
 #include "redis_connection.h"
 #include "redis_table.h"
 
+extern unsigned int db_redis_max_key_len;
+
 int db_redis_key_add_string(redis_key_t **list, const char *entry, size_t len)
 {
        redis_key_t *k;
@@ -37,6 +39,12 @@ int db_redis_key_add_string(redis_key_t **list, const char 
*entry, size_t len)
                return -1;
        }
 
+       if (db_redis_max_key_len > 0 && len > db_redis_max_key_len) {
+               LM_ERR("Too big length for key being added: allowed '%u' / 
given '%zu'\n",
+                               db_redis_max_key_len, len);
+               return -1;
+       }
+
        k = (redis_key_t *)pkg_malloc(sizeof(redis_key_t));
        if(!k) {
                LM_ERR("Failed to allocate memory for key list entry\n");
@@ -87,6 +95,12 @@ int db_redis_key_prepend_string(redis_key_t **list, const 
char *entry, size_t le
                return -1;
        }
 
+       if (db_redis_max_key_len > 0 && len > db_redis_max_key_len) {
+               LM_ERR("Too big length for key being prepended: allowed '%u' / 
given '%zu'\n",
+                               db_redis_max_key_len, len);
+               return -1;
+       }
+
        k = (redis_key_t *)pkg_malloc(sizeof(redis_key_t));
        if(!k) {
                LM_ERR("Failed to allocate memory for key list entry\n");

_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to