Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Empty slice is returned as nil #2067

Open
stepanovdmitrii opened this issue Jun 26, 2024 · 1 comment
Open

[BUG]: Empty slice is returned as nil #2067

stepanovdmitrii opened this issue Jun 26, 2024 · 1 comment
Labels
kind/bug Something is broken. Stale

Comments

@stepanovdmitrii
Copy link

What version of Badger are you using?

a09e983

What version of Go are you using?

1.22.4

Have you tried reproducing the issue with the latest release?

Yes

What is the hardware spec (RAM, CPU, OS)?

Linux workstation 6.8.11-300.fc40.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 27 14:53:33 UTC 2024 x86_64 GNU/Linux

What steps will reproduce the bug?

Test:

func TestSetAndGetEmptySlice(t *testing.T) {
	dir := t.TempDir()
	db, err := Open(DefaultOptions(dir))
	require.NoError(t, err)
	defer func() {
		require.NoError(t, db.Close())
	}()

	key := []byte("key")
	value := []byte{}
	err = db.Update(func(txn *Txn) error {
		return txn.Set(key, value)
	})
	require.NoError(t, err)

	var item *Item
	err = db.View(func(txn *Txn) (dbErr error) {
		item, dbErr = txn.Get(key)
		return dbErr
	})
	require.NoError(t, err)
	require.NotNil(t, item)

	dbValue, err := item.ValueCopy(nil)
	require.NoError(t, err)
	require.Equal(t, value, dbValue)
}

Test result:

--- FAIL: TestSetAndGetEmptySlice (0.01s)
...
        	Error:      	Not equal: 
        	            	expected: []byte{}
        	            	actual  : []byte(nil)
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,3 +1,2 @@
        	            	-([]uint8) {
        	            	-}
        	            	+([]uint8) <nil>
        	            	 
        	Test:       	TestSetAndGetEmptySlice
FAIL
FAIL	github.com/dgraph-io/badger/v4	0.021s
FAIL

Expected behavior and actual result.

An empty slice should be returned.

Additional information

The similar test for bbolt passes:

func TestDB_SetAndGetEmptySlice(t *testing.T) {
	db := btesting.MustCreateDB(t)
	bucket := []byte("test bucket")
	key := []byte("key")
	value := []byte{}
	err := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists(bucket)
		if err != nil {
			return err
		}
		return b.Put(key, value)
	})
	require.NoError(t, err)

	var dbValue []byte
	err = db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket(bucket)
		dbValue = b.Get(key)
		return nil
	})
	require.NoError(t, err)

	require.Equal(t, value, dbValue)
}
@stepanovdmitrii stepanovdmitrii added the kind/bug Something is broken. label Jun 26, 2024
Copy link

This issue has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.

@github-actions github-actions bot added the Stale label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something is broken. Stale
Development

No branches or pull requests

1 participant