nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: cberner/redb
Pull request #167 (2022-02-12 22:11:15)

Fix crash when initializing a db with < 4 bytes of space


Patch:
diff --git a/src/tree_store/page_store/page_manager.rs b/src/tree_store/page_store/page_manager.rs
index f1a4e71..0f308a4 100644
--- a/src/tree_store/page_store/page_manager.rs
+++ b/src/tree_store/page_store/page_manager.rs
@@ -462,6 +462,9 @@ impl TransactionalMemory {
         let mmap = Mmap::new(mmap);
         // Safety: we have exclusive access to the mmap
         let all_memory = unsafe { mmap.get_memory_mut(0..mmap.len()) };
+        if all_memory.len() < MAGICNUMBER.len() {
+            return Err(Error::OutOfSpace);
+        }
 
         let mutex = Mutex::new(MetapageGuard {});
         if all_memory[0..MAGICNUMBER.len()] != MAGICNUMBER {
@@ -1038,11 +1041,12 @@ impl Drop for TransactionalMemory {
 #[cfg(test)]
 mod test {
     use crate::tree_store::page_store::page_manager::{
-        set_allocator_dirty, DB_SIZE_OFFSET, GOD_BYTE_OFFSET, UPGRADE_IN_PROGRESS,
-        UPGRADE_LOG_OFFSET,
+        set_allocator_dirty, DB_SIZE_OFFSET, GOD_BYTE_OFFSET, MIN_USABLE_PAGES,
+        UPGRADE_IN_PROGRESS, UPGRADE_LOG_OFFSET,
     };
+    use crate::tree_store::page_store::utils::get_page_size;
     use crate::tree_store::page_store::TransactionalMemory;
-    use crate::{Database, ReadOnlyTable, ReadableTable, Table};
+    use crate::{Database, Error, ReadOnlyTable, ReadableTable, Table};
     use memmap2::{MmapMut, MmapRaw};
     use std::fs::OpenOptions;
     use std::mem::size_of;
@@ -1126,4 +1130,20 @@ mod test {
         table.insert(b"hello2", b"world2").unwrap();
         write_txn.commit().unwrap();
     }
+
+    #[test]
+    fn too_small_db() {
+        let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+        let result = unsafe { Database::create(tmpfile.path(), 1) };
+        assert!(matches!(result, Err(Error::OutOfSpace)));
+
+        let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+        let result = unsafe { Database::create(tmpfile.path(), 1024) };
+        assert!(matches!(result, Err(Error::OutOfSpace)));
+
+        let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+        let result =
+            unsafe { Database::create(tmpfile.path(), MIN_USABLE_PAGES * get_page_size() - 1) };
+        assert!(matches!(result, Err(Error::OutOfSpace)));
+    }
 }


Test patch:
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
index 12c1cef..721be83 100644
--- a/tests/integration_tests.rs
+++ b/tests/integration_tests.rs
@@ -38,14 +38,6 @@ fn mixed_durable_commit() {
     txn.commit().unwrap();
 }
 
-#[test]
-fn small_db() {
-    let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
-
-    let result = unsafe { Database::create(tmpfile.path(), 1024) };
-    assert!(matches!(result, Err(Error::OutOfSpace)))
-}
-
 #[test]
 fn non_durable_commit_persistence() {
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
Repository: cberner/redb
Pull request #305 (2022-07-30 16:22:57)

Implement RedbValue for the unit type


Patch:
diff --git a/src/types.rs b/src/types.rs
index 82c9e5d..aff54c1 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -55,6 +55,28 @@ pub trait RedbKey: RedbValue {
     fn compare(data1: &[u8], data2: &[u8]) -> Ordering;
 }
 
+impl RedbValue for () {
+    type View = OwnedLifetime<()>;
+    type ToBytes = RefAsBytesLifetime<[u8]>;
+
+    fn fixed_width() -> Option<usize> {
+        Some(0)
+    }
+
+    #[allow(clippy::unused_unit)]
+    fn from_bytes(_data: &[u8]) -> <Self::View as WithLifetime>::Out {
+        ()
+    }
+
+    fn as_bytes(&self) -> <Self::ToBytes as AsBytesWithLifetime>::Out {
+        &[]
+    }
+
+    fn redb_type_name() -> String {
+        "()".to_string()
+    }
+}
+
 impl RedbValue for [u8] {
     type View = RefLifetime<[u8]>;
     type ToBytes = RefAsBytesLifetime<[u8]>;


Test patch:
diff --git a/tests/basic_tests.rs b/tests/basic_tests.rs
index 6425b63..40e32a1 100644
--- a/tests/basic_tests.rs
+++ b/tests/basic_tests.rs
@@ -435,6 +435,25 @@ fn str_type() {
     assert!(iter.next().is_none());
 }
 
+#[test]
+fn empty_type() {
+    let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+    let db = unsafe { Database::create(tmpfile.path(), 1024 * 1024).unwrap() };
+
+    let definition: TableDefinition<u8, ()> = TableDefinition::new("x");
+
+    let write_txn = db.begin_write().unwrap();
+    {
+        let mut table = write_txn.open_table(definition).unwrap();
+        table.insert(&0, &()).unwrap();
+    }
+    write_txn.commit().unwrap();
+
+    let read_txn = db.begin_read().unwrap();
+    let table = read_txn.open_table(definition).unwrap();
+    assert!(!table.is_empty().unwrap());
+}
+
 #[test]
 fn array_type() {
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
Repository: google/osv-scanner
Pull request #426 (2023-06-27 20:37:37)

test: ensure fixtures directory isn't already a git repository
Currently if the test suite is exited prematurely by a ctrl+c or if it runs badly such as due to invalid flags being passed, it'll not properly do its cleanup - this'll then cause the suite to fail with a panic until the `.git` directory is manually removed from `.cmd/osv-scanner/fixtures`.


Patch:
diff --git a/.gitignore b/.gitignore
index e982b9a..c2999dd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 .history/
 .vscode/
+.idea/
 /dist/
-/osv-scanner
\ No newline at end of file
+/osv-scanner


Test patch:
diff --git a/cmd/osv-scanner/main_test.go b/cmd/osv-scanner/main_test.go
index b199fb0..6d2fa4b 100644
--- a/cmd/osv-scanner/main_test.go
+++ b/cmd/osv-scanner/main_test.go
@@ -506,6 +506,10 @@ func TestRun_LockfileWithExplicitParseAs(t *testing.T) {
 }
 
 func TestMain(m *testing.M) {
+	// ensure a git repository doesn't already exist in the fixtures directory,
+	// in case we didn't get a chance to clean-up properly in the last run
+	os.RemoveAll("./fixtures/.git")
+
 	// Temporarily make the fixtures folder a git repository to prevent gitignore files messing with tests.
 	_, err := git.PlainInit("./fixtures", false)
 	if err != nil {