Skip to content

Commit d6082f2

Browse files
authored
Merge pull request DeusData#2002 from metehanulusoy/fix/cypher-nonnumeric-limit-silently-dropped
fix(cypher): reject a non-numeric SKIP/LIMIT operand instead of dropping it
2 parents 5aeebba + c6ff57d commit d6082f2

2 files changed

Lines changed: 64 additions & 6 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,20 +1830,27 @@ static int parse_return_or_with(parser_t *p, cbm_return_clause_t **out, bool is_
18301830
}
18311831
}
18321832

1833-
/* Optional SKIP */
1833+
/* Optional SKIP. A non-numeric operand is a loud parse error: expect() has
1834+
* already filled p->error, and dropping the clause instead would leave
1835+
* r->skip at its "none" default while the query still reports success. */
18341836
if (match(p, TOK_SKIP)) {
18351837
const cbm_token_t *num = expect(p, TOK_NUMBER);
1836-
if (num) {
1837-
r->skip = (int)strtol(num->text, NULL, CBM_DECIMAL_BASE);
1838+
if (!num) {
1839+
free_return_clause(r);
1840+
return CBM_NOT_FOUND;
18381841
}
1842+
r->skip = (int)strtol(num->text, NULL, CBM_DECIMAL_BASE);
18391843
}
18401844

1841-
/* Optional LIMIT */
1845+
/* Optional LIMIT. Same rule: a dropped LIMIT is the failure mode #1334
1846+
* banned - the caller would silently receive the whole result set. */
18421847
if (match(p, TOK_LIMIT)) {
18431848
const cbm_token_t *num = expect(p, TOK_NUMBER);
1844-
if (num) {
1845-
r->limit = (int)strtol(num->text, NULL, CBM_DECIMAL_BASE);
1849+
if (!num) {
1850+
free_return_clause(r);
1851+
return CBM_NOT_FOUND;
18461852
}
1853+
r->limit = (int)strtol(num->text, NULL, CBM_DECIMAL_BASE);
18471854
}
18481855

18491856
*out = r;

‎tests/test_cypher.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,53 @@ TEST(cypher_parse_order_by_over_cap_rejected_issue1334) {
485485
PASS();
486486
}
487487

488+
/* #1994: a non-numeric SKIP/LIMIT operand must be a loud parse error too. The old
489+
* failure mode was the same one #1334 banned by a different route: expect()
490+
* returned NULL, the clause was dropped, and the query still reported success
491+
* with limit left at its -1 "no LIMIT" sentinel - so query_graph answered a
492+
* bounded query with the entire result set. Cypher parameters ($limit) are the
493+
* everyday trigger: they are what a Neo4j-shaped client writes by default. */
494+
TEST(cypher_parse_nonnumeric_limit_rejected_issue1994) {
495+
cbm_query_t *q = NULL;
496+
char *err = NULL;
497+
int rc = cbm_cypher_parse("MATCH (f:Function) RETURN f.name LIMIT $limit", &q, &err);
498+
ASSERT(rc != 0);
499+
free(err);
500+
PASS();
501+
}
502+
503+
TEST(cypher_parse_nonnumeric_skip_rejected_issue1994) {
504+
cbm_query_t *q = NULL;
505+
char *err = NULL;
506+
/* The orphaned operand also swallowed the LIMIT that followed it. */
507+
int rc = cbm_cypher_parse("MATCH (f:Function) RETURN f.name SKIP $offset LIMIT 10", &q, &err);
508+
ASSERT(rc != 0);
509+
free(err);
510+
PASS();
511+
}
512+
513+
TEST(cypher_parse_word_limit_operand_rejected_issue1994) {
514+
cbm_query_t *q = NULL;
515+
char *err = NULL;
516+
int rc = cbm_cypher_parse("MATCH (f:Function) RETURN f.name LIMIT abc", &q, &err);
517+
ASSERT(rc != 0);
518+
free(err);
519+
PASS();
520+
}
521+
522+
/* Control: a well-formed SKIP/LIMIT still parses and still carries its values. */
523+
TEST(cypher_parse_numeric_skip_limit_still_accepted) {
524+
cbm_query_t *q = NULL;
525+
char *err = NULL;
526+
int rc = cbm_cypher_parse("MATCH (f:Function) RETURN f.name SKIP 2 LIMIT 10", &q, &err);
527+
ASSERT_EQ(rc, 0);
528+
ASSERT_EQ(q->ret->skip, 2);
529+
ASSERT_EQ(q->ret->limit, 10);
530+
531+
cbm_query_free(q);
532+
PASS();
533+
}
534+
488535
TEST(cypher_parse_return_distinct) {
489536
cbm_query_t *q = NULL;
490537
char *err = NULL;
@@ -4556,6 +4603,10 @@ SUITE(cypher) {
45564603
RUN_TEST(cypher_parse_return_order_limit);
45574604
RUN_TEST(cypher_parse_multikey_order_by_issue1334);
45584605
RUN_TEST(cypher_parse_order_by_over_cap_rejected_issue1334);
4606+
RUN_TEST(cypher_parse_nonnumeric_limit_rejected_issue1994);
4607+
RUN_TEST(cypher_parse_nonnumeric_skip_rejected_issue1994);
4608+
RUN_TEST(cypher_parse_word_limit_operand_rejected_issue1994);
4609+
RUN_TEST(cypher_parse_numeric_skip_limit_still_accepted);
45594610
RUN_TEST(cypher_parse_return_distinct);
45604611
RUN_TEST(cypher_parse_inline_props);
45614612
RUN_TEST(cypher_parse_error);

0 commit comments

Comments
 (0)