theonedev/onedev · error · RuntimeException

Malformed query

Error message

Malformed query

What it means

CodeCommentQuery.parse() runs the user's query string through an ANTLR lexer/parser for the code-comment query language. Lexer and parser error listeners are replaced with one that throws RuntimeException("Malformed query") on any lexical or syntactic error, so any token/structure that does not match the query grammar aborts parsing. This is how OneDev rejects unparseable code comment search queries.

Source

Thrown at server-core/src/main/java/io/onedev/server/search/entity/codecomment/CodeCommentQuery.java:98

		this(criteria, new ArrayList<>());
	}

	public CodeCommentQuery() {
		this(null);
	}

	public static CodeCommentQuery parse(Project project, @Nullable String queryString,
										 boolean withCurrentUserCriteria) {
		if (queryString != null) {
			CharStream is = CharStreams.fromString(queryString);
			CodeCommentQueryLexer lexer = new CodeCommentQueryLexer(is);
			lexer.removeErrorListeners();
			lexer.addErrorListener(new BaseErrorListener() {

				@Override
				public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
										int charPositionInLine, String msg, RecognitionException e) {
					throw new RuntimeException("Malformed query", e);
				}

			});
			CommonTokenStream tokens = new CommonTokenStream(lexer);
			CodeCommentQueryParser parser = new CodeCommentQueryParser(tokens);
			parser.removeErrorListeners();
			parser.setErrorHandler(new BailErrorStrategy());
			QueryContext queryContext = parser.query();
			CriteriaContext criteriaContext = queryContext.criteria();
			Criteria<CodeComment> commentCriteria;
			if (criteriaContext != null) {
				commentCriteria = new CodeCommentQueryBaseVisitor<Criteria<CodeComment>>() {
					@Override
					public Criteria<CodeComment> visitFuzzyCriteria(FuzzyCriteriaContext ctx) {
						return new FuzzyCriteria(getValue(ctx.getText()));
					}

					@Override

View on GitHub (pinned to d44925c47c)

Solutions

  1. Fix the query string so it conforms to the code comment query grammar (quote values, balance parentheses, use valid operators).
  2. Validate/sanitize user input before passing it to CodeCommentQuery.parse().
  3. Catch RuntimeException (and ExplicitException) around parse() and surface a friendly 'invalid query' message to the user.
  4. Check the OneDev docs for the supported query syntax of the installed version, as grammar rules may differ between releases.

Example fix

// before
var query = CodeCommentQuery.parse(project, "(" + userInput, true); // unbalanced paren
// after
var sanitized = userInput.trim();
if (sanitized.chars().filter(c -> c == '(').count() != sanitized.chars().filter(c -> c == ')').count())
    throw new ExplicitException("Unbalanced parentheses in query");
var query = CodeCommentQuery.parse(project, sanitized, true);
Defensive patterns

Strategy: try-catch

Validate before calling

// basic sanity check before parsing
static boolean balanced(String q) {
    int depth = 0;
    boolean inQuote = false;
    for (char c : q.toCharArray()) {
        if (c == '"') inQuote = !inQuote;
        else if (!inQuote && c == '(') depth++;
        else if (!inQuote && c == ')') depth--;
        if (depth < 0) return false;
    }
    return depth == 0 && !inQuote;
}

Try / catch

try {
    var query = CodeCommentQuery.parse(project, queryString, true);
} catch (ExplicitException e) {
    showUserError("Invalid query: " + e.getMessage());
} catch (RuntimeException e) {
    showUserError("Malformed query — check syntax (quotes, parentheses, operators)");
}

Prevention

When it happens

Trigger: Calling CodeCommentQuery.parse(project, queryString, withCurrentUserCriteria) with a query string that violates the CodeCommentQuery grammar: unbalanced parentheses, unquoted values containing special characters, invalid operators, stray tokens, or unterminated quoted strings.

Common situations: Users typing free-text searches with characters that are grammar-significant (e.g. '~', '(', quotes) without quoting; saved queries or URLs carrying an edited/broken query string; programmatic query builders emitting invalid syntax after a OneDev version changed the grammar.

Understand the failure class

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/4ba3c944a9a75a27. Report an issue: GitHub.