nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: bitflags/bitflags
Pull request #426 (2024-12-20 12:07:41)

Add method to check if unknown bits are set
This PR adds a method to the `Flags` trait, `contains_unknown_bits(&self)`, to check whether any unknown bits are set (See #425). It also adds tests for this method.


Patch:
diff --git a/src/traits.rs b/src/traits.rs
index 3905d7d..e100268 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -152,6 +152,11 @@ pub trait Flags: Sized + 'static {
         Self::from_bits_retain(truncated)
     }
 
+    /// This method will return `true` if any unknown bits are set.
+    fn contains_unknown_bits(&self) -> bool {
+        Self::all().bits() & self.bits() != self.bits()
+    }
+
     /// Get the underlying bits value.
     ///
     /// The returned value is exactly the bits set in this flags value.


Test patch:
diff --git a/src/tests.rs b/src/tests.rs
index ed52ad4..a7dc28d 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -22,6 +22,7 @@ mod parser;
 mod remove;
 mod symmetric_difference;
 mod union;
+mod unknown;
 
 bitflags! {
     #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
diff --git a/src/tests/unknown.rs b/src/tests/unknown.rs
new file mode 100644
index 0000000..020f7e9
--- /dev/null
+++ b/src/tests/unknown.rs
@@ -0,0 +1,40 @@
+use super::*;
+
+use crate::Flags;
+
+#[test]
+fn cases() {
+    case(false, TestFlags::empty(), TestFlags::contains_unknown_bits);
+    case(false, TestFlags::A, TestFlags::contains_unknown_bits);
+
+    case(
+        true,
+        TestFlags::ABC | TestFlags::from_bits_retain(1 << 3),
+        TestFlags::contains_unknown_bits,
+    );
+
+    case(
+        true,
+        TestFlags::empty() | TestFlags::from_bits_retain(1 << 3),
+        TestFlags::contains_unknown_bits,
+    );
+
+    case(false, TestFlags::all(), TestFlags::contains_unknown_bits);
+
+    case(false, TestZero::empty(), TestZero::contains_unknown_bits);
+}
+#[track_caller]
+fn case<T: Flags + std::fmt::Debug>(expected: bool, value: T, inherent: impl FnOnce(&T) -> bool) {
+    assert_eq!(
+        expected,
+        inherent(&value),
+        "{:?}.contains_unknown_bits()",
+        value
+    );
+    assert_eq!(
+        expected,
+        Flags::contains_unknown_bits(&value),
+        "Flags::contains_unknown_bits({:?})",
+        value
+    );
+}
Repository: bitflags/bitflags
Pull request #428 (2025-01-07 19:31:31)

Add `truncate(&mut self)` method to unset unknown bits
Hi,

This PR adds the method `truncate(&mut self)` to the `Flags` trait (see #425), which unsets any unknown bits. It also adds some tests for this method.


Patch:
diff --git a/src/traits.rs b/src/traits.rs
index e100268..d351bfc 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -246,6 +246,14 @@ pub trait Flags: Sized + 'static {
         self.bits() & other.bits() == other.bits()
     }
 
+    /// Remove any unknown bits from the flags.
+    fn truncate(&mut self)
+    where
+        Self: Sized,
+    {
+        *self = Self::from_bits_truncate(self.bits());
+    }
+
     /// The bitwise or (`|`) of the bits in two flags values.
     fn insert(&mut self, other: Self)
     where


Test patch:
diff --git a/src/tests.rs b/src/tests.rs
index a7dc28d..28ed0a6 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -21,6 +21,7 @@ mod iter;
 mod parser;
 mod remove;
 mod symmetric_difference;
+mod truncate;
 mod union;
 mod unknown;
 
diff --git a/src/tests/truncate.rs b/src/tests/truncate.rs
new file mode 100644
index 0000000..e38df48
--- /dev/null
+++ b/src/tests/truncate.rs
@@ -0,0 +1,29 @@
+use super::*;
+
+use crate::Flags;
+
+#[test]
+fn cases() {
+    case(
+        TestFlags::ABC | TestFlags::from_bits_retain(1 << 3),
+        TestFlags::ABC,
+    );
+
+    case(TestZero::empty(), TestZero::empty());
+
+    case(TestZero::all(), TestZero::all());
+
+    case(
+        TestFlags::from_bits_retain(1 << 3) | TestFlags::all(),
+        TestFlags::all(),
+    );
+}
+
+#[track_caller]
+fn case<T: Flags + std::fmt::Debug>(mut before: T, after: T)
+where
+    T: std::fmt::Debug + PartialEq + Copy,
+{
+    before.truncate();
+    assert_eq!(before, after, "{:?}.truncate()", before);
+}
Repository: buchgr/bazel-remote
Pull request #221 (2020-03-06 22:12:09)

unexport server.ServeGRPC
This function is only used internally by the server package, and does not need to be exported.


Patch:
diff --git a/server/grpc.go b/server/grpc.go
index 2663a13..37530d0 100644
--- a/server/grpc.go
+++ b/server/grpc.go
@@ -43,10 +43,10 @@ func ListenAndServeGRPC(addr string, opts []grpc.ServerOption,
 		return err
 	}
 
-	return ServeGRPC(listener, opts, c, a, e)
+	return serveGRPC(listener, opts, c, a, e)
 }
 
-func ServeGRPC(l net.Listener, opts []grpc.ServerOption,
+func serveGRPC(l net.Listener, opts []grpc.ServerOption,
 	c *disk.DiskCache, a cache.Logger, e cache.Logger) error {
 
 	srv := grpc.NewServer(opts...)


Test patch:
diff --git a/server/grpc_test.go b/server/grpc_test.go
index 6069db0..bed5b76 100644
--- a/server/grpc_test.go
+++ b/server/grpc_test.go
@@ -73,7 +73,7 @@ func TestMain(m *testing.M) {
 	listener = bufconn.Listen(bufSize)
 
 	go func() {
-		err2 := ServeGRPC(
+		err2 := serveGRPC(
 			listener,
 			[]grpc.ServerOption{},
 			diskCache, accessLogger, errorLogger)
Repository: buchgr/bazel-remote
Pull request #340 (2020-08-26 15:29:31)

Don't add to cache if everything reserved
Edge case, but looks like previous behavior was unexpected in this case:
the Add method would:
  - call removeElement on the newly added element;
  - not add the new entry to the LRU;
  - return true.


Patch:
diff --git a/cache/disk/lru.go b/cache/disk/lru.go
index 77a1079..a2c1c13 100644
--- a/cache/disk/lru.go
+++ b/cache/disk/lru.go
@@ -74,7 +74,8 @@ func NewSizedLRU(maxSize int64, onEvict EvictCallback) SizedLRU {
 }
 
 // Add adds a (key, value) to the cache, evicting items as necessary. Add returns false (
-// and does not add the item) if the item size is larger than the maximum size of the cache.
+// and does not add the item) if the item size is larger than the maximum size of the cache,
+// or it cannot be added to the cache because too much space is reserved.
 func (c *sizedLRU) Add(key Key, value sizedItem) (ok bool) {
 	if value.Size() > c.maxSize {
 		return false
@@ -82,13 +83,19 @@ func (c *sizedLRU) Add(key Key, value sizedItem) (ok bool) {
 
 	var sizeDelta int64
 	if ee, ok := c.cache[key]; ok {
-		c.ll.MoveToFront(ee)
 		sizeDelta = value.Size() - ee.Value.(*entry).value.Size()
+		if c.reservedSize+sizeDelta > c.maxSize {
+			return false
+		}
+		c.ll.MoveToFront(ee)
 		ee.Value.(*entry).value = value
 	} else {
+		sizeDelta = value.Size()
+		if c.reservedSize+sizeDelta > c.maxSize {
+			return false
+		}
 		ele := c.ll.PushFront(&entry{key, value})
 		c.cache[key] = ele
-		sizeDelta = value.Size()
 	}
 
 	// Eviction. This is needed even if the key was already present, since the size of the


Test patch:
diff --git a/cache/disk/lru_test.go b/cache/disk/lru_test.go
index 2693916..d3638cb 100644
--- a/cache/disk/lru_test.go
+++ b/cache/disk/lru_test.go
@@ -122,7 +122,7 @@ func TestRejectBigItem(t *testing.T) {
 	checkSizeAndNumItems(t, lru, 0, 0)
 }
 
-func TestReserveSharesSpace(t *testing.T) {
+func TestReserveZeroAlwaysPossible(t *testing.T) {
 	largeItem := testSizedItem{math.MaxInt64, "pretend large item"}
 
 	lru := NewSizedLRU(math.MaxInt64, nil)
@@ -254,3 +254,30 @@ func TestUnreserve(t *testing.T) {
 			lru.TotalSize())
 	}
 }
+
+func TestAddWithSpaceReserved(t *testing.T) {
+	lru := NewSizedLRU(2, nil)
+
+	ok, err := lru.Reserve(1)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !ok {
+		t.Fatalf("Expected to be able to reserve 1")
+	}
+
+	ok = lru.Add("hello", &testSizedItem{2, "hello"})
+	if ok {
+		t.Fatal("Expected to not be able to add item with size 2")
+	}
+
+	err = lru.Unreserve(1)
+	if err != nil {
+		t.Fatal("Expected to be able to unreserve 1:", err)
+	}
+
+	ok = lru.Add("hello", &testSizedItem{2, "hello"})
+	if !ok {
+		t.Fatal("Expected to be able to add item with size 2")
+	}
+}
Repository: buchgr/bazel-remote
Pull request #410 (2021-03-20 02:57:19)

Fix v1->v2 cas migration
See https://github.com/buchgr/bazel-remote/issues/409


Patch:
diff --git a/cache/disk/disk.go b/cache/disk/disk.go
index ea2f9ee..36959b5 100644
--- a/cache/disk/disk.go
+++ b/cache/disk/disk.go
@@ -330,7 +330,7 @@ func migrateV1Subdir(oldDir string, destDir string, kind cache.EntryKind) error
 				return fmt.Errorf("Unexpected file: %s", oldPath)
 			}
 
-			destPath := path.Join(destDir, item.Name()) + ".v1"
+			destPath := path.Join(destDir, item.Name()) + "-556677.v1"
 			err = os.Rename(oldPath, destPath)
 			if err != nil {
 				return fmt.Errorf("Failed to migrate CAS blob %s: %w",


Test patch:
diff --git a/cache/disk/disk_test.go b/cache/disk/disk_test.go
index 21e5eae..d216391 100644
--- a/cache/disk/disk_test.go
+++ b/cache/disk/disk_test.go
@@ -597,7 +597,7 @@ func TestLoadExistingEntries(t *testing.T) {
 	cacheDir := testutils.TempDir(t)
 	defer os.RemoveAll(cacheDir)
 
-	numBlobs := int64(3)
+	numBlobs := int64(4)
 	blobSize := int64(1024)
 
 	var err error
@@ -612,6 +612,7 @@ func TestLoadExistingEntries(t *testing.T) {
 		t.Fatal(err)
 	}
 
+	// V0 CAS entry.
 	casData, casHash := testutils.RandomDataAndHash(blobSize)
 	err = os.MkdirAll(path.Join(cacheDir, "cas"), 0755)
 	if err != nil {
@@ -622,6 +623,17 @@ func TestLoadExistingEntries(t *testing.T) {
 		t.Fatal(err)
 	}
 
+	// V1 CAS entry.
+	casV1Data, casV1Hash := testutils.RandomDataAndHash(blobSize)
+	err = os.MkdirAll(path.Join(cacheDir, "cas", casV1Hash[:2]), 0755)
+	if err != nil {
+		t.Fatal(err)
+	}
+	err = ioutil.WriteFile(path.Join(cacheDir, "cas", casV1Hash[:2], casV1Hash), casV1Data, 0644)
+	if err != nil {
+		t.Fatal(err)
+	}
+
 	rawData, rawHash := testutils.RandomDataAndHash(blobSize)
 	err = os.MkdirAll(path.Join(cacheDir, "raw"), 0755)
 	if err != nil {
@@ -658,6 +670,11 @@ func TestLoadExistingEntries(t *testing.T) {
 		t.Fatalf("Expected cache to contain CAS entry '%s'", casHash)
 	}
 
+	found, _ = testCache.Contains(cache.CAS, casV1Hash, blobSize)
+	if !found {
+		t.Fatalf("Expected cache to contain CAS V1 entry '%s'", casV1Hash)
+	}
+
 	found, _ = testCache.Contains(cache.RAW, rawHash, blobSize)
 	if !found {
 		t.Fatalf("Expected cache to contain RAW entry '%s'", rawHash)
Repository: buchgr/bazel-remote
Pull request #650 (2023-03-11 17:40:20)

[disk] SizedLRU.Reserve should return a non-nil error if unable to reserve the required space
Relates to #649


Patch:
diff --git a/cache/disk/disk.go b/cache/disk/disk.go
index 32c6b10..ba7ef4b 100644
--- a/cache/disk/disk.go
+++ b/cache/disk/disk.go
@@ -278,7 +278,10 @@ func (c *diskCache) Put(ctx context.Context, kind cache.EntryKind, hash string,
 		ok, err := c.lru.Reserve(size)
 		if err != nil {
 			c.mu.Unlock()
-			return internalErr(err)
+			return &cache.Error{
+				Code: http.StatusInsufficientStorage,
+				Text: err.Error(),
+			}
 		}
 		if !ok {
 			c.mu.Unlock()
diff --git a/cache/disk/lru.go b/cache/disk/lru.go
index faeb60b..3213bd1 100644
--- a/cache/disk/lru.go
+++ b/cache/disk/lru.go
@@ -215,15 +215,19 @@ func (c *SizedLRU) Reserve(size int64) (bool, error) {
 		return true, nil
 	}
 
-	if size < 0 || size > c.maxSize {
-		return false, nil
+	if size < 0 {
+		return false, fmt.Errorf("Invalid negative blob size: %d", size)
+	}
+
+	if size > c.maxSize {
+		return false, fmt.Errorf("Unable to reserve space for blob (size: %d) larger than cache size %d", size, c.maxSize)
 	}
 
 	if sumLargerThan(size, c.reservedSize, c.maxSize) {
 		// If size + c.reservedSize is larger than c.maxSize
 		// then we cannot evict enough items to make enough
 		// space.
-		return false, nil
+		return false, fmt.Errorf("INTERNAL ERROR: unable to reserve enough space for blob with size %d (undersized cache?)", size)
 	}
 
 	// Evict elements until we are able to reserve enough space.


Test patch:
diff --git a/cache/disk/lru_test.go b/cache/disk/lru_test.go
index f7ae928..42a4dbb 100644
--- a/cache/disk/lru_test.go
+++ b/cache/disk/lru_test.go
@@ -156,10 +156,7 @@ func TestReserveAtCapacity(t *testing.T) {
 	}
 
 	ok, err = lru.Reserve(1)
-	if err != nil {
-		t.Fatal(err)
-	}
-	if ok {
+	if ok || err == nil {
 		t.Fatal("Should not be able to reserve any space")
 	}
 	if lru.TotalSize() != math.MaxInt64 {
@@ -184,19 +181,13 @@ func TestReserveOverflow(t *testing.T) {
 	}
 
 	ok, err = lru.Reserve(math.MaxInt64)
-	if err != nil {
-		t.Fatal(err)
-	}
-	if ok {
+	if ok || err == nil {
 		t.Fatal("Expected overflow")
 	}
 
 	lru = NewSizedLRU(10, nil, 0)
 	ok, err = lru.Reserve(math.MaxInt64)
-	if err != nil {
-		t.Fatal(err)
-	}
-	if ok {
+	if ok || err == nil {
 		t.Fatal("Expected overflow")
 	}
 }