From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id +IWwMJPClGKeqwAAbAwnHQ (envelope-from ) for ; Mon, 30 May 2022 15:11:47 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id GHyVMJPClGKlXgEAauVa8A (envelope-from ) for ; Mon, 30 May 2022 15:11:47 +0200 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 4D7A8E96D for ; Mon, 30 May 2022 15:11:47 +0200 (CEST) Received: from localhost ([::1]:33342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nvfBR-0003W7-4b for larch@yhetil.org; Mon, 30 May 2022 09:11:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42688) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nvf7q-0000kX-Tj for guix-patches@gnu.org; Mon, 30 May 2022 09:08:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:49314) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nvf7p-0007S6-Q0; Mon, 30 May 2022 09:08:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nvf7p-0006lV-L4; Mon, 30 May 2022 09:08:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#55673] [PATCH v3] cache: Catch invalid 'last-expiry-cleanup'. References: <20220527082519.501697-1-zimon.toutoune@gmail.com> In-Reply-To: <20220527082519.501697-1-zimon.toutoune@gmail.com> Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: ludo@gnu.org, guix-patches@gnu.org Resent-Date: Mon, 30 May 2022 13:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 55673 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 55673@debbugs.gnu.org Cc: zimoun , ludo@gnu.org X-Debbugs-Original-Xcc: ludo@gnu.org Received: via spool by 55673-submit@debbugs.gnu.org id=B55673.165391605525943 (code B ref 55673); Mon, 30 May 2022 13:08:01 +0000 Received: (at 55673) by debbugs.gnu.org; 30 May 2022 13:07:35 +0000 Received: from localhost ([127.0.0.1]:43211 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nvf7O-0006kM-LU for submit@debbugs.gnu.org; Mon, 30 May 2022 09:07:34 -0400 Received: from mail-wm1-f52.google.com ([209.85.128.52]:40782) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nvf7M-0006k9-SS for 55673@debbugs.gnu.org; Mon, 30 May 2022 09:07:33 -0400 Received: by mail-wm1-f52.google.com with SMTP id v4-20020a1cac04000000b00397001398c0so8357412wme.5 for <55673@debbugs.gnu.org>; Mon, 30 May 2022 06:07:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3lccS2bliL+603pRG1316bCgFWmHFZ3xtbxlNgTUcVU=; b=JRfLo3cDS0qak3ImMzVThZBjXEVz/ZWRP0tAJeEuzcu60N6MOVWUCP1fHtC7TePSTI rq0g5oO5uZA/6QKTMRSOM33M9KYzwGG3D7LXrDMCA78QiziMvf+uPe/7XMu6XLs5WuKj iX9JHvwkAtUdu6f9SkgS35z8nyW4BNVaJSgaAqn2Y/kjPYrKFg5RwdhQX0/YpHf0VlBS +sOco0xQm6bd7rXt+NoBrJo9Uf9aibNPTt/lnvwgHujaF1JEXjwxhu4ANQpYD/AxZQX2 AAu7fCvsC/JUmMUK+PKweW/FxQEPQVgp4fudb/UeIzRIpBVUgYhOxA9vFKwPgq50uHjc dktQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3lccS2bliL+603pRG1316bCgFWmHFZ3xtbxlNgTUcVU=; b=UgeFuvyj5lQ1TGW9nmf20tpNNAemB8NLPNbYa5qr3zSL8WR5KpIPQfLs3fLkScT6Td OTSrEJh4RqBhcNPJQavCK6BssrULBRiis0or15I+z+hQxRrctgWqYyZFGf6331AoKzHl 5QbflvHew3AFxuCedy9Sp5L0VYbR75N58D0vJdDLpqQW0pxK+xcfJI6lOivt0sbqNDj3 1Z8aWHaKd0446QSOhJ3PkvJ0lknLc21PV61fjh4fqZnacSYwJvTuSTuHgnQOT2g4hn6W sYOxD6cgPFJj/qcGwooK+QytLGL7LTeyQCFedr4Vk1TneKymDl5hwih4Omw2UST18nn5 y5ug== X-Gm-Message-State: AOAM5335auHqbqd96+g0WrWy47w1lOR/ctvqqQAoSieTSHsR8YmK5qao I1qB0NwpEyBy3QDoRoal13OuzlRTVUA= X-Google-Smtp-Source: ABdhPJxI1rrXIkAci4F+2YgjvEbn2Y7xLnCk+b7D9EORHl3nJaLrohv7otAdVB402tYU0VK60dr4RQ== X-Received: by 2002:a05:600c:19cb:b0:397:51db:446f with SMTP id u11-20020a05600c19cb00b0039751db446fmr19086745wmq.182.1653916046922; Mon, 30 May 2022 06:07:26 -0700 (PDT) Received: from localhost.localdomain ([193.48.40.241]) by smtp.gmail.com with ESMTPSA id e15-20020a056000178f00b002102f2fac37sm4756531wrg.51.2022.05.30.06.07.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 May 2022 06:07:26 -0700 (PDT) From: zimoun Date: Mon, 30 May 2022 15:07:14 +0200 Message-Id: <20220530130714.2142519-1-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.36.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1653916307; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: 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:dkim-signature; bh=3lccS2bliL+603pRG1316bCgFWmHFZ3xtbxlNgTUcVU=; b=a3LUQKkxDCnIlegWWsjxusdmL1I3apRV4PNbxGncYs/wiTayxRnxYXb9BvQUYz8cNy9Cqa Ccske36lwbhvOqVJw4ZcDiABqgX85CDhdEy6oazg9ooly1BTmw2XFVlCPEURp9Way0ieob F/l5DaanIxxyGUoRmpx2jZ8a+91cWuw56HYip44WySCgt0XpM5xHKBFjUmZCZbwkGM1jDk MJJ9pLH6w7i0DuzXJJJ4VXaRQ4Rc6Shg89IU2RNbmraWIY0eIy+yqWySkNGoRfULWzXesJ szi7q8cwmLzn+gA0IoepS/4ssSRcXmBxojBG0rni3GZ1zPW14nfS1HdHqXgrnA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1653916307; a=rsa-sha256; cv=none; b=BCU2n4cY9iJWIvTs/DMvBzIqh5y9yFPpTYZqEDNL1/ImMk7hyhM5XOURTKRZ2NDcoCNk2e 57+dHPI47pC47xNTBjRbSlqspdBpUBnPTwzCnN4x7kJfLKuMQJYWUIofkO8cf0brp/BSZf v9A8RrS56MwSuUtFANPZw448pMq+y/4J6im8GLJj2/Z2tdPjqegXLC01zYmyBWZbWPM/+7 I16rU1ElJ7NBVhK7R1EKWaT97JukNVFJhTz7R3Ciz8EoIV2sP70tY8tISJcSQOfOAFe0jV /7jWeSQ2gMUgZJM62o4W0ihCydpVfT1g3734ZVFXaAESIOo3iTlElL9/9tcpcA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=JRfLo3cD; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 5.47 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=JRfLo3cD; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 4D7A8E96D X-Spam-Score: 5.47 X-Migadu-Scanner: scn0.migadu.com X-TUID: hZZlhQFHJtLH Fixes . * guix/cache.scm (maybe-remove-expired-cache-entries)[last-expiry-date]: Check if the value is a valid integer. * tests/cache.scm: Test it. --- guix/cache.scm | 20 +++++++++++++++----- tests/cache.scm | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/guix/cache.scm b/guix/cache.scm index 51009809bd..9727a9eb58 100644 --- a/guix/cache.scm +++ b/guix/cache.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020, 2021 Ludovic Courtès +;;; Copyright © 2022 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -17,9 +18,11 @@ ;;; along with GNU Guix. If not, see . (define-module (guix cache) + #:use-module ((guix utils) #:select (with-atomic-file-output)) #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) #:use-module (ice-9 match) + #:use-module ((ice-9 textual-ports) #:select (get-string-all)) #:export (obsolete? delete-file* file-expiration-time @@ -91,10 +94,17 @@ (define expiry-file (string-append cache "/last-expiry-cleanup")) (define last-expiry-date - (catch 'system-error - (lambda () - (call-with-input-file expiry-file read)) - (const 0))) + (let ((str (catch 'system-error + (lambda () + (call-with-input-file expiry-file get-string-all)) + (const "0")))) + ;; Handle empty or corrupted 'expiry-file' when 'write' below is + ;; interrupted before being complete (e.g., SIGINT with C-c) or when + ;; the filesystem crashes. + (if (or (string-index str #\e) ; Handle value out of range + (string-index str #\E)) ; (e.g., 1234567890 -> 12E4567890) + 0 + (or (string->number str) 0)))) (when (obsolete? last-expiry-date now cleanup-period) (remove-expired-cache-entries (cache-entries cache) @@ -103,7 +113,7 @@ (define last-expiry-date #:delete-entry delete-entry) (catch 'system-error (lambda () - (call-with-output-file expiry-file + (with-atomic-file-output expiry-file (cute write (time-second now) <>))) (lambda args ;; ENOENT means CACHE does not exist. diff --git a/tests/cache.scm b/tests/cache.scm index 80b44d69aa..bd6fd64a22 100644 --- a/tests/cache.scm +++ b/tests/cache.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2017, 2020 Ludovic Courtès +;;; Copyright © 2022 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -74,6 +75,27 @@ (define-syntax-rule (test-cache-cleanup cache exp ...) (lambda (port) (display 0 port))))) +(test-equal "maybe-remove-expired-cache-entries, empty cache" + '("a" "b" "c") + (test-cache-cleanup cache + (call-with-output-file (string-append cache "/last-expiry-cleanup") + (lambda (port) + (display "" port))))) + +(test-equal "maybe-remove-expired-cache-entries, corrupted cache" + '("a" "b" "c") + (test-cache-cleanup cache + (call-with-output-file (string-append cache "/last-expiry-cleanup") + (lambda (port) + (display "1\"34657890" port))))) + +(test-equal "maybe-remove-expired-cache-entries, corrupted cache of out range" + '("a" "b" "c") + (test-cache-cleanup cache + (call-with-output-file (string-append cache "/last-expiry-cleanup") + (lambda (port) + (display "12E4567890" port))))) + (test-end "cache") ;;; Local Variables: base-commit: 38bf6c7d0cb588e8d4546db7d2e0bae2ec25183d -- 2.36.0