From patchwork Mon May 30 13:07:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 39684 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 ABC1827BBEA; Mon, 30 May 2022 14:12:05 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 3253127BBE9 for ; Mon, 30 May 2022 14:12:05 +0100 (BST) Received: from localhost ([::1]:33342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nvfBk-0003W7-8Y for patchwork@mira.cbaines.net; Mon, 30 May 2022 09:12:04 -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 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 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(-) base-commit: 38bf6c7d0cb588e8d4546db7d2e0bae2ec25183d 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: