Skip to content

Commit

Permalink
fix index check for bytevector-reference functions (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
mflatt authored Mar 26, 2024
1 parent 7749410 commit 3555ad0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
23 changes: 23 additions & 0 deletions mats/foreign.ms
Original file line number Diff line number Diff line change
Expand Up @@ -3680,12 +3680,35 @@
(error? (make-reference-bytevector -1))
(error? (bytevector-reference-ref #vu8(1 2 3) 0))
(error? (bytevector-reference-ref (make-reference-bytevector 8) -8))
(< 3 (foreign-sizeof 'ptr)) ;; assumption for some of the following tests
(error? (bytevector-reference-ref (make-reference-bytevector 3) 1))
(error? (bytevector-reference-ref (make-reference-bytevector 3) 0))
(error? (bytevector-reference-ref (make-reference-bytevector 3) -1))
(error? (guard (x [#t (error 'bytevector-reference-ref "failed as expected")])
;; guard the exception to hide the size of the vector, which
;; varies with the machine's word size
(bytevector-reference-ref (make-reference-bytevector (foreign-sizeof 'ptr)) 1)))
(error? (bytevector-reference-ref (make-reference-bytevector 8) 'oops))
(error? (bytevector-reference*-ref (make-reference-bytevector 8) -8))
(error? (bytevector-reference*-ref (make-reference-bytevector 8) 'oops))
(error? (bytevector-reference*-ref (make-reference-bytevector 3) 1))
(error? (bytevector-reference*-ref (make-reference-bytevector 3) 0))
(error? (bytevector-reference*-ref (make-reference-bytevector 3) -1))
(error? (guard (x [#t (error 'bytevector-reference-ref "failed as expected")])
(bytevector-reference-ref (make-reference*-bytevector (foreign-sizeof 'ptr)) 1)))
(error? (bytevector-reference-set! (make-reference-bytevector 8) -8 #false))
(error? (bytevector-reference-set! (make-reference-bytevector 8) 'oops #false))
(error? (bytevector-reference-set! (make-reference-bytevector 3) 1 #false))
(error? (bytevector-reference-set! (make-reference-bytevector 3) 0 #false))
(error? (guard (x [#t (error 'bytevector-reference-set! "failed as expected")])
(bytevector-reference-set! (make-reference-bytevector (foreign-sizeof 'ptr)) 1 #false)))
(error? (reference-address->object #f))
(error? (reference*-address->object #f))

(equal? #false (bytevector-reference-ref (make-reference-bytevector (foreign-sizeof 'ptr)) 0))
(equal? #false (bytevector-reference*-ref (make-reference-bytevector (foreign-sizeof 'ptr)) 0))
(equal? (void) (bytevector-reference-set! (make-reference-bytevector (foreign-sizeof 'ptr)) 0 #false))

(not (reference-bytevector? #vu8(1 2 3)))
(not (reference-bytevector? 7))
(begin
Expand Down
13 changes: 13 additions & 0 deletions mats/root-experr-compile-0-f-f-f
Original file line number Diff line number Diff line change
Expand Up @@ -10458,9 +10458,22 @@ foreign.mo:Expected error in mat collect-safe: "foreign-callable: string result
foreign.mo:Expected error in mat reference-bytevector: "make-reference-bytevector: -1 is not a valid bytevector length".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: #vu8(1 2 3) is not a reference bytevector".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: invalid index -8 for #vu8(0 0 0 0 0 0 ...)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: invalid index 1 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: invalid index 0 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: invalid index -1 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: failed as expected".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: invalid index oops for #vu8(0 0 0 0 0 0 ...)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference*-ref: invalid index -8 for #vu8(0 0 0 0 0 0 ...)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference*-ref: invalid index oops for #vu8(0 0 0 0 0 0 ...)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference*-ref: invalid index 1 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference*-ref: invalid index 0 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference*-ref: invalid index -1 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-ref: failed as expected".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-set!: invalid index -8 for #vu8(0 0 0 0 0 0 ...)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-set!: invalid index oops for #vu8(0 0 0 0 0 0 ...)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-set!: invalid index 1 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-set!: invalid index 0 for #vu8(0 0 0)".
foreign.mo:Expected error in mat reference-bytevector: "bytevector-reference-set!: failed as expected".
foreign.mo:Expected error in mat reference-bytevector: "reference-address->object: invalid address #f".
foreign.mo:Expected error in mat reference-bytevector: "reference*-address->object: invalid address #f".
ftype.mo:Expected error in mat ftype: "unexpected function ftype outside pointer field (function #f (int) int)".
Expand Down
6 changes: 3 additions & 3 deletions s/prims.ss
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
(lambda (bv i)
(unless (reference-bytevector? bv) ($oops who "~s is not a reference bytevector" bv))
(unless (and (fixnum? i)
(not ($fxu< (fx- (bytevector-length bv) (fx- (constant ptr-bytes) 1)) i)))
(fx<= 0 i (fx- (bytevector-length bv) (constant ptr-bytes))))
($oops who "invalid index ~s for ~s" i bv))
(bytevector-reference-ref bv i)))

Expand All @@ -97,15 +97,15 @@
(lambda (bv i)
(unless (reference-bytevector? bv) ($oops who "~s is not a reference bytevector" bv))
(unless (and (fixnum? i)
(not ($fxu< (fx- (bytevector-length bv) (fx- (constant ptr-bytes) 1)) i)))
(fx<= 0 i (fx- (bytevector-length bv) (constant ptr-bytes))))
($oops who "invalid index ~s for ~s" i bv))
(ref bv i))))

(define-who bytevector-reference-set!
(lambda (bv i val)
(unless (reference-bytevector? bv) ($oops who "~s is not a reference bytevector" bv))
(unless (and (fixnum? i)
(not ($fxu< (fx- (bytevector-length bv) (fx- (constant ptr-bytes) 1)) i)))
(fx<= 0 i (fx- (bytevector-length bv) (constant ptr-bytes))))
($oops who "invalid index ~s for ~s" i bv))
(bytevector-reference-set! bv i val)))

Expand Down

0 comments on commit 3555ad0

Please sign in to comment.