{"record":{"id":"f13d22e4e6d10d1f","repo":"OtterMind/Chat2DB","slug":"ewkb-contains-a-negative-element-count","errorCode":null,"errorMessage":"EWKB contains a negative element count","messagePattern":"EWKB contains a negative element count","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"info","filePath":"chat2db-community-server/chat2db-community-plugins/chat2db-community-postgresql/src/main/java/ai/chat2db/plugin/postgresql/value/sub/PostgreSQLGeometryProcessor.java","lineNumber":294,"sourceCode":"    private void inspectChildren(WkbCursor cursor, ByteOrder byteOrder, WkbHeader parent,\n                                 Integer inheritedSrid, int expectedType, int depth) {\n        int childCount = readCount(cursor, byteOrder);\n        Integer effectiveSrid = parent.srid() != null ? parent.srid() : inheritedSrid;\n        for (int i = 0; i < childCount; i++) {\n            WkbHeader child = inspectGeometry(cursor, effectiveSrid, false, depth + 1);\n            if (expectedType != 0 && child.baseType() != expectedType) {\n                throw new IllegalArgumentException(\"EWKB collection contains an unexpected geometry type\");\n            }\n            if (child.hasZ() != parent.hasZ() || child.hasMeasure() != parent.hasMeasure()) {\n                throw new IllegalArgumentException(\"EWKB collection contains mixed coordinate dimensions\");\n            }\n        }\n    }\n\n    private int readCount(WkbCursor cursor, ByteOrder byteOrder) {\n        int count = cursor.readInt(byteOrder);\n        if (count < 0) {\n            throw new IllegalArgumentException(\"EWKB contains a negative element count\");\n        }\n        return count;\n    }\n\n    private ByteOrder readByteOrder(WkbCursor cursor) {\n        int byteOrder = cursor.readUnsignedByte();\n        if (byteOrder == 0) {\n            return ByteOrder.BIG_ENDIAN;\n        }\n        if (byteOrder == 1) {\n            return ByteOrder.LITTLE_ENDIAN;\n        }\n        throw new IllegalArgumentException(\"Unsupported EWKB byte order\");\n    }\n\n    private String removeTypeParenthesisSpace(String wkt) {\n        int openParenthesis = wkt.indexOf('(');\n        if (openParenthesis > 0 && wkt.charAt(openParenthesis - 1) == ' ') {","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-plugins/chat2db-community-postgresql/src/main/java/ai/chat2db/plugin/postgresql/value/sub/PostgreSQLGeometryProcessor.java#L276-L312","documentation":"Thrown by PostgreSQLGeometryProcessor.readCount when a 4-byte integer read from the EWKB byte stream is negative. This count represents the number of coordinates, rings, or child geometries depending on context. Negative counts are structurally impossible in valid EWKB. readCount is called from inspectLineString, inspectLinearRing (ring coordinate count), inspectGeometry (polygon ring count at line 203), and inspectChildren (child count at line 278). The exception is caught by convertJDBCValueByType at line 80 and the raw hex value is returned gracefully.","triggerScenarios":"Parsing EWKB where any count field (coordinate count, ring count, or child count) is a negative 4-byte signed integer. readCount at line 292 reads via cursor.readInt and checks count < 0.","commonSituations":"Truncated or corrupt binary data where byte patterns decode to negative integers; endianness mismatch causing bytes to be misinterpreted; bit-flipped data in storage.","solutions":["No action required in production — convertJDBCValueByType catches this and returns the raw hex value","Hex-dump the EWKB to verify count fields are valid unsigned values","Re-import the geometry from a trusted source"],"exampleFix":"// before: EWKB count field bytes decode to a negative int (corrupt)\n// after: ensure count fields contain valid non-negative integers in the correct byte order","handlingStrategy":"try-catch","validationCode":"// Already caught in convertJDBCValueByType. For EWKB integrity:\n// Verify count fields are non-negative before trusting the binary structure.\n// This is inherent to valid EWKB; corrupt data is the only trigger.","typeGuard":null,"tryCatchPattern":"try {\n    int count = readCount(cursor, byteOrder);\n} catch (IllegalArgumentException e) {\n    log.debug(\"EWKB negative count\", e);\n    return value;\n}","preventionTips":["Verify EWKB binary integrity before parsing — negative counts indicate corruption","Hex-dump the data to check count fields if this appears frequently","Re-import geometry data from a trusted source if corruption is suspected"],"tags":["postgresql","ewkb","geometry","data-integrity","validation"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}