summaryrefslogtreecommitdiff
path: root/guix/store
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2021-01-31 16:14:59 +0100
committerLudovic Courtès <ludo@gnu.org>2021-01-31 21:49:58 +0100
commit13a7d2a538b00aa0a8cf9b999f1a4ff3e5959af9 (patch)
tree0781cb715eb947cd41d4a490c6a37fce047dd6e6 /guix/store
parent9c4869fe9ca76caafe3ccfebfcb4a22d44cdf3f5 (diff)
database: Validate #:nar-size and #:time when registering store items.
* guix/store/database.scm (assert-integer): New procedure. (update-or-insert): Use it to validate NAR-SIZE and TIME. * tests/store-database.scm ("sqlite-register with incorrect size"): New test.
Diffstat (limited to 'guix/store')
-rw-r--r--guix/store/database.scm16
1 files changed, 15 insertions, 1 deletions
diff --git a/guix/store/database.scm b/guix/store/database.scm
index 4eea166d92..8d08def833 100644
--- a/guix/store/database.scm
+++ b/guix/store/database.scm
@@ -1,6 +1,6 @@
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2017, 2019 Caleb Ristvedt <caleb.ristvedt@cune.org>
-;;; Copyright © 2018, 2020 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2018, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;;
;;; This file is part of GNU Guix.
@@ -241,12 +241,26 @@ identifier. Otherwise, return #f."
"INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize)
VALUES (:path, :hash, :time, :deriver, :size)")
+(define-inlinable (assert-integer proc in-range? key number)
+ (unless (integer? number)
+ (throw 'wrong-type-arg proc
+ "Wrong type argument ~A: ~S" (list key number)
+ (list number)))
+ (unless (in-range? number)
+ (throw 'out-of-range proc
+ "Integer ~A out of range: ~S" (list key number)
+ (list number))))
+
(define* (update-or-insert db #:key path deriver hash nar-size time)
"The classic update-if-exists and insert-if-doesn't feature that sqlite
doesn't exactly have... they've got something close, but it involves deleting
and re-inserting instead of updating, which causes problems with foreign keys,
of course. Returns the row id of the row that was modified or inserted."
+ ;; Make sure NAR-SIZE is valid.
+ (assert-integer "update-or-insert" positive? #:nar-size nar-size)
+ (assert-integer "update-or-insert" (cut >= <> 0) #:time time)
+
;; It's important that querying the path-id and the insert/update operation
;; take place in the same transaction, as otherwise some other
;; process/thread/fiber could register the same path between when we check