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

  1. Re-read the value from the database and compare byte length against the original before parsing
  2. Fix or regenerate the stored geometry (re-export via ST_AsBinary) so the payload is complete
  3. 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

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


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/9d58d1fd2e770b76. Report an issue: GitHub.