From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 8D6JEBnZ3GA3ZAEAgWs5BA (envelope-from ) for ; Wed, 30 Jun 2021 22:50:33 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id iMVBDBnZ3GBfKgAA1q6Kng (envelope-from ) for ; Wed, 30 Jun 2021 20:50:33 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id BA30D1D9A5 for ; Wed, 30 Jun 2021 22:50:32 +0200 (CEST) Received: from localhost ([::1]:54058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lyhAA-0001X0-UH for larch@yhetil.org; Wed, 30 Jun 2021 16:50:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45914) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lyh9p-0000tZ-Gx for guix-patches@gnu.org; Wed, 30 Jun 2021 16:50:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:47412) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lyh9p-0001jT-8z for guix-patches@gnu.org; Wed, 30 Jun 2021 16:50:05 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lyh9p-0005CS-8r for guix-patches@gnu.org; Wed, 30 Jun 2021 16:50:05 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49169] [PATCH v2 07/16] 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: Wed, 30 Jun 2021 20:50:05 +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.162508615419818 (code B ref 49169); Wed, 30 Jun 2021 20:50:05 +0000 Received: (at 49169) by debbugs.gnu.org; 30 Jun 2021 20:49:14 +0000 Received: from localhost ([127.0.0.1]:58934 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lyh8z-00059R-J4 for submit@debbugs.gnu.org; Wed, 30 Jun 2021 16:49:13 -0400 Received: from eggs.gnu.org ([209.51.188.92]:47886) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lyh8m-00055r-8p for 49169@debbugs.gnu.org; Wed, 30 Jun 2021 16:49:00 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:53132) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lyh8h-0000yI-1H; Wed, 30 Jun 2021 16:48:55 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=42586 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 1lyh8g-0001jQ-Pi; Wed, 30 Jun 2021 16:48:54 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Wed, 30 Jun 2021 22:48:23 +0200 Message-Id: <20210630204832.25753-8-ludo@gnu.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210630204832.25753-1-ludo@gnu.org> References: <20210622090221.15182-1-ludo@gnu.org> <20210630204832.25753-1-ludo@gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1625086232; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=8nfrDU0OFX+sj8DY/wORixonqS16jBLsj6BuGAI7PJ8=; b=JwlS8Jf5OM0ruuFoPk0Lw5l7UbltUab+bQxyNjXYwlQzpGiqybirpRfyO1QMkbwv5aC7fm T0OjG4IWvHXh6PWdt8qOBMLTxKGxR2SmI394rwM8McCTMtYYTDd6hNIHs3lVPT+BwJ4bYb O3f9XW+Qo7/bMXyw8KauQ/IPUUeoQmsNmXQ970w5mM+L4xgf06A2lPAHu54EHvTVTIciw1 6YTfQ2olXEeZVWJ9JvTgOQuHMisF1+fjA0MO9VpSAl8KWSwRyFzECqqVS91yMew/3PWxxB Ge+VS6cHaP4G9t7oa0MucdRjrnlJVRyOQRggoXGBaCIAvaCz5yHySyxpZBp7ew== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1625086232; a=rsa-sha256; cv=none; b=qL9XEQSZTZlGihHvg6pn/IJAIytMf6BpPOIBnFObUaHMm9ga8lIAU7Ag3EiXMB1R0s2/h/ De0CMOBM7kTtWMqBvbhlYA/lDYSMoPM3HpPBWhQiVcZ6zoec53W3et3RvkD3adR8zpYon2 W+X5SuyL7fqb265gguAuTUAQMH7EDlXv7c/ASCy+b4QEMz3laQDGF5ESTIgt6+H/t8vEl9 hpVgtAENelh+73shPa8MIerKougp04OFVQpgPy/3LL3icSTqrg/ChvaZHpCNTv13vVbCaX xj8Za2Veapaeei9/zGmXbjiE2RCXGEK0JqJ6p80NhMSqer4gzO7MJfBkSz8tjQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: 3.58 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: BA30D1D9A5 X-Spam-Score: 3.58 X-Migadu-Scanner: scn0.migadu.com X-TUID: knmEM5z7Z1DA * 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)))))))) ;;; -- 2.32.0