Skip to content

Commit

Permalink
db_text: fix computing absolute path to db using cfg dir
Browse files Browse the repository at this point in the history
- reported by GH #1224

(cherry picked from commit f74cddd)
(cherry picked from commit 630bf04)
  • Loading branch information
miconda committed Aug 31, 2017
1 parent 40b2b43 commit 75f13d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
15 changes: 11 additions & 4 deletions modules/db_text/dbt_base.c
Expand Up @@ -52,6 +52,7 @@ db1_con_t* dbt_init(const str* _sqlurl)
LM_ERR("invalid parameter value\n");
return NULL;
}
LM_DBG("initializing for db url: [%.*s]\n", _sqlurl->len, _sqlurl->s);
_s.s = _sqlurl->s;
_s.len = _sqlurl->len;
if(_s.len <= DBT_ID_LEN || strncmp(_s.s, DBT_ID, DBT_ID_LEN)!=0)
Expand All @@ -68,16 +69,22 @@ db1_con_t* dbt_init(const str* _sqlurl)
_s.len -= DBT_ID_LEN;
if(_s.s[0]!='/')
{
if(sizeof(CFG_DIR)+_s.len+2 > DBT_PATH_LEN)
if(sizeof(CFG_DIR)+_s.len+2 >= DBT_PATH_LEN)
{
LM_ERR("path to database is too long\n");
return NULL;
}
strcpy(dbt_path, CFG_DIR);
dbt_path[sizeof(CFG_DIR)] = '/';
strncpy(&dbt_path[sizeof(CFG_DIR)+1], _s.s, _s.len);
_s.len += sizeof(CFG_DIR);
if(dbt_path[sizeof(CFG_DIR)-2]!='/') {
dbt_path[sizeof(CFG_DIR)-1] = '/';
strncpy(&dbt_path[sizeof(CFG_DIR)], _s.s, _s.len);
_s.len += sizeof(CFG_DIR);
} else {
strncpy(&dbt_path[sizeof(CFG_DIR)-1], _s.s, _s.len);
_s.len += sizeof(CFG_DIR) - 1;
}
_s.s = dbt_path;
LM_DBG("updated db url: [%.*s]\n", _s.len, _s.s);
}

_res = pkg_malloc(sizeof(db1_con_t)+sizeof(dbt_con_t));
Expand Down
21 changes: 12 additions & 9 deletions modules/db_text/dbt_file.c
Expand Up @@ -91,17 +91,18 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
dbt_table_p dtp = NULL;
dbt_column_p colp, colp0 = NULL;
dbt_row_p rowp, rowp0 = NULL;

enum {DBT_FLINE_ST, DBT_NLINE_ST, DBT_DATA_ST} state;

LM_DBG("request for table [%.*s]\n", tbn->len, tbn->s);


if(!tbn || !tbn->s || tbn->len<=0 || tbn->len>=255)
return NULL;

LM_DBG("request for table [%.*s] (len: %d)\n", tbn->len, tbn->s, tbn->len);

path[0] = 0;
if(dbn && dbn->s && dbn->len>0)
{
LM_DBG("db is [%.*s]\n", dbn->len, dbn->s);
LM_DBG("db is [%.*s] (len: %d)\n", dbn->len, dbn->s, dbn->len);
if(dbn->len+tbn->len<511)
{
strncpy(path, dbn->s, dbn->len);
Expand All @@ -115,12 +116,14 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
strncpy(path, tbn->s, tbn->len);
path[tbn->len] = 0;
}

LM_DBG("loading file [%s]\n", path);

fin = fopen(path, "rt");
if(!fin)
return NULL;

if(!fin) {
LM_ERR("failed to open file [%s]\n", path);
return NULL;
}

buf = pkg_malloc(_db_text_read_buffer_size);
if(!buf) {
LM_ERR("error allocating read buffer, %i\n", _db_text_read_buffer_size);
Expand Down

0 comments on commit 75f13d0

Please sign in to comment.