{"record":{"id":"ab3e34381c61b654","repo":"phalcon/cphalcon","slug":"invalid-wkb-buffer-too-short-for-mysql-prefix","errorCode":null,"errorMessage":"Invalid WKB: buffer too short for MySQL prefix","messagePattern":"Invalid WKB: buffer too short for MySQL prefix","errorType":"exception","errorClass":"Phalcon\\Db\\Exceptions\\InvalidWkb","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Geometry/WkbParser.zep","lineNumber":55,"sourceCode":"    public function parse(string raw) -> <GeometryInterface>\n    {\n        var srid = 0, body, arr;\n\n        if raw === \"\" {\n            throw new InvalidWkb(\"empty value\");\n        }\n\n        /**\n         * PostGIS returns EWKB as an even-length ASCII-hex string; MySQL\n         * returns binary (4-byte LE SRID prefix + standard WKB). Real binary\n         * WKB is not valid ASCII hex (it carries the 0x01 byte-order byte and\n         * arbitrary double bytes), so this distinguishes the two.\n         */\n        if strlen(raw) % 2 === 0 && ctype_xdigit(raw) {\n            let body = hex2bin(raw);\n        } else {\n            if strlen(raw) < 5 {\n                throw new InvalidWkb(\"buffer too short for MySQL prefix\");\n            }\n\n            let arr  = unpack(\"V\", substr(raw, 0, 4)),\n                srid = (int) arr[1],\n                body = substr(raw, 4);\n        }\n\n        let this->buffer   = body,\n            this->length   = strlen(body),\n            this->position = 0;\n\n        return this->readGeometry(srid);\n    }\n\n    protected function readGeometry(int outerSrid) -> <GeometryInterface>\n    {\n        var byteOrder, little, typeWord, geomCode, baseType, hasZ, hasM,\n            srid, count, i, items;","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Geometry/WkbParser.zep#L37-L73","documentation":"When the input is not a hex string, WkbParser assumes the MySQL internal geometry format — a 4-byte little-endian SRID prefix followed by standard WKB — so at least 5 bytes are required. Fewer than 5 bytes throw InvalidWkb('buffer too short for MySQL prefix') before parsing starts.","triggerScenarios":"Passing a truncated geometry BLOB (under 5 bytes); feeding a short placeholder string that fails the hex check; BLOB values cut off by a too-small column type or by post-processing (substr, charset conversion).","commonSituations":"TINYBLOB/BLOB overflow truncating stored geometries; fixtures with placeholder values; binary values run through string functions or JSON round-trips that shorten them.","solutions":["Pass the geometry value exactly as the database driver returned it — no substr() or encoding conversion","Fix storage: use a BLOB type large enough and re-insert valid geometries","Catch InvalidWkb and quarantine the offending row instead of aborting the whole batch"],"exampleFix":"// before\n$geom = $parser->parse($row['geom']); // truncated BLOB\n\n// after\n$raw  = $row['geom'];\n$geom = (is_string($raw) && strlen($raw) >= 5) ? $parser->parse($raw) : null;","handlingStrategy":"try-catch","validationCode":"if (!is_string($raw) || strlen($raw) < 5) {\n    // cannot contain even the 4-byte SRID prefix + 1 byte of WKB\n    return null;\n}\nreturn $parser->parse($raw);","typeGuard":"function isPlausibleBinaryWkb(mixed $value): bool\n{\n    return is_string($value) && strlen($value) >= 5;\n}","tryCatchPattern":"try {\n    $geom = $parser->parse($raw);\n} catch (\\Phalcon\\Db\\Exceptions\\InvalidWkb $e) {\n    $logger->warning('Rejected geometry payload', ['bytes' => strlen($raw)]);\n    $geom = null;\n}","preventionTips":["Pass geometry BLOBs through untouched — no substr(), casts, or charset conversion","Size BLOB columns for the largest geometry you store","Validate payload length before parsing when data provenance is uncertain"],"tags":["wkb","geometry","phalcon-db","spatial","parser","mysql"],"backgroundTag":"invalid-wkb-payload","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}