Fix AddFile to verify actual bytes read matches declared size (closes #25) (#30)

After reading file content, verify `totalRead == size` and return an error on mismatch.

Co-authored-by: clawbot <clawbot@openclaw>
Reviewed-on: #30
Co-authored-by: clawbot <clawbot@noreply.example.org>
Co-committed-by: clawbot <clawbot@noreply.example.org>
This commit is contained in:
clawbot 2026-02-09 01:35:07 +01:00 committed by Jeffrey Paul
parent 4b80c0067b
commit ebaf2a65ca

View File

@ -3,6 +3,7 @@ package mfer
import (
"crypto/sha256"
"errors"
"fmt"
"io"
"sync"
"time"
@ -96,6 +97,11 @@ func (b *Builder) AddFile(
}
}
// Verify actual bytes read matches declared size
if totalRead != size {
return totalRead, fmt.Errorf("size mismatch for %q: declared %d bytes but read %d bytes", path, size, totalRead)
}
// Encode hash as multihash (SHA2-256)
mh, err := multihash.Encode(h.Sum(nil), multihash.SHA2_256)
if err != nil {