Skip to content

Commit

Permalink
[2.8] Fix bad usage of strncmp - [MOD-6709] (#4484)
Browse files Browse the repository at this point in the history
* fix parser

(cherry picked from commit 4ea15f1)

* add test

(cherry picked from commit dcadbd9)

---------

Co-authored-by: GuyAv46 <guy.avimor@gmail.com>
  • Loading branch information
github-actions[bot] and GuyAv46 committed Mar 1, 2024
1 parent a065d3e commit 32fdaca
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/query_parser/v2/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2122,7 +2122,7 @@ yylhsminor.yy119 = yymsp[0].minor.yy119;
break;
case 75: /* vector_command ::= TERM param_size modifier ATTRIBUTE */
{
if (!strncasecmp("KNN", yymsp[-3].minor.yy0.s, yymsp[-3].minor.yy0.len)) {
if (yymsp[-3].minor.yy0.len == strlen("KNN") && !strncasecmp("KNN", yymsp[-3].minor.yy0.s, yymsp[-3].minor.yy0.len)) {
yymsp[0].minor.yy0.type = QT_PARAM_VEC;
yylhsminor.yy119 = NewVectorNode_WithParams(ctx, VECSIM_QT_KNN, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
yylhsminor.yy119->vn.vq->property = rm_strndup(yymsp[-1].minor.yy0.s, yymsp[-1].minor.yy0.len);
Expand Down Expand Up @@ -2173,7 +2173,7 @@ yylhsminor.yy119 = yymsp[0].minor.yy119;
break;
case 80: /* vector_range_command ::= TERM param_num ATTRIBUTE */
{
if (!strncasecmp("VECTOR_RANGE", yymsp[-2].minor.yy0.s, yymsp[-2].minor.yy0.len)) {
if (yymsp[-2].minor.yy0.len == strlen("VECTOR_RANGE") && !strncasecmp("VECTOR_RANGE", yymsp[-2].minor.yy0.s, yymsp[-2].minor.yy0.len)) {
yymsp[0].minor.yy0.type = QT_PARAM_VEC;
yylhsminor.yy119 = NewVectorNode_WithParams(ctx, VECSIM_QT_RANGE, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/query_parser/v2/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ query ::= star ARROW LSQB vector_query(B) RSQB ARROW LB attribute_list(C) RB. {
// Every vector query will have basic command part.
// It is this rule's job to create the new vector node for the query.
vector_command(A) ::= TERM(T) param_size(B) modifier(C) ATTRIBUTE(D). {
if (!strncasecmp("KNN", T.s, T.len)) {
if (T.len == strlen("KNN") && !strncasecmp("KNN", T.s, T.len)) {
D.type = QT_PARAM_VEC;
A = NewVectorNode_WithParams(ctx, VECSIM_QT_KNN, &B, &D);
A->vn.vq->property = rm_strndup(C.s, C.len);
Expand Down Expand Up @@ -1019,7 +1019,7 @@ expr(A) ::= modifier(B) COLON LSQB vector_range_command(C) RSQB. {
}

vector_range_command(A) ::= TERM(T) param_num(B) ATTRIBUTE(C). {
if (!strncasecmp("VECTOR_RANGE", T.s, T.len)) {
if (T.len == strlen("VECTOR_RANGE") && !strncasecmp("VECTOR_RANGE", T.s, T.len)) {
C.type = QT_PARAM_VEC;
A = NewVectorNode_WithParams(ctx, VECSIM_QT_RANGE, &B, &C);
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/pytests/test_vecsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def test_search_errors():

env.expect('FT.SEARCH', 'idx', '*=>[REDIS 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Syntax error')
env.expect('FT.SEARCH', 'idx', '*=>[KNN str @v $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Syntax error')
env.expect('FT.SEARCH', 'idx', '*=>[K 4 @v $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Syntax error')

env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]', 'PARAMS', '2', 'b', 'abcdefg').error().contains('Error parsing vector similarity query: query vector blob size (7) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '*=>[KNN 2 @v $b]', 'PARAMS', '2', 'b', 'abcdefghi').error().contains('Error parsing vector similarity query: query vector blob size (9) does not match index\'s expected size (8).')
Expand Down Expand Up @@ -554,6 +555,7 @@ def test_search_errors():
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefg').error().contains('Error parsing vector similarity query: query vector blob size (7) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefghi').error().contains('Error parsing vector similarity query: query vector blob size (9) does not match index\'s expected size (8).')
env.expect('FT.SEARCH', 'idx', '@bad:[vector_range 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefgh').equal([0]) # wrong field
env.expect('FT.SEARCH', 'idx', '@v:[vector 0.1 $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Syntax error')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range -1 $b]', 'PARAMS', '2', 'b', 'abcdefgh').error().equal('Error parsing vector similarity query: negative radius (-1) given in a range query')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$yield_distance_as:t}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `t` already exists in schema')
env.expect('FT.SEARCH', 'idx', '@v:[vector_range 0.1 $b]=>{$yield_distance_as:dist} @v:[vector_range 0.2 $b]=>{$yield_distance_as:dist}', 'PARAMS', '2', 'b', 'abcdefgh').error().contains('Property `dist` specified more than once')
Expand Down

0 comments on commit 32fdaca

Please sign in to comment.