From fe7e7db55980877ed4648d2998902d5b3710c2fa Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 14 Jun 2023 23:52:04 +0200 Subject: [PATCH] cpp/pmtiles.hpp: fix build error with big-endian, and add extra sanitfy check --- cpp/pmtiles.hpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cpp/pmtiles.hpp b/cpp/pmtiles.hpp index 735eaf2..f1685e1 100644 --- a/cpp/pmtiles.hpp +++ b/cpp/pmtiles.hpp @@ -25,7 +25,7 @@ const uint8_t COMPRESSION_ZSTD = 0x4; #ifdef PMTILES_MSB template -inline void swap_byte_order(T* ptr) { +inline void swap_byte_order_if_msb(T* ptr) { unsigned char* ptrBytes = reinterpret_cast(ptr); for (size_t i = 0; i < sizeof(T)/2; ++i) { std::swap(ptrBytes[i], ptrBytes[sizeof(T)-1-i]); @@ -492,7 +492,11 @@ inline std::vector deserialize_directory(const std::string &decompresse uint64_t last_id = 0; for (size_t i = 0; i < num_entries; i++) { - uint64_t tile_id = last_id + decode_varint(&t, end); + const uint64_t val = decode_varint(&t, end); + if (val > std::numeric_limits::max() - last_id) { + throw malformed_directory_exception(); + } + const uint64_t tile_id = last_id + val; result[i].tile_id = tile_id; last_id = tile_id; }