Skip to content

Commit

Permalink
db_sqlite raw queries: Fix unnecessary lowercase restriction
Browse files Browse the repository at this point in the history
The code was searching for the " from " string chunk in the query but
in a case-sensitive manner (??), causing formatted queries (e.g. " FROM
") to fail.

Credits to Eric Tamme (@etamme) for spotting and reporting the issue

Fixes #3409

(cherry picked from commit 388d4fa)
  • Loading branch information
liviuchircu committed Jun 13, 2024
1 parent 098168d commit 54b671f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/db_sqlite/dbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static inline int db_copy_rest_of_count(const str* _qh, str* count_query)
const str searched_str = {" from ", sizeof(" from ")-1};

count_query->len = sizeof(COUNT_QUERY)-1;
if ((found=str_strstr(_qh, &searched_str)) != NULL) {
if ((found=str_strcasestr(_qh, &searched_str)) != NULL) {
const int len=_qh->len-(found-_qh->s);
/* check for overflow */
if (len > COUNT_BUF_SIZE-(sizeof(COUNT_QUERY)-1)) {
Expand Down
33 changes: 33 additions & 0 deletions test/test_ut.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,37 @@ void test_ut(void)
ok(_is_e164(_str("+123456789012345"), 0, 15) == 1, "test-e164-17");
ok(_is_e164(_str("+1234567890123456"), 0, 15) == -1, "test-e164-18");
ok(_is_e164(_str("123456789x12345"), 0, 15) == -1, "test-e164-19");

/* str_strcasestr() tests */
{
struct {
str a;
str b;
int ok_offset;
} tests[] = {
{str_init(""), str_init(""), -1},
{str_init(""), str_init("x"), -1},
{str_init("x"), str_init(""), -1},
{str_init("x"), str_init("x"), 0},
{str_init("xy"), str_init("x"), 0},
{str_init("yx"), str_init("x"), 1},
{str_init("yxy"), str_init("x"), 1},
{str_init("foo"), str_init("bar"), -1},
{str_init("foobar"), str_init("BAR"), 3},
{str_init("fooBaRx"), str_init("bAr"), 3},
{str_init("fbaRx"), str_init("bar"), 1},
{str_init("barx"), str_init("Bar"), 0},
{str_init("Bar"), str_init("baR"), 0},
{str_init("foo"), str_init("foobar"), -1},
};
int i;

for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
char *p = str_strcasestr(&tests[i].a, &tests[i].b);
if (tests[i].ok_offset < 0)
ok(!p, "test-str_strcasestr-%d", i);
else
ok(p == (tests[i].a.s + tests[i].ok_offset), "test-str_strcasestr-%d", i);
}
}
}
42 changes: 42 additions & 0 deletions ut.h
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,48 @@ static inline char* str_strstr(const str *stra, const str *strb)
return NULL;
}

/*
* search @strb in @stra ignoring case of both, return pointer to 1st occurrence
*/
static inline char* str_strcasestr(const str *stra, const str *strb)
{
int i;
int len;

if (stra==NULL || strb==NULL || stra->s==NULL || strb->s==NULL
|| stra->len<=0 || strb->len<=0) {
#ifdef EXTRA_DEBUG
LM_DBG("bad parameters\n");
#endif
return NULL;
}

if (strb->len > stra->len)
return NULL;

len=0;
while (stra->len-len >= strb->len){
if (tolower(stra->s[len]) != tolower(strb->s[0])) {
len++;
continue;
}

for (i=1; i<strb->len; i++)
if (tolower(stra->s[len+i])!=tolower(strb->s[i])) {
len++;
break;
}

if (i != strb->len)
continue;

return stra->s+len;
}


return NULL;
}

/*
* case-insensitive compare n chars of two str's
*/
Expand Down

0 comments on commit 54b671f

Please sign in to comment.