From e712b4716cbbe08abc7e7f79c5b4a59e51a0b900 Mon Sep 17 00:00:00 2001 From: Fabio Ponciroli Date: Wed, 6 Dec 2023 14:38:21 +0100 Subject: [PATCH] Make sure ref to prune is in packed refs RefDirectory:pack might raise an NPE when deleting loose refs as final part of the RefDirectory.pack(). This is what the code does: 1) packed ref update: update the list of refs which will be persisted in packed-refs 2) persit packed-refs: flush on file the refs computed in #1 3) prune loose refs: prune loose refs that have been packed in #2 The code correctly locks the packed-refs file during phases 1 to 3. However, it makes the wrong assumption of considering the loose refs set as immutable between phases 1 and 3. The number and values of loose refs on the filesystem can mutate at any time whilst the RefDirectory.pack() is in progress. Assuming the contrary can lead to an NPE when retrieving refs from the mutable loose refs list during phase #3. Make sure that the ref is not null before dereferencing its object-id value. Bug: jgit-4 Change-Id: I2cd01f8a880f3c6561ad978a389ec2db45b6018b --- .../org/eclipse/jgit/internal/storage/file/RefDirectory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java index f0676d9bec..9f31e688c3 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java @@ -813,7 +813,7 @@ public class RefDirectory extends RefDatabase { } Ref packedRef = newPacked.get(refName); ObjectId clr_oid = currentLooseRef.getObjectId(); - if (clr_oid != null + if (clr_oid != null && packedRef != null && clr_oid.equals(packedRef.getObjectId())) { RefList curLoose, newLoose; do { -- 2.39.5