From f5d3c117582d25e1aa5b7ea37947653b2276585c Mon Sep 17 00:00:00 2001 From: Masaya Tojo Date: Sun, 4 Aug 2024 18:56:16 +0900 Subject: Check the length of the given bytevector --- pnm/pbm.scm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'pnm/pbm.scm') diff --git a/pnm/pbm.scm b/pnm/pbm.scm index 32506e0..7e7bc78 100644 --- a/pnm/pbm.scm +++ b/pnm/pbm.scm @@ -37,7 +37,8 @@ (data (make-bytevector byte-count 0))) (make-pbm-image width height data))) ((width height data) - (let* ((byte-width (ceiling (/ width 8)))) + (let* ((byte-width (ceiling (/ width 8))) + (byte-count (* byte-width height))) (define (xy->byte-idx+bit-idx x y) (let-values (((byte-x bit-x) (floor/ x 8))) (values (+ (* y byte-width) @@ -52,4 +53,6 @@ (let ((byte (bytevector-u8-ref data byte-idx))) (bytevector-u8-set! data byte-idx (copy-bit (- 7 bit-idx) byte b))))) + (unless (= byte-count (bytevector-length data)) + (error (string-append "(pnm pbm) make-pbm-image: Invalid bytevector length" byte-count))) (make-image 'pbm width height 1 data pixel-getter pixel-setter))))))) -- cgit v1.2.3