>From 366e3eb5e75adaf2838c2fff8f5bfd39ad20ad3e Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Sun, 22 Sep 2013 11:37:09 +0200 Subject: [PATCH] Read no more than the buffer length when a length of #f is passed in Signed-off-by: Mario Domenech Goulart --- NEWS | 1 + extras.scm | 8 ++++---- tests/port-tests.scm | 44 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 5b3cfdb..17560ce 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,7 @@ to Florian Zumbiehl) - CVE-2013-2075: Use POSIX poll() in other places where select() was still being used. (thanks to Florian Zumbiehl and Joerg Wittenberger) + - read-string! no longer reads beyond its buffer when length is #f. - Tools - csc: added "-oi"/"-ot" options as alternatives to "-emit-inline-file" diff --git a/extras.scm b/extras.scm index 49ab5cf..8f17e1f 100644 --- a/extras.scm +++ b/extras.scm @@ -176,10 +176,10 @@ (define (read-string! n dest #!optional (port ##sys#standard-input) (start 0)) (##sys#check-input-port port #t 'read-string!) (##sys#check-string dest 'read-string!) - (when n - (##sys#check-exact n 'read-string!) - (when (fx> (fx+ start n) (##sys#size dest)) - (set! n (fx- (##sys#size dest) start)))) + (when n (##sys#check-exact n 'read-string!)) + (let ((dest-size (##sys#size dest))) + (unless (and n (fx<= (fx+ start n) dest-size)) + (set! n (fx- dest-size start)))) (##sys#check-exact start 'read-string!) (##sys#read-string! n dest port start) ) diff --git a/tests/port-tests.scm b/tests/port-tests.scm index 409c552..bd7c858 100644 --- a/tests/port-tests.scm +++ b/tests/port-tests.scm @@ -295,7 +295,49 @@ EOF (test-group "read-line string port position tests" -(test-port-position read-string-line/pos)) + (test-port-position read-string-line/pos)) + +(test-group "read-string!" + (let ((in (open-input-string "1234567890")) + (buf (make-string 5))) + (test-equal "read-string! won't read past buffer if given #f" + (read-string! #f buf in) + 5) + (test-equal "read-string! reads the requested bytes with #f" + buf + "12345") + (test-equal "read-string! won't read past buffer if given #f and offset" + (read-string! #f buf in 3) + 2) + (test-equal "read-string! reads the requested bytes with #f and offset" + buf + "12367") + (test-equal "read-string! reads until the end correctly" + (read-string! #f buf in) + 3) + (test-equal "read-string! leaves the buffer's tail intact" + buf + "89067")) + (let ((in (open-input-string "1234567890")) + (buf (make-string 5))) + (test-equal "read-string! won't read past buffer if given size" + (read-string! 10 buf in) + 5) + (test-equal "read-string! reads the requested bytes with buffer size" + buf + "12345") + (test-equal "read-string! won't read past buffer if given size and offset" + (read-string! 10 buf in 3) + 2) + (test-equal "read-string! reads the requested bytes with buffer size and offset" + buf + "12367") + (test-equal "read-string! reads until the end correctly with buffer size" + (read-string! 10 buf in) + 3) + (test-equal "read-string! leaves the buffer's tail intact" + buf + "89067"))) ;; Disabled because it requires `echo -n` for ;; the EOF test, and that is not available on all systems. -- 1.7.10.4