[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 08/23: add #:limit to get-bytevector-delimited
From: |
Andy Wingo |
Subject: |
[Guile-commits] 08/23: add #:limit to get-bytevector-delimited |
Date: |
Thu, 24 Mar 2016 14:26:03 +0000 |
wingo pushed a commit to branch wip-ethreads
in repository guile.
commit a049cf2b66e9f1dc44776ccd58e1d1555b33fe4d
Author: Andy Wingo <address@hidden>
Date: Fri Mar 23 17:40:12 2012 +0100
add #:limit to get-bytevector-delimited
* module/ice-9/eports.scm (get-bytevector-delimited): Allow a limit to
be placed on the size of the bytevector.
---
module/ice-9/eports.scm | 30 ++++++++++++++++++++----------
1 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/module/ice-9/eports.scm b/module/ice-9/eports.scm
index dd03224..efa6ccf 100644
--- a/module/ice-9/eports.scm
+++ b/module/ice-9/eports.scm
@@ -315,7 +315,7 @@
;; delimiter, and the delimiter, or the EOF object if EOF was
;; encountered first.
;;
-(define* (get-bytevector-delimited eport predicate)
+(define* (get-bytevector-delimited eport predicate #:key limit)
(define (collect-result prev prev-len bv)
(if (null? prev-len)
bv
@@ -328,6 +328,15 @@
(let ((len (bytevector-length (car prev))))
(bytevector-copy! (car prev) 0 out (- prev-len len) len)
(lp (cdr prev) (- prev-len len)))))))))
+ (define (found-delimiter buf start len delimiter prev prev-len)
+ (when (and limit (> (+ len prev-len) limit))
+ (error "Input too long" limit (+ len prev-len)))
+ (let ((ret (make-bytevector len)))
+ (bytevector-copy! (buf-bv buf) start ret 0 len)
+ ;; Plus one for the delimiter, if present
+ (flush-buffer buf (if (eof-object? delimiter) len (1+ len)))
+ (values (collect-result prev prev-len ret)
+ delimiter)))
(let ((buf (eport-readbuf eport)))
(unless buf
(error "not a readable port" eport))
@@ -335,25 +344,26 @@
(size (bytevector-length bv)))
(let lp ((prev '()) (prev-len 0))
(when (= (buf-cur buf) (buf-end buf))
+ (when (and limit (> prev-len limit))
+ (error "Input too long" limit prev-len))
(fill-input eport))
(let ((cur (buf-cur buf))
(end (buf-end buf)))
(let search ((i cur))
(if (< i end)
(if (predicate (bytevector-u8-ref bv i))
- (let ((ret (make-bytevector (- i cur))))
- (bytevector-copy! bv cur ret 0 (- i cur))
- ;; Plus one for the delimiter
- (flush-buffer buf (1+ (- i cur)))
- (values ret (bytevector-u8-ref bv i)))
+ (found-delimiter buf cur (- i cur)
+ (bytevector-u8-ref bv i)
+ prev prev-len)
(search (1+ i)))
(let ((len (- end cur)))
(if (zero? len)
;; EOF
- (values (if (zero? prev-len)
- the-eof-object
- (collect-result prev prev-len #vu8()))
- the-eof-object)
+ (if (zero? prev-len)
+ (values the-eof-object
+ the-eof-object)
+ (found-delimiter buf cur len the-eof-object
+ prev prev-len))
(let ((ret (make-bytevector len)))
(bytevector-copy! bv cur ret 0 len)
(flush-buffer buf len)
- [Guile-commits] branch wip-ethreads created (now 4dc952f), Andy Wingo, 2016/03/24
- [Guile-commits] 04/23: add (ice-9 ethreads), Andy Wingo, 2016/03/24
- [Guile-commits] 09/23: eports: some more exports, Andy Wingo, 2016/03/24
- [Guile-commits] 12/23: (web server ethreads): Use a large backlog., Andy Wingo, 2016/03/24
- [Guile-commits] 13/23: add latin1 chars and strings to eports, Andy Wingo, 2016/03/24
- [Guile-commits] 18/23: (web server ethreads) TCP_NODELAY tweak, Andy Wingo, 2016/03/24
- [Guile-commits] 05/23: http: allow custom read-line / continuation-line? functions, Andy Wingo, 2016/03/24
- [Guile-commits] 06/23: setsockopt can take an fd, Andy Wingo, 2016/03/24
- [Guile-commits] 10/23: EOF fix for continuation-line?, Andy Wingo, 2016/03/24
- [Guile-commits] 19/23: nio: add non-blocking connect, Andy Wingo, 2016/03/24
- [Guile-commits] 08/23: add #:limit to get-bytevector-delimited,
Andy Wingo <=
- [Guile-commits] 11/23: socket: TCP_CORK, TCP_NODELAY, Andy Wingo, 2016/03/24
- [Guile-commits] 23/23: virtualize read/write/close operations in <eport>, Andy Wingo, 2016/03/24
- [Guile-commits] 15/23: (web server ethreads): more use of latin1 accessors, Andy Wingo, 2016/03/24
- [Guile-commits] 01/23: add (ice-9 nio), Andy Wingo, 2016/03/24
- [Guile-commits] 20/23: eports: nonblocking connect-eport, Andy Wingo, 2016/03/24
- [Guile-commits] 14/23: refactoring to (web server ethreads) read-http-line, Andy Wingo, 2016/03/24
- [Guile-commits] 02/23: add (ice-9 eports), Andy Wingo, 2016/03/24
- [Guile-commits] 17/23: getsockopt: allow raw file descriptors, Andy Wingo, 2016/03/24
- [Guile-commits] 16/23: eports: add put-utf8-char, put-utf8-string, Andy Wingo, 2016/03/24
- [Guile-commits] 03/23: add (ice-9 epoll), Andy Wingo, 2016/03/24