Add overflow check in READVECTOR macro (#4946)

Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/4946

When deserializing vectors from index files, the READVECTOR macro did not
check whether `size * sizeof(element)` would overflow `SIZE_MAX`. A
malformed index file could specify a very large vector size that passes
the 1TB upper-bound check but overflows when multiplied by the element
size, leading to an undersized allocation and subsequent out-of-bounds
writes during the read.

Add an explicit overflow check before calling `resize()` to ensure the
total byte count fits in `size_t`.

Reviewed By: mnorris11

Differential Revision: D96951153

fbshipit-source-id: 70d9ed3a53b90a8088f469c270c0c942e0a58921
This commit is contained in:
Justin Gibbs
2026-03-17 22:40:03 -07:00
committed by meta-codesync[bot]
parent 02d609caff
commit 52d9fc7053
+6
View File
@@ -51,6 +51,12 @@ size_t get_deserialization_vector_byte_limit();
size >= 0 && \
size < (faiss::get_deserialization_vector_byte_limit() / \
sizeof(*(vec).data()))); \
FAISS_THROW_IF_NOT_FMT( \
size <= SIZE_MAX / sizeof((vec)[0]), \
"READVECTOR: size %zu would overflow for element " \
"size %zu", \
size, \
sizeof((vec)[0])); \
(vec).resize(size); \
READANDCHECK((vec).data(), size); \
}