cs: avoid uninitialized bytevectors via ffi/unsafe

Zeroing out allocated memory is required by the specification of
`alloc` from `ffi/unsafe`.

Possibly relevant to #2493
This commit is contained in:
Matthew Flatt 2019-02-21 08:35:01 -07:00
parent 858a925c16
commit b7654d9a84

View File

@ -137,7 +137,7 @@
(foreign-procedure "(cs)fxmul" (foreign-procedure "(cs)fxmul"
(u8* uptr) (u8* uptr)
uptr)) uptr))
(let* ([s (make-bytevector 1)] (let* ([s (make-bytevector 1 0)]
[offset [offset
;; Disable interrupts to avoid a GC: ;; Disable interrupts to avoid a GC:
(with-interrupts-disabled (with-interrupts-disabled
@ -1336,7 +1336,7 @@
[(eq? mode 'raw) [(eq? mode 'raw)
(make-cpointer (foreign-alloc size) #f)] (make-cpointer (foreign-alloc size) #f)]
[(eq? mode 'atomic) [(eq? mode 'atomic)
(make-cpointer (make-bytevector size) #f)] (make-cpointer (make-bytevector size 0) #f)]
[(eq? mode 'nonatomic) [(eq? mode 'nonatomic)
(make-cpointer (#%make-vector (quotient size 8) 0) #f)] (make-cpointer (#%make-vector (quotient size 8) 0) #f)]
[(eq? mode 'atomic-interior) [(eq? mode 'atomic-interior)
@ -1344,7 +1344,7 @@
;; a finalizer is associated with the cpointer (as opposed to ;; a finalizer is associated with the cpointer (as opposed to
;; the address that is wrapped by the cpointer). Also, interior ;; the address that is wrapped by the cpointer). Also, interior
;; pointers are not allowed as GCable pointers. ;; pointers are not allowed as GCable pointers.
(let* ([bstr (make-bytevector size)] (let* ([bstr (make-bytevector size 0)]
[p (make-cpointer bstr #f)]) [p (make-cpointer bstr #f)])
(lock-object bstr) (lock-object bstr)
(with-global-lock (the-foreign-guardian p (lambda () (unlock-object bstr)))) (with-global-lock (the-foreign-guardian p (lambda () (unlock-object bstr))))