cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.97k stars 984 forks source link

bytevector-reference-ref & set! fail to correctly check size of bytevectors smaller than ptr-bytes - 1 #821

Closed burgerrg closed 5 months ago

burgerrg commented 6 months ago
> (define rbv (make-reference-bytevector 1))
> (bytevector-reference-set! rbv 100 '(oh oh))
> (bytevector-reference-ref rbv 100)
(oh oh)
> rbv
#vu8(0)

The unsigned check ($fxu< (fx- (bytevector-length bv) (fx- (constant ptr-bytes) 1)) i) doesn't work as designed when the first argument goes negative.

mflatt commented 6 months ago

Thanks for tracking that down! In the borken checks, I was trying to use the $fxu< trick used in other functions to guard against both a too-large value and a negative value in one step — but that doesn't work for a larger range (word-sized, in this case) that needs to be in bounds on both ends.