Skip to content

Commit e3b894b

Browse files
committed
Added validation in as_numpy_vector
1 parent 3cc5041 commit e3b894b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

bson/binary.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,29 @@ def as_numpy_vector(self) -> BinaryVector:
625625

626626
dtype, padding = struct.unpack_from("<sB", self, 0)
627627
dtype = BinaryVectorDtype(dtype)
628+
n_bytes = len(self) - 2
628629

629630
if dtype == BinaryVectorDtype.INT8:
630631
data = np.frombuffer(self[2:], dtype="int8")
631632
elif dtype == BinaryVectorDtype.FLOAT32:
633+
if n_bytes % 4:
634+
raise ValueError(
635+
"Corrupt data. N bytes for a float32 vector must be a multiple of 4."
636+
)
632637
data = np.frombuffer(self[2:], dtype="float32")
633638
elif dtype == BinaryVectorDtype.PACKED_BIT:
639+
# data packed as uint8
640+
if padding and not n_bytes:
641+
raise ValueError("Corrupt data. Vector has a padding P, but no data.")
642+
if padding > 7 or padding < 0:
643+
raise ValueError(f"Corrupt data. Padding ({padding}) must be between 0 and 7.")
634644
data = np.frombuffer(self[2:], dtype="uint8")
645+
if padding and np.unpackbits(data[-1])[-padding:].sum() > 0:
646+
warnings.warn(
647+
"Vector has a padding P, but bits in the final byte lower than P are non-zero. For pymongo>=5.0, they must be zero.",
648+
DeprecationWarning,
649+
stacklevel=2,
650+
)
635651
else:
636652
raise ValueError(f"Unsupported dtype code: {dtype!r}")
637653
return BinaryVector(data, dtype, padding)

0 commit comments

Comments
 (0)