From a5e9ab6979988d02e366240098dd6039fc09c3a3 Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Wed, 23 Oct 2024 15:27:45 +0300 Subject: [PATCH] cli: verify index file construction in `upload-bin` command Verify that there are no empty OIDs in the constructed index file, as empty payloads can result in improperly set attributes, leading to empty OIDs being added to the index file. Close #3628 Signed-off-by: Ekaterina Pavlova --- cli/util/uploader.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cli/util/uploader.go b/cli/util/uploader.go index cba55c7b9..96df4a2d2 100644 --- a/cli/util/uploader.go +++ b/cli/util/uploader.go @@ -4,6 +4,7 @@ import ( "context" "crypto/sha256" "fmt" + "slices" "strconv" "sync" "time" @@ -346,6 +347,8 @@ func updateIndexFiles(ctx *cli.Context, p *pool.Pool, containerID cid.ID, accoun buffer = make([]byte, indexFileSize*oidSize) oidCh = make(chan oid.ID, indexFileSize) oidFetcherToProcessor = make(chan struct{}, indexFileSize) + + emptyOid = make([]byte, oidSize) ) defer close(oidCh) for range maxParallelSearches { @@ -426,6 +429,14 @@ func updateIndexFiles(ctx *cli.Context, p *pool.Pool, containerID cid.ID, accoun } } } + // Check if there are any empty oids in the created index file. + // This could happen if object payload is empty -> + // attribute is not set correctly -> empty oid is added to the index file. + for k := 0; k < len(buffer); k += oidSize { + if slices.Compare(buffer[k:k+oidSize], emptyOid) == 0 { + return fmt.Errorf("empty oid found in index file %d at position %d (block index %d)", i, k/oidSize, i+uint(k/oidSize)) + } + } attrs := []object.Attribute{ *object.NewAttribute(attributeKey, strconv.Itoa(int(i))), *object.NewAttribute("IndexSize", strconv.Itoa(int(indexFileSize))),