From 6c857e1c025ff6cd1c0eddb80af1382da2095264 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 21 Oct 2026 14:23:48 +0200 Subject: [PATCH] A bytes file of exactly 64 KiB stays inline: take its exact decoded length (issue 1694) --- go/declaration/filereference.go | 4 +++- go/declaration/filereference_test.go | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/go/declaration/filereference.go b/go/declaration/filereference.go index 1bbdcdd..166022a 100644 --- a/go/declaration/filereference.go +++ b/go/declaration/filereference.go @@ -85,7 +85,9 @@ func (f *File) TravelsByDigest() bool { return false } if f.Bytes != "" { - return base64.StdEncoding.DecodedLen(len(f.Bytes)) > FileReferenceThreshold + // DecodedLen counts padding, so the exact length is taken off the decoded bytes. + body, err := f.Body() + return err == nil && len(body) > FileReferenceThreshold } return len(f.Content) > FileReferenceThreshold } diff --git a/go/declaration/filereference_test.go b/go/declaration/filereference_test.go index 957a054..6bf9be9 100644 --- a/go/declaration/filereference_test.go +++ b/go/declaration/filereference_test.go @@ -111,6 +111,13 @@ func TestWhichFilesTravelByDigest(t *testing.T) { t.Errorf("%s was made a reference", name) } } + exact := File{ID: "m.e", Bytes: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, FileReferenceThreshold))} + if exact.TravelsByDigest() { + t.Error("a bytes file of exactly the threshold travels by digest") + } + if _, _, err := exact.AsReference(); err == nil { + t.Error("a bytes file of exactly the threshold was made a reference") + } raw := bytes.Repeat([]byte{0, 1, 2}, FileReferenceThreshold) f := File{ID: "m.b", Bytes: base64.StdEncoding.EncodeToString(raw)} ref, body, err := f.AsReference()