| author | Manuel Coutand | 2012-04-05 05:01:22 (EDT) |
|---|---|---|
| committer | Eugene Tarassov | 2012-04-05 13:58:21 (EDT) |
| commit | be86faea25c88368b8165f9517131f78de2e85a4 (patch) (side-by-side diff) | |
| tree | 9e5a6fb9dd9e9a6d8bcf94c574b2982cac45afcf | |
| parent | a0b681795c606a2c9c44574cee25c6be3a50e4c3 (diff) | |
| download | org.eclipse.tcf.agent-be86faea25c88368b8165f9517131f78de2e85a4.zip org.eclipse.tcf.agent-be86faea25c88368b8165f9517131f78de2e85a4.tar.gz org.eclipse.tcf.agent-be86faea25c88368b8165f9517131f78de2e85a4.tar.bz2 | |
Add syntax check for context queries
| -rw-r--r-- | agent/tcf/services/contextquery.c | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/agent/tcf/services/contextquery.c b/agent/tcf/services/contextquery.c index c94348c..21a7ebb 100644 --- a/agent/tcf/services/contextquery.c +++ b/agent/tcf/services/contextquery.c @@ -63,7 +63,6 @@ static void add_char(char ch) { str_buf[str_pos++] = ch; } -/* TODO: parse_context_query() should check for syntax errors and set errno */ int parse_context_query(const char * q) { str_pos = 0; str_buf = NULL; @@ -75,41 +74,61 @@ int parse_context_query(const char * q) { str_max = 64; str_buf = (char *)tmp_alloc(str_max); if ((abs_path = *q == '/') != 0) q++; + if (*q == 0) { + set_errno(ERR_OTHER, "Invalid context query syntax"); + return -1; + } while (*q) { Attribute * attr = (Attribute *)tmp_alloc_zero(sizeof(Attribute)); for (;;) { str_pos = 0; while (*q) { - if (*q == '/') { - q++; - break; - } - else if (*q == '=' || *q == ',') { - break; + if (*q == '*'){ + add_char(*q++); + if (*q == '*'){ + add_char(*q++); + } + if ((*q != 0) && (*q != '/')) { + set_errno(ERR_OTHER, "Invalid context query syntax"); + return -1; + } } - else if (*q == '"') { + else if (*q != '"') { while (*q) { - if (*q == '"') { - q++; + if ((*q != '_') && + (((*q < '0') || (*q > '9')) && + ((*q < 'a') || (*q > 'z')) && + ((*q < 'A') || (*q > 'Z')))) { + set_errno(ERR_OTHER, "Invalid context query syntax"); + return -1; + } + + add_char(*q++); + + if ((*q == '/') || (*q == '=') || (*q == ',')) { break; } - else if (*q == '\\') { + } + } + else { + q++; + while (*q != '"') { + if (*q == '\\') { q++; if (*q == '\\' || *q == '"') { add_char(*q++); } else { - add_char('\\'); + set_errno(ERR_OTHER, "Invalid context query syntax"); + return -1; } } - else { - add_char(*q++); - } + add_char(*q++); } + q++; } - else { - add_char(*q++); - } + if ((*q == '/') || (*q == '=') || (*q == ',')) + break; } add_char(0); if (*q == ',') { @@ -125,6 +144,7 @@ int parse_context_query(const char * q) { } else { attr->value = tmp_strdup(str_buf); + if (*q != 0) q++; break; } } @@ -211,23 +231,29 @@ int context_query(Context * ctx, const char * query) { static void command_query(char * token, Channel * c) { LINK * l; unsigned cnt = 0; + int err = 0; char * query = json_read_alloc_string(&c->inp); if (read_stream(&c->inp) != 0) exception(ERR_JSON_SYNTAX); if (read_stream(&c->inp) != MARKER_EOM) exception(ERR_JSON_SYNTAX); + parse_context_query(query); + + err=errno; + write_stringz(&c->out, "R"); write_stringz(&c->out, token); - write_errno(&c->out, 0); + write_errno(&c->out, err); write_stream(&c->out, '['); - parse_context_query(query); - for (l = context_root.next; l != &context_root; l = l->next) { - Context * ctx = ctxl2ctxp(l); - if (ctx->exited) continue; - if (run_context_query(ctx)) { - if (cnt > 0) write_stream(&c->out, ','); - json_write_string(&c->out, ctx->id); - cnt++; + if (!err) { + for (l = context_root.next; l != &context_root; l = l->next) { + Context * ctx = ctxl2ctxp(l); + if (ctx->exited) continue; + if (run_context_query(ctx)) { + if (cnt > 0) write_stream(&c->out, ','); + json_write_string(&c->out, ctx->id); + cnt++; + } } } |

