fabrykowski created an issue (kamailio/kamailio#4815)

### Description

`mtree` fails during module initialization when `db_sqlite` is used with a
multi-column `cols` definition containing three selected columns.

Expected behavior: `mtree` loads the row and packs all value columns into the
tree value, as documented for multi-column `cols`.

Actual behavior: Kamailio exits during startup with:

```text
unsupported data type for column 2
cannot load info from database
```

The same SQLite table and data load successfully when only two columns are
selected. The failure is reproducible even when the third selected column is the
same text column as the second selected column, so it is not caused by a 
distinct
third column's schema or data.

Self-contained reproducer:

https://github.com/fabrykowski/kamailio_mtree_multicolumn_bug

### Troubleshooting

#### Reproduction

Clone the reproducer repository:

```sh
git clone https://github.com/fabrykowski/kamailio_mtree_multicolumn_bug.git
cd kamailio_mtree_multicolumn_bug
```

Run the two-column control. It should start; stop it with Ctrl-C:

```sh
docker run --rm \
  --platform linux/amd64 \
  --entrypoint kamailio \
  -v "$PWD/repro:/repro:ro" \
  ghcr.io/kamailio/kamailio:6.1.3-trixie \
  -E -D -P /tmp/kamailio.pid -f /repro/01-control-two-columns.cfg
```

Run the duplicate-third-column failure:

```sh
docker run --rm \
  --platform linux/amd64 \
  --entrypoint kamailio \
  -v "$PWD/repro:/repro:ro" \
  ghcr.io/kamailio/kamailio:6.1.3-trixie \
  -E -D -P /tmp/kamailio.pid -f /repro/02-fail-duplicate-third-column.cfg
```

Run the distinct-third-column failure:

```sh
docker run --rm \
  --platform linux/amd64 \
  --entrypoint kamailio \
  -v "$PWD/repro:/repro:ro" \
  ghcr.io/kamailio/kamailio:6.1.3-trixie \
  -E -D -P /tmp/kamailio.pid -f /repro/03-fail-distinct-third-column.cfg
```

The reproducer contains one SQLite database and three minimal configs:

```text
repro/mtree.db
repro/01-control-two-columns.cfg
repro/02-fail-duplicate-third-column.cfg
repro/03-fail-distinct-third-column.cfg
```

The SQLite table uses placeholder column names and values:

```sql
create table lookup_rows (
  lookup_key varchar(32) not null primary key,
  payload_one varchar(128) not null,
  payload_two varchar(128) not null
);

insert into lookup_rows (lookup_key, payload_one, payload_two)
values ('123', 'alpha', 'beta');
```

#### Debugging Data

No core dump is produced. This is a startup module initialization failure.

The duplicate-third-column config selects:

```cfg
modparam("mtree", "mtree", 
"name=lookup_tree;dbtable=lookup_rows;type=0;cols='lookup_key,payload_one,payload_one'")
```

The distinct-third-column config selects:

```cfg
modparam("mtree", "mtree", 
"name=lookup_tree;dbtable=lookup_rows;type=0;cols='lookup_key,payload_one,payload_two'")
```

Both fail in the same way.

#### Log Messages

Duplicate-third-column failure:

```text
WARNING: no fork mode
 0(1) ERROR: mtree [mtree_mod.c:482]: mt_pack_values(): unsupported data type 
for column 2
 0(1) ERROR: mtree [mtree_mod.c:622]: mt_load_db(): Error packing values
 0(1) ERROR: mtree [mtree_mod.c:253]: mod_init(): cannot load info from database
 0(1) ERROR: <core> [core/sr_module.c:1053]: init_mod(): Error while 
initializing module mtree (/usr/lib/x86_64-linux-gnu/kamailio/modules/mtree.so)
ERROR: error while initializing modules
```

Distinct-third-column failure:

```text
WARNING: no fork mode
 0(1) ERROR: mtree [mtree_mod.c:482]: mt_pack_values(): unsupported data type 
for column 2
 0(1) ERROR: mtree [mtree_mod.c:622]: mt_load_db(): Error packing values
 0(1) ERROR: mtree [mtree_mod.c:253]: mod_init(): cannot load info from database
 0(1) ERROR: <core> [core/sr_module.c:1053]: init_mod(): Error while 
initializing module mtree (/usr/lib/x86_64-linux-gnu/kamailio/modules/mtree.so)
ERROR: error while initializing modules
```

#### SIP Traffic

N/A. The failure happens at startup before SIP request processing.

### Possible Solutions

In `src/modules/mtree/mtree_mod.c`, the non-`DB_CAP_FETCH` branch appears to
query only two columns even when `cols` contains more than two configured
columns. It later calls `mt_pack_values()` with the full configured column
count, which makes column index 2 unavailable in the result set.

The non-fetch branch should use the configured selected-column count, like the
fetch-capable branch does, instead of a hardcoded result-column count of 2.

The fix may be as small as:

```diff
diff --git a/src/modules/mtree/mtree_mod.c b/src/modules/mtree/mtree_mod.c
--- a/src/modules/mtree/mtree_mod.c
+++ b/src/modules/mtree/mtree_mod.c
@@
-    if((ret = mt_dbf.query(db_con, key_cols, op, vals, db_cols, pt->multi,
-                2, 0, &db_res))
+    if((ret = mt_dbf.query(db_con, key_cols, op, vals, db_cols, pt->multi,
+                c, 0, &db_res))
                     != 0
             || RES_ROW_N(db_res) <= 0) {
```

I am hesitant to open a full pull request with only this change because I have
too little experience with this codebase to judge whether other non-fetch DB
drivers or mtree modes need additional checks.

### Additional Information

- [x] LLM/AI Assistants were involved in discovery, analysis or submission of 
the issue
- [x] It happened in a production deployment
- [x] It happened in a testing or development deployment
- [ ] Testing or fuzzing tools were used to generate SIP traffic when it 
happened

  * **Kamailio Version** - output of `kamailio -v`

```text
version: kamailio 6.1.3 (x86_64/Linux)
flags: USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, 
USE_MCAST, DNS_IP_HACK, SHM_MMAP, PKG_MALLOC, MEM_JOIN_FREE, Q_MALLOC, 
F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, DBG_QM_MALLOC, DBG_F_MALLOC, 
DBG_TLSF_MALLOC, USE_FUTEX, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, 
USE_DST_BLOCKLIST, HAVE_RESOLV_RES, TLS_PTHREAD_MUTEX_SHARED
MAX_RECV_BUFFER_SIZE 262144, MAX_SEND_BUFFER_SIZE 262144, MAX_URI_SIZE 1024, 
BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: unknown
compiled with gcc 14.2.0
```

* **Operating System**:

```text
PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
NAME="Debian GNU/Linux"
VERSION_ID="13"
VERSION="13 (trixie)"
VERSION_CODENAME=trixie
DEBIAN_VERSION_FULL=13.5
ID=debian
HOME_URL="https://www.debian.org/";
SUPPORT_URL="https://www.debian.org/support";
BUG_REPORT_URL="https://bugs.debian.org/";
Linux dcac524cf561 6.12.76-linuxkit #1 SMP Thu Jun 18 21:12:39 UTC 2026 x86_64 
GNU/Linux
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/issues/4815
You are receiving this because you are subscribed to this thread.

Message ID: <kamailio/kamailio/issues/[email protected]>
_______________________________________________
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