felixwluo commented on code in PR #46661: URL: https://github.com/apache/doris/pull/46661#discussion_r1923050213
########## be/src/geo/wkb_parse.cpp: ########## @@ -122,108 +122,169 @@ WkbParseContext* WkbParse::read(std::istream& is, WkbParseContext* ctx) { auto size = is.tellg(); is.seekg(0, std::ios::beg); - std::vector<unsigned char> buf(static_cast<size_t>(size)); - is.read(reinterpret_cast<char*>(buf.data()), static_cast<std::streamsize>(size)); - - ctx->dis = ByteOrderDataInStream(buf.data(), buf.size()); // will default to machine endian + // Check if size is valid + if (size <= 0) { + ctx->parse_status = GEO_PARSE_WKB_SYNTAX_ERROR; + return ctx; + } - ctx->shape = readGeometry(ctx).release(); + std::vector<unsigned char> buf(static_cast<size_t>(size)); + if (!is.read(reinterpret_cast<char*>(buf.data()), static_cast<std::streamsize>(size))) { + ctx->parse_status = GEO_PARSE_WKB_SYNTAX_ERROR; + return ctx; + } - if (!ctx->shape) { + // Ensure we have at least one byte for byte order + if (buf.empty()) { ctx->parse_status = GEO_PARSE_WKB_SYNTAX_ERROR; + return ctx; } - return ctx; -} -std::unique_ptr<GeoShape> WkbParse::readGeometry(WkbParseContext* ctx) { - // determine byte order - unsigned char byteOrder = ctx->dis.readByte(); + // First read the byte order using machine endian + auto byteOrder = buf[0]; - // default is machine endian + // Create ByteOrderDataInStream with the correct byte order if (byteOrder == byteOrder::wkbNDR) { + ctx->dis = ByteOrderDataInStream(buf.data(), buf.size()); ctx->dis.setOrder(ByteOrderValues::ENDIAN_LITTLE); } else if (byteOrder == byteOrder::wkbXDR) { + ctx->dis = ByteOrderDataInStream(buf.data(), buf.size()); ctx->dis.setOrder(ByteOrderValues::ENDIAN_BIG); + } else { + ctx->parse_status = GEO_PARSE_WKB_SYNTAX_ERROR; + return ctx; + } + + std::unique_ptr<GeoShape> shape = readGeometry(ctx); + if (!shape) { + ctx->parse_status = GEO_PARSE_WKB_SYNTAX_ERROR; + return ctx; } - uint32_t typeInt = ctx->dis.readUnsigned(); + ctx->shape = shape.release(); + return ctx; +} - uint32_t geometryType = (typeInt & 0xffff) % 1000; +std::unique_ptr<GeoShape> WkbParse::readGeometry(WkbParseContext* ctx) { + try { + // Ensure we have enough data to read + if (ctx->dis.size() < 5) { // At least 1 byte for order and 4 bytes for type + return nullptr; + } - std::unique_ptr<GeoShape> shape; + // Skip the byte order as we've already handled it + ctx->dis.readByte(); - switch (geometryType) { - case wkbType::wkbPoint: - shape.reset(readPoint(ctx).release()); - break; - case wkbType::wkbLine: - shape.reset(readLine(ctx).release()); - break; - case wkbType::wkbPolygon: - shape.reset(readPolygon(ctx).release()); - break; - default: + uint32_t typeInt = ctx->dis.readUnsigned(); + + // Check if geometry has SRID + bool has_srid = (typeInt & 0x20000000) != 0; Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org