commit c34b2a4f433233eef34c5e452f5b23a81e1cedb6
parent 3e3fb9b26f9805a6b5e8df8c1907ac57c6c32983
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Sat, 13 Jan 2024 08:20:00 +0200
Add binary port assertions and reorder cond for error detection.
Diffstat:
2 files changed, 60 insertions(+), 16 deletions(-)
diff --git a/rsv/arbitrary-null.scm b/rsv/arbitrary-null.scm
@@ -27,6 +27,10 @@
port))))
(define (write-rsv-row row port)
+ (unless (binary-port? port)
+ (error "Provided port should be a binary port."
+ port))
+
(let loop ((row row))
(cond
((null? row)
@@ -48,6 +52,9 @@ RSV := ( ROW* )
ROW := ( STRING-or-NULL-VALUE* )
STRING-or-NULL-VALUE := String | Null-Value
Null-Value := #f"
+ (unless (binary-port? port)
+ (error "Expected binary port." port))
+
(let loop ((scm scm))
(cond
((null? scm) '())
@@ -72,6 +79,9 @@ Null-Value := #f"
Return the string just read, and the number of bytes just
read."
+ (unless (binary-port? port)
+ (error "Expected binary port." port))
+
(let ((output-field-port (open-output-bytevector)))
(let loop ((column-count column-count))
(let ((byte (read-u8 port)))
@@ -80,6 +90,11 @@ read."
(error "Prematurely terminated RSV at: (row, column)"
row-count
column-count))
+ ((not (legal-rsv? byte))
+ (error "Illegal value returned by RSV string reader: (illegal value, row, column)"
+ byte
+ row-count
+ column-count))
((value-terminator-byte? byte)
(values (utf8->string (get-output-bytevector output-field-port))
row-count
@@ -87,31 +102,50 @@ read."
((legal-utf8? byte)
(write-u8 byte
output-field-port)
- (loop (+ column-count 1)))
- (else
- (error "Illegal value returned by RSV string reader: (illegal value, row, column)"
- byte
- row-count
- column-count)))))))
+ (loop (+ column-count 1))))))))
(define (read-null-value row-count column-count port)
;; `row->scm` had already `peek-u8`-ed and knows that the next
;; `read-u8` will be the Null-Value-Byte, a.k.a. #xFE, so we can
;; remove it without looking at it.
- (read-u8 port)
- (let ((byte (read-u8 port))) ;; remove value terminator byte.
+ (unless (binary-port? port)
+ (error "Expected binary port." port))
+
+ ;; Remove Null-Value-Byte. XXX: Already checked in the
+ ;; procedure calling read-null-value, so it should be okay not
+ ;; to check it, no? Can a time-of-check-time-of-use result when not checking?
+ (let ((byte (read-u8 port)))
(cond
+ ((eof-object? byte)
+ (error "Prematurely terminated RSV: (row, column)"
+ row-count
+ column-count))
+ ((not (legal-rsv? byte))
+ (error "Illegal value returned by RSV string reader: (illegal value, row, column)"
+ byte
+ row-count
+ column-count))
+ ((not (null-value-byte? byte))
+ (error "Expected a Null Byte (#xFE), instead got: (unexpected byte, row, column)"
+ byte
+ row-count
+ column-count))))
+
+ (let ((byte (read-u8 port))) ;; Remove value terminator byte.
+ (cond
+ ((eof-object? byte)
+ (error "Prematurely terminated RSV: (row, column)"
+ row-count
+ column-count))
+ ((not (legal-rsv? byte))
+ (error "Expected a Value-Terminator-Byte (#xFF) after a Null Byte (#xFE), instead got illegal value: (illegal value, row, column)"
+ byte
+ row-count
+ column-count))
((value-terminator-byte? byte)
;; Return successfully.
(values row-count
(+ column-count 2)))
- ((number? byte)
- ;; If we've read any other byte following a null byte, we
- ;; raise an error.
- (error "Expected a Value-Terminator-Byte (#xFF) after a Null Byte (#xFE), instead got: (unexpected byte, row, column)"
- byte
- row-count
- column-count))
(else
(error "Expected a Value-Terminator-Byte (#xFF) after a Null Byte (#xFE), instead got illegal value: (illegal value, row, column)"
byte
@@ -119,6 +153,9 @@ read."
column-count)))))
(define (read-rsv-row row-count column-count port)
+ (unless (binary-port? port)
+ (error "Provided port should be a binary port."
+ port))
(let loop ((row '())
(row-count row-count)
(column-count column-count))
@@ -128,7 +165,7 @@ read."
row-count
column-count))
((row-terminator-byte? byte) ;; End of row.
- (read-u8 port) ;; Remove row terminator.
+ (read-u8 port) ;; Remove row terminator.
(values (reverse row)
(+ row-count 1)
0))
@@ -164,6 +201,9 @@ read."
column-count))))))
(define (read-rsv port)
+ (unless (binary-port? port)
+ (error "Provided port should be a binary port."
+ port))
(let loop ((rows '())
(row-count 0)
(column-count 0))
diff --git a/rsv/rows-streams.scm b/rsv/rows-streams.scm
@@ -8,6 +8,10 @@
(begin
(define-stream (port->rsv-row-stream port)
+ (unless (binary-port? port)
+ (error "Provided port should be a binary port."
+ port))
+
(let loop ((row-count 0)
(column-count 0))
(let ((byte (peek-u8 port)))