Module: kamailio Branch: master Commit: 2c5c25b77c086ffe925820ed281f91d0549aee9e URL: https://github.com/kamailio/kamailio/commit/2c5c25b77c086ffe925820ed281f91d0...
Author: Victor Seva linuxmaniac@torreviejawireless.org Committer: Victor Seva linuxmaniac@torreviejawireless.org Date: 2017-12-18T16:55:22+01:00
cfgt: use snprintf to simplify _cfgt_get_filename()
CID 153754 (#1 of 1): Buffer not null terminated (BUFFER_SIZE) 6. buffer_size: Calling strncpy with a source string whose length (1 chars) is greater than or equal to the size argument (1) will fail to null-terminate dest->s + i.
---
Modified: src/modules/cfgt/cfgt_int.c
---
Diff: https://github.com/kamailio/kamailio/commit/2c5c25b77c086ffe925820ed281f91d0... Patch: https://github.com/kamailio/kamailio/commit/2c5c25b77c086ffe925820ed281f91d0...
---
diff --git a/src/modules/cfgt/cfgt_int.c b/src/modules/cfgt/cfgt_int.c index 2a0777ebcb..1c5b938e9c 100644 --- a/src/modules/cfgt/cfgt_int.c +++ b/src/modules/cfgt/cfgt_int.c @@ -270,15 +270,19 @@ void _cfgt_remove_node(cfgt_node_p node)
int _cfgt_get_filename(int msgid, str uuid, str *dest, int *dir) { - int i, lid; + int lid; char buff_id[INT2STR_MAX_LEN]; char *sid; + char *format = "%.*s%.*s/%.*s.json"; if(dest == NULL || uuid.len == 0) return -1;
dest->len = cfgt_basedir.len + uuid.len; - if(cfgt_basedir.s[cfgt_basedir.len - 1] != '/') + if(cfgt_basedir.s[cfgt_basedir.len - 1] != '/') { dest->len = dest->len + 1; + format = "%.*s/%.*s/%.*s.json"; + } + (*dir) = dest->len; sid = sint2strbuf(msgid, buff_id, INT2STR_MAX_LEN, &lid); dest->len += lid + 6; dest->s = (char *)pkg_malloc((dest->len * sizeof(char) + 1)); @@ -286,20 +290,8 @@ int _cfgt_get_filename(int msgid, str uuid, str *dest, int *dir) LM_ERR("no more memory.\n"); return -1; } - strncpy(dest->s, cfgt_basedir.s, cfgt_basedir.len); - i = cfgt_basedir.len; - if(cfgt_basedir.s[cfgt_basedir.len - 1] != '/') { - strncpy(dest->s + i, "/", 1); - i = i + 1; - } - strncpy(dest->s + i, uuid.s, uuid.len); - i = i + uuid.len; - (*dir) = i; - strncpy(dest->s + i, "\0", 1); - i = i + 1; - strncpy(dest->s + i, sid, lid); - i = i + lid; - strncpy(dest->s + i, ".json\0", 6); + snprintf(dest->s, dest->len + 1, format, cfgt_basedir.len, cfgt_basedir.s, + uuid.len, uuid.s, lid, sid); return 0; }
@@ -334,6 +326,7 @@ void cfgt_save_node(cfgt_node_p node) LM_ERR("can't build filename\n"); return; } + dest.s[dir] = '\0'; LM_DBG("dir [%s]\n", dest.s); if(mkdir(dest.s, S_IRWXO | S_IXGRP | S_IRWXU) < 0) { LM_ERR("failed to make directory: %s\n", strerror(errno));