From patchwork Tue Jun 22 09:08:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 30618 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id EA00727BC81; Tue, 22 Jun 2021 10:09:15 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 8601B27BC78 for ; Tue, 22 Jun 2021 10:09:15 +0100 (BST) Received: from localhost ([::1]:46190 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lvcPC-0008WL-I9 for patchwork@mira.cbaines.net; Tue, 22 Jun 2021 05:09:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58908) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lvcP4-0008UB-DZ for guix-patches@gnu.org; Tue, 22 Jun 2021 05:09:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:54507) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lvcP3-00033O-6S for guix-patches@gnu.org; Tue, 22 Jun 2021 05:09:06 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lvcP2-0007o8-Uk for guix-patches@gnu.org; Tue, 22 Jun 2021 05:09:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49169] [PATCH 07/11] utils: 'edit-expression' no longer leaks file ports. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 22 Jun 2021 09:09:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49169 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49169@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 49169-submit@debbugs.gnu.org id=B49169.162435294329995 (code B ref 49169); Tue, 22 Jun 2021 09:09:04 +0000 Received: (at 49169) by debbugs.gnu.org; 22 Jun 2021 09:09:03 +0000 Received: from localhost ([127.0.0.1]:37813 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lvcP1-0007nY-1U for submit@debbugs.gnu.org; Tue, 22 Jun 2021 05:09:03 -0400 Received: from eggs.gnu.org ([209.51.188.92]:32858) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lvcOq-0007lQ-08 for 49169@debbugs.gnu.org; Tue, 22 Jun 2021 05:08:52 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:52822) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lvcOk-0002q0-Kt; Tue, 22 Jun 2021 05:08:46 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=49370 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lvcOk-0000B8-DE; Tue, 22 Jun 2021 05:08:46 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 22 Jun 2021 11:08:26 +0200 Message-Id: <20210622090830.15561-7-ludo@gnu.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210622090830.15561-1-ludo@gnu.org> References: <20210622090830.15561-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/utils.scm (edit-expression): Use 'call-with-input-file' to make sure IN gets closed. --- guix/utils.scm | 64 ++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/guix/utils.scm b/guix/utils.scm index 19990ceb8a..a13b13c4fa 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -342,38 +342,40 @@ a list of command-line arguments passed to the compression program." be a procedure that takes the original expression in string and returns a new one. ENCODING will be used to interpret all port I/O, it default to UTF-8. This procedure returns #t on success." + (define file (assq-ref source-properties 'filename)) + (define line (assq-ref source-properties 'line)) + (define column (assq-ref source-properties 'column)) + (with-fluids ((%default-port-encoding encoding)) - (let* ((file (assq-ref source-properties 'filename)) - (line (assq-ref source-properties 'line)) - (column (assq-ref source-properties 'column)) - (in (open-input-file file)) - ;; The start byte position of the expression. - (start (begin (while (not (and (= line (port-line in)) - (= column (port-column in)))) - (when (eof-object? (read-char in)) - (error (format #f "~a: end of file~%" in)))) - (ftell in))) - ;; The end byte position of the expression. - (end (begin (read in) (ftell in)))) - (seek in 0 SEEK_SET) ; read from the beginning of the file. - (let* ((pre-bv (get-bytevector-n in start)) - ;; The expression in string form. - (str (iconv:bytevector->string - (get-bytevector-n in (- end start)) - (port-encoding in))) - (post-bv (get-bytevector-all in)) - (str* (proc str))) - ;; Verify the edited expression is still a scheme expression. - (call-with-input-string str* read) - ;; Update the file with edited expression. - (with-atomic-file-output file - (lambda (out) - (put-bytevector out pre-bv) - (display str* out) - ;; post-bv maybe the end-of-file object. - (when (not (eof-object? post-bv)) - (put-bytevector out post-bv)) - #t)))))) + (call-with-input-file file + (lambda (in) + (let* ( ;; The start byte position of the expression. + (start (begin (while (not (and (= line (port-line in)) + (= column (port-column in)))) + (when (eof-object? (read-char in)) + (error (format #f "~a: end of file~%" in)))) + (ftell in))) + ;; The end byte position of the expression. + (end (begin (read in) (ftell in)))) + (seek in 0 SEEK_SET) ; read from the beginning of the file. + (let* ((pre-bv (get-bytevector-n in start)) + ;; The expression in string form. + (str (iconv:bytevector->string + (get-bytevector-n in (- end start)) + (port-encoding in))) + (post-bv (get-bytevector-all in)) + (str* (proc str))) + ;; Verify the edited expression is still a scheme expression. + (call-with-input-string str* read) + ;; Update the file with edited expression. + (with-atomic-file-output file + (lambda (out) + (put-bytevector out pre-bv) + (display str* out) + ;; post-bv maybe the end-of-file object. + (when (not (eof-object? post-bv)) + (put-bytevector out post-bv)) + #t)))))))) ;;;