{"record":{"id":"393eb02e7ca3eff1","repo":"moodle/moodle","slug":"invalid-comparison-cmp","errorCode":null,"errorMessage":"Invalid comparison: $cmp","messagePattern":"Invalid comparison: \\$cmp","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"public/lib/jmespath/src/TreeInterpreter.php","lineNumber":232,"sourceCode":"                throw new \\RuntimeException(\"Unknown node type: {$node['type']}\");\n        }\n    }\n\n    /**\n     * @return bool\n     */\n    private static function relativeCmp($left, $right, $cmp)\n    {\n        if (!(is_int($left) || is_float($left)) || !(is_int($right) || is_float($right))) {\n            return false;\n        }\n\n        switch ($cmp) {\n            case '>': return $left > $right;\n            case '>=': return $left >= $right;\n            case '<': return $left < $right;\n            case '<=': return $left <= $right;\n            default: throw new \\RuntimeException(\"Invalid comparison: $cmp\");\n        }\n    }\n}\n","sourceCodeStart":214,"sourceCodeEnd":236,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/jmespath/src/TreeInterpreter.php#L214-L236","documentation":"TreeInterpreter::relativeCmp implements the JMESPath ordering operators >, >=, <, <=. If any other comparison operator string reaches it, it throws RuntimeException 'Invalid comparison: $cmp'. Since the parser only emits valid operators, this indicates a corrupted/foreign AST rather than user input error.","triggerScenarios":"Evaluating an AST comparison node whose operator value is not one of '>', '>=', '<', '<=' — e.g. '=' or '!=' (equality is handled elsewhere) injected via a hand-built or deserialized-from-another-version AST.","commonSituations":"Users porting filter expressions with '==' semantics expecting it to hit relativeCmp, custom AST mutation, or ASTs cached across library versions where operator encoding changed.","solutions":["Re-parse the expression with the current library so operators are encoded correctly","Remove or rewrite the malformed comparison node in any hand-built AST (use '==' in expression text, which the parser routes to the equality node)","Check that '=' or '!=' did not end up in the operator field; equality is a separate node type","Align library versions if the AST came from a cache produced by another jmespath.php release"],"exampleFix":"// before: hand-built comparison node\n['type' => 'comparator', 'value' => '==', 'children' => [...]] // dispatched to relativeCmp\n// after: let the parser build it\n$ast = (new Parser())->parse('a >= `1`'); // '==' stays in the equality node, not relativeCmp","handlingStrategy":"validation","validationCode":"if (isset($node['value']) && !in_array($node['value'], ['>', '>=', '<', '<='], true)) {\n    throw new \\InvalidArgumentException('Unsupported comparator in AST');\n}","typeGuard":"function isSupportedComparator($op): bool {\n    return in_array($op, ['>', '>=', '<', '<='], true);\n}","tryCatchPattern":"try {\n    $result = \\JmesPath\\search($expr, $data);\n} catch (\\RuntimeException $e) {\n    if (str_starts_with($e->getMessage(), 'Invalid comparison')) {\n        throw new \\InvalidArgumentException(\"Bad JMESPath expression: $expr\", 0, $e);\n    } else { throw $e; }\n}","preventionTips":["Use '==' for equality in expression text; only >, >=, <, <= are ordering comparators","Never mutate AST operator values manually","Re-parse expressions rather than reusing serialized trees from other versions"],"tags":["jmespath","php","comparison","interpreter"],"backgroundTag":"internal-invariant-violation","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}