From 72560efa5a4300e61f509cf67dd87f28ffc71686 Mon Sep 17 00:00:00 2001 From: Moritz Petersen Date: Tue, 14 Apr 2020 13:21:20 +0200 Subject: [PATCH 1/2] Always close stream in with-input-from-string & Cosmetic changes The Common Lisp Standard defines that with-input-from-string always closes the input stream that it creates. But with the current definition it would only be closed in the case of an index variable being provided. This change wraps the code for both cases (index given or not) in a unified unwind-protect form, updates the index if present, and always closes the stream. It also unifies the processing of declarations. Downcase code & fix indentation: Set the uppercased code in lower case to harmonise appearance and increase readability. Differentiated indentation for the first forms in unwind-protect and multiple-value-prog1 helps to understand their special meaning. --- src/lsp/iolib.lsp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/lsp/iolib.lsp b/src/lsp/iolib.lsp index b63741811..696f488b2 100644 --- a/src/lsp/iolib.lsp +++ b/src/lsp/iolib.lsp @@ -34,18 +34,17 @@ automatically closed on exit." Evaluates FORMs with VAR bound to a string input stream from the string that is the value of STRING-FORM. The stream is automatically closed on exit. Possible keywords are :INDEX, :START, and :END." - (if index - (multiple-value-bind (ds b) - (find-declarations body) - `(LET ((,var (MAKE-STRING-INPUT-STREAM ,string ,start ,end))) - ,@ds - (UNWIND-PROTECT - (MULTIPLE-VALUE-PROG1 - (PROGN ,@b) - (SETF ,index (FILE-POSITION ,var))) - (CLOSE ,var)))) - `(LET ((,var (MAKE-STRING-INPUT-STREAM ,string ,start ,end))) - ,@body))) + (multiple-value-bind (ds b) + (find-declarations body) + `(let ((,var (make-string-input-stream ,string ,start ,end))) + ,@ds + (unwind-protect + ,(if index + `(multiple-value-prog1 + (progn ,@b) + (setf ,index (file-position ,var))) + `(progn ,@b)) + (close ,var))))) (defmacro with-output-to-string ((var &optional string &rest r &key element-type) &rest body) "Syntax: (with-output-to-string (var [string-form]) {decl}* {form}*) -- GitLab From 17f183873dca9ce6be581dc2f5625a6427fa4986 Mon Sep 17 00:00:00 2001 From: Moritz Petersen Date: Sat, 18 Apr 2020 12:29:29 +0200 Subject: [PATCH 2/2] Add a regression test for the bug described in !197 --- src/tests/normal-tests/mixed.lsp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tests/normal-tests/mixed.lsp b/src/tests/normal-tests/mixed.lsp index 3b91ab68f..455201185 100644 --- a/src/tests/normal-tests/mixed.lsp +++ b/src/tests/normal-tests/mixed.lsp @@ -373,3 +373,17 @@ (test mix.0018.stack-overflow (signals ext:stack-overflow (labels ((f (x) (f (1+ x)))) (f 1)))) + +;;; Date 2020-04-18 +;;; URL: https://gitlab.com/embeddable-common-lisp/ecl/-/merge_requests/197 +;;; Description: +;;; +;;; Ensure that with-input-from-string closes the input stream +;;; that it creates. +(test mix.0019.close-with-input-from-string-stream + (let (stream-var) + (with-input-from-string (inner-stream-var "test") + (setf stream-var inner-stream-var) + (is (open-stream-p stream-var))) + (is (streamp stream-var)) + (is (not (open-stream-p stream-var))))) -- GitLab