Commit ed1018cf for libheif
commit ed1018cfdc89fde76618ae089f58873298bff0a5
Author: Dirk Farin <dirk.farin@gmail.com>
Date: Sun May 17 21:15:53 2026 +0200
fix false UB detection in libstdc++
diff --git a/libheif/file.cc b/libheif/file.cc
index 460a45a3..d4b4e350 100644
--- a/libheif/file.cc
+++ b/libheif/file.cc
@@ -759,51 +759,58 @@ Result<std::vector<uint8_t>> HeifFile::get_uncompressed_item_data(heif_item_id I
if (item_type == fourcc("mime")) {
std::string encoding = infe_box->get_content_encoding();
- if (encoding == "compress_zlib") {
+ // Skip the encoding comparisons when no encoding is set; falling through
+ // to the raw read below is the right behaviour for the uncompressed case.
+ // Skipping is also necessary to avoid libstdc++'s _S_compare(0, N) which
+ // computes unsigned `0 - N` and trips UBSan even though the cast result
+ // is benign.
+ if (!encoding.empty()) {
+ if (encoding == "compress_zlib") {
#if HAVE_ZLIB
- std::vector<uint8_t> compressed_data;
- error = m_iloc_box->read_data(ID, m_input_stream, m_idat_box, &compressed_data, m_limits);
- if (error) {
- return error;
- }
+ std::vector<uint8_t> compressed_data;
+ error = m_iloc_box->read_data(ID, m_input_stream, m_idat_box, &compressed_data, m_limits);
+ if (error) {
+ return error;
+ }
- return decompress_zlib(compressed_data);
+ return decompress_zlib(compressed_data);
#else
- return Error(heif_error_Unsupported_feature,
- heif_suberror_Unsupported_header_compression_method,
- encoding);
+ return Error(heif_error_Unsupported_feature,
+ heif_suberror_Unsupported_header_compression_method,
+ encoding);
#endif
- }
- else if (encoding == "deflate") {
-#if HAVE_ZLIB
- std::vector<uint8_t> compressed_data;
- error = m_iloc_box->read_data(ID, m_input_stream, m_idat_box, &compressed_data, m_limits);
- if (error) {
- return error;
}
- return decompress_deflate(compressed_data);
+ else if (encoding == "deflate") {
+#if HAVE_ZLIB
+ std::vector<uint8_t> compressed_data;
+ error = m_iloc_box->read_data(ID, m_input_stream, m_idat_box, &compressed_data, m_limits);
+ if (error) {
+ return error;
+ }
+ return decompress_deflate(compressed_data);
#else
- return Error(heif_error_Unsupported_feature,
- heif_suberror_Unsupported_header_compression_method,
- encoding);
+ return Error(heif_error_Unsupported_feature,
+ heif_suberror_Unsupported_header_compression_method,
+ encoding);
#endif
- }
- else if (encoding == "br") {
-#if HAVE_BROTLI
- std::vector<uint8_t> compressed_data;
- error = m_iloc_box->read_data(ID, m_input_stream, m_idat_box, &compressed_data, m_limits);
- if (error) {
- return error;
}
- return decompress_brotli(compressed_data);
+ else if (encoding == "br") {
+#if HAVE_BROTLI
+ std::vector<uint8_t> compressed_data;
+ error = m_iloc_box->read_data(ID, m_input_stream, m_idat_box, &compressed_data, m_limits);
+ if (error) {
+ return error;
+ }
+ return decompress_brotli(compressed_data);
#else
- return Error(heif_error_Unsupported_feature,
- heif_suberror_Unsupported_header_compression_method,
- encoding);
+ return Error(heif_error_Unsupported_feature,
+ heif_suberror_Unsupported_header_compression_method,
+ encoding);
#endif
- }
- else if (!encoding.empty()) {
- return Error(heif_error_Unsupported_feature, heif_suberror_Unsupported_codec);
+ }
+ else {
+ return Error(heif_error_Unsupported_feature, heif_suberror_Unsupported_codec);
+ }
}
}