phalcon/cphalcon · error · Phalcon\Db\Exceptions\InvalidWkb
Invalid WKB: truncated buffer
Error message
Invalid WKB: truncated buffer
What it means
While walking the WKB buffer, WkbParser::readByte() needs 1 byte at the current position (byte-order markers, type codes, element counts). If fewer bytes remain it throws InvalidWkb('truncated buffer') — the payload ended earlier than its declared structure requires.
Source
Thrown at phalcon/Db/Geometry/WkbParser.zep:204
}
protected function skipExtraOrdinates(bool little, bool hasZ, bool hasM) -> void
{
if hasZ {
this->readDouble(little);
}
if hasM {
this->readDouble(little);
}
}
protected function readByte() -> int
{
var arr;
if this->position + 1 > this->length {
throw new InvalidWkb("truncated buffer");
}
let arr = unpack("C", substr(this->buffer, this->position, 1)),
this->position = this->position + 1;
return (int) arr[1];
}
protected function readUint32(bool little) -> int
{
var fmt, arr;
if this->position + 4 > this->length {
throw new InvalidWkb("truncated buffer");
}
let fmt = little ? "V" : "N",
arr = unpack(fmt, substr(this->buffer, this->position, 4)),View on GitHub (pinned to b7419de9cd)
Solutions
- Re-read the value from the database and compare byte length against the original before parsing
- Fix or regenerate the stored geometry (re-export via ST_AsBinary) so the payload is complete
- Catch InvalidWkb per row and skip bad records during batch processing
Example fix
// before
$geom = $parser->parse($value); // may throw on truncated input
// after
try {
$geom = $parser->parse($value);
} catch (\Phalcon\Db\Exceptions\InvalidWkb $e) {
$logger->warning('truncated geometry rejected', ['value' => bin2hex($value)]);
$geom = null;
} Defensive patterns
Strategy: try-catch
Try / catch
try {
$geom = $parser->parse($raw);
} catch (\Phalcon\Db\Exceptions\InvalidWkb $e) {
$geom = null; // payload shorter than its declared structure
} Prevention
- Compare fetched byte length against the value the database reports before parsing
- Never hand-edit or concatenate WKB strings to build fixtures
- Wrap parse() in try/catch at the batch level so one bad row does not stop an import
When it happens
Trigger: A payload that declares more elements than bytes are present (e.g. a multi-geometry count followed by too few sub-geometries); a BLOB cut off mid-geometry; offsets shifted by mixing hex and binary interpretations of the same value.
Common situations: Interrupted inserts leaving truncated geometry values; manual substr() on geometry data; hand-crafted fixture strings with wrong lengths.
Related errors
- Invalid WKB: empty value
- Invalid WKB: buffer too short for MySQL prefix
- Invalid WKB: unknown geometry type {baseType}
- The expression {type} is unknown
- DATABASE PARAMETER 'FOREIGN_KEY_CHECKS' HAS TO BE 1
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/9d58d1fd2e770b76.
Report an issue: GitHub.