From patchwork Fri May 27 15:46:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 39631 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 16F9F27BBEA; Fri, 27 May 2022 17:30:53 +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 968E927BBE9 for ; Fri, 27 May 2022 17:30:52 +0100 (BST) Received: from localhost ([::1]:36322 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nucrT-0004i5-OM for patchwork@mira.cbaines.net; Fri, 27 May 2022 12:30:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41006) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nucC2-0002es-9H for guix-patches@gnu.org; Fri, 27 May 2022 11:48:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:41747) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nucC1-0004dx-WB for guix-patches@gnu.org; Fri, 27 May 2022 11:48:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nucC1-0006kJ-Q8 for guix-patches@gnu.org; Fri, 27 May 2022 11:48:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#55673] [PATCH v2] cache: Catch valid integer for '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: guix-patches@gnu.org Resent-Date: Fri, 27 May 2022 15:48: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 Received: via spool by 55673-submit@debbugs.gnu.org id=B55673.165366643425730 (code B ref 55673); Fri, 27 May 2022 15:48:01 +0000 Received: (at 55673) by debbugs.gnu.org; 27 May 2022 15:47:14 +0000 Received: from localhost ([127.0.0.1]:35644 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nucBF-0006gv-KW for submit@debbugs.gnu.org; Fri, 27 May 2022 11:47:13 -0400 Received: from mail-wr1-f41.google.com ([209.85.221.41]:36800) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nucBC-0006gh-OG for 55673@debbugs.gnu.org; Fri, 27 May 2022 11:47:12 -0400 Received: by mail-wr1-f41.google.com with SMTP id u3so6381914wrg.3 for <55673@debbugs.gnu.org>; Fri, 27 May 2022 08:47:10 -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=/9MTsnm05Ku6jeO1qnRc6AdRgafy7KjBcCVWHGD8mS8=; b=S6Pgm/yRA500QS4V0mjL/ZTUkIwxZJKi22mFJDSTKxhWM4xMDB5Wsk0ufN8G9QtQFS CoFz0fISG7dJr8ENWtE9XTd6srGAEL4/cf/lEtQJ++o3xHmOZbQGVEFCgEBfCqvKNpul yCNs25HopMhZoOxSJxH2948AoZVP6Kps+JtPKyWXqzWO/0K8gQJBjbyHFGzCzy5B9dyS TK78to62UHUq3RDsV2aiDawIapmnqkHt/k0HxQimnIN9s/DF0e6nUWEKuzYZTrIxS/Kn zZR8OtJoWf+mxxUUpVKtOKJtbjjRZM8O0aX3v3B2ZYDrdfFSoeYXsVfLgZaznbhnAV6S BfDQ== 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=/9MTsnm05Ku6jeO1qnRc6AdRgafy7KjBcCVWHGD8mS8=; b=WNwKFlEFB1iQHckdtEgqAOCy1sJCn5WoWrB9rmjGPIooOA+cRat64IDWqpDWHI+tY8 W2K8rc1yho23N2w4Wiyi4clOFzkG3nTK4aTmxJcfKiKv/W3efobWJu2Ssq+Tlo9M1jB/ s5AKJj4wJCf6Dk3SRSNd4s9msVxlZTnD8sYmrUZtuP1jsusqij1ASFdVjG9NrybYmNpY Y3xitQPYo2VMnecUqufQUN4uiIsS9R2VTyEY+VKKQe7/8nL3Jj/W5+wnAd7kFnCcGCIx nLfrE7jEIfL+Nqtwewms2Y8iiNbkDVHmaTJ8xu0YiEXrkGVH/xIAf9XZbDgHqBBoacbK /pzQ== X-Gm-Message-State: AOAM533AyghmwRMsWOV2A3utdu6JM0v9PIdFE1tRrZvNQDsO/QI18zbf FGWLOp2Tgju0bdGP1yU8LsThdb6fIYg= X-Google-Smtp-Source: ABdhPJwI4fZflGnsTR+3rTM7oJQsyulxXWsDu/mf1k50/ZaTmseG/zf/VNiLcAP3NC79ULXC5a7NaA== X-Received: by 2002:a5d:588d:0:b0:20f:f290:ed83 with SMTP id n13-20020a5d588d000000b0020ff290ed83mr14866356wrf.588.1653666424488; Fri, 27 May 2022 08:47:04 -0700 (PDT) Received: from localhost.localdomain ([193.48.40.241]) by smtp.gmail.com with ESMTPSA id y4-20020a056000168400b0020fd392df33sm2069044wrd.29.2022.05.27.08.47.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 May 2022 08:47:04 -0700 (PDT) From: zimoun Date: Fri, 27 May 2022 17:46:52 +0200 Message-Id: <20220527154652.700549-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 date is a valid integer. * tests/cache.scm: Test it. --- guix/cache.scm | 16 ++++++++++++---- tests/cache.scm | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) base-commit: 38bf6c7d0cb588e8d4546db7d2e0bae2ec25183d diff --git a/guix/cache.scm b/guix/cache.scm index 51009809bd..13f9e4a439 100644 --- a/guix/cache.scm +++ b/guix/cache.scm @@ -20,6 +20,7 @@ (define-module (guix cache) #: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 +92,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 ((value (catch 'system-error + (lambda () + (call-with-input-file expiry-file get-string-all)) + (const "0")))) + (catch #t ; Handle value out of range (e.g., 1234567890 -> 12E4567890) + (lambda () + ;; 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. + (or (string->number value) 0)) + (const 0)))) (when (obsolete? last-expiry-date now cleanup-period) (remove-expired-cache-entries (cache-entries cache) 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: