Skip to content

Commit

Permalink
cli: verify index file construction in upload-bin command
Browse files Browse the repository at this point in the history
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 <ekt@morphbits.io>
  • Loading branch information
AliceInHunterland committed Oct 23, 2024
1 parent 42c8e40 commit a5e9ab6
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cli/util/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"slices"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -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)

Check warning on line 351 in cli/util/uploader.go

View check run for this annotation

Codecov / codecov/patch

cli/util/uploader.go#L350-L351

Added lines #L350 - L351 were not covered by tests
)
defer close(oidCh)
for range maxParallelSearches {
Expand Down Expand Up @@ -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))
}

Check warning on line 438 in cli/util/uploader.go

View check run for this annotation

Codecov / codecov/patch

cli/util/uploader.go#L435-L438

Added lines #L435 - L438 were not covered by tests
}
attrs := []object.Attribute{
*object.NewAttribute(attributeKey, strconv.Itoa(int(i))),
*object.NewAttribute("IndexSize", strconv.Itoa(int(indexFileSize))),
Expand Down

0 comments on commit a5e9ab6

Please sign in to comment.