From 52da1564e630ce766e33458123996470e8806c3c Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Wed, 14 Apr 2021 20:22:15 -0500 Subject: [PATCH] Explicitly throw on early EOF in decoder --- decode.rkt | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/decode.rkt b/decode.rkt index 9300a0b..1d89bc4 100644 --- a/decode.rkt +++ b/decode.rkt @@ -5,6 +5,12 @@ (provide cbor-read) +(define (expect-byte port) + (define res (read-byte port)) + (if (byte? res) + res + (error "Unexpected EOF"))) + ; read n bytes from port into buf. n is inferred from the length of buf (define (read-n-bytes buf port) (define n (bytes-length buf)) @@ -12,34 +18,34 @@ (if (= cur n) buf (begin - (bytes-set! buf cur (read-byte port)) + (bytes-set! buf cur (expect-byte port)) (loop (add1 cur)))))) (define (read-argument port major additional) (if (< additional 24) additional (case additional - [(24) (read-byte port)] + [(24) (expect-byte port)] [(25) (bitwise-ior - (arithmetic-shift (read-byte port) 8) - (read-byte port))] + (arithmetic-shift (expect-byte port) 8) + (expect-byte port))] [(26) (bitwise-ior - (arithmetic-shift (read-byte port) 24) - (arithmetic-shift (read-byte port) 16) - (arithmetic-shift (read-byte port) 8) - (read-byte port))] + (arithmetic-shift (expect-byte port) 24) + (arithmetic-shift (expect-byte port) 16) + (arithmetic-shift (expect-byte port) 8) + (expect-byte port))] [(27) (bitwise-ior - (arithmetic-shift (read-byte port) 56) - (arithmetic-shift (read-byte port) 48) - (arithmetic-shift (read-byte port) 40) - (arithmetic-shift (read-byte port) 32) - (arithmetic-shift (read-byte port) 24) - (arithmetic-shift (read-byte port) 16) - (arithmetic-shift (read-byte port) 8) - (read-byte port))] + (arithmetic-shift (expect-byte port) 56) + (arithmetic-shift (expect-byte port) 48) + (arithmetic-shift (expect-byte port) 40) + (arithmetic-shift (expect-byte port) 32) + (arithmetic-shift (expect-byte port) 24) + (arithmetic-shift (expect-byte port) 16) + (arithmetic-shift (expect-byte port) 8) + (expect-byte port))] [(28 29 30) (error "Argument with additional ~a is reserved" additional)] [(31) (case major [(0 1 6) (error "Major ~a with additional ~a is malformed" major additional)] @@ -158,14 +164,14 @@ (parse-simple-value config additional) (case additional [(24) - (let ([extra (read-byte port)]) + (let ([extra (expect-byte port)]) (if (< extra 32) (error "Simple value used extra byte to encode ~a when it should have been in the initial byte" extra) (parse-simple-value config extra)))] [(25) (decode-half (bitwise-ior - (arithmetic-shift (read-byte port) 8) - (read-byte port)))] + (arithmetic-shift (expect-byte port) 8) + (expect-byte port)))] [(26) (define buf (bytes 0 0 0 0)) (read-n-bytes buf port) @@ -216,7 +222,7 @@ ; Deserialize one cbor value from the given input port. Can and will be called reentrantly. (define (cbor-read config in) - (define header (read-byte in)) + (define header (expect-byte in)) (let ([major (bitwise-bit-field header 5 8)] [additional (bitwise-and header #b11111)]) ((vector-ref major-dispatch-table major) config in additional))) -- 2.45.2