From 08b783aa212c3e154a4a19e6a9e85cfd12f6c96f Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sat, 14 Nov 2026 12:25:08 +0100 Subject: [PATCH] [Go] Reject generated flyweight group headers whose blockLength is below the fixed fields. WrapForDecode now panics when a group with entries has a blockLength smaller than the fields present at the acting version, so Next cannot hand out entries aliasing the same bytes. Constant fields and composite members newer than the acting version must stay excluded. --- .gitignore | 2 +- .../otf/flyweight_group_block_length_test.go | 38 ++++++++++ .../flyweight/GolangFlyweightGenerator.java | 71 ++++++++++++++++++- .../flyweight-group-block-length-schema.xml | 36 ++++++++++ 4 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 gocode/flyweight/src/otf/flyweight_group_block_length_test.go create mode 100644 sbe-tool/src/test/resources/flyweight-group-block-length-schema.xml diff --git a/.gitignore b/.gitignore index 02c31be8a8..8fd57d676e 100644 --- a/.gitignore +++ b/.gitignore @@ -79,7 +79,7 @@ gocode/flyweight/src/*/*.go !gocode/flyweight/src/*/*_test.go gocode/flyweight/src/*/*/*.go !gocode/flyweight/src/*/*/*_test.go -gocode/flyweight/src/otf/code-generation-schema.sbeir +gocode/flyweight/src/otf/*.sbeir # csharp csharp/*/bin diff --git a/gocode/flyweight/src/otf/flyweight_group_block_length_test.go b/gocode/flyweight/src/otf/flyweight_group_block_length_test.go new file mode 100644 index 0000000000..bfd557b5af --- /dev/null +++ b/gocode/flyweight/src/otf/flyweight_group_block_length_test.go @@ -0,0 +1,38 @@ +package otf + +import ( + "encoding/binary" + "github.com/real-logic/simple-binary-encoding/otf/test/flyweight_group_block_length" + "github.com/stretchr/testify/assert" + "testing" +) + +//go:generate java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -Dsbe.output.dir=. -Dsbe.target.language=golang -Dsbe.go.generate.generate.flyweights=true -Dsbe.generate.ir=true -Dsbe.keyword.append.token=X -jar ../../../${SBE_JAR} ../../../../sbe-tool/src/test/resources/flyweight-group-block-length-schema.xml + +const groupBlockLengthMessage = "group blockLength is less than the fields present at the acting version [E108]" + +// wrapQuotes wraps a Quotes message whose group header carries the given blockLength and count. +func wrapQuotes(actingVersion uint64, blockLength uint16, count uint16) *flyweight_group_block_length.Quotes { + buffer := make([]byte, 8+4+int(count)*16) + binary.LittleEndian.PutUint16(buffer[8:], blockLength) + binary.LittleEndian.PutUint16(buffer[10:], count) + + var quotes flyweight_group_block_length.Quotes + return quotes.WrapForDecode(buffer, 8, 0, actingVersion, uint64(len(buffer))) +} + +func TestGroupBlockLengthMustCoverFieldsAtActingVersion(t *testing.T) { + assert := assert.New(t) + + // A version 0 entry holds only the 8 byte mantissa: side was added to the composite in version 1. + assert.NotPanics(func() { wrapQuotes(0, 8, 2).Entries() }) + assert.PanicsWithValue(groupBlockLengthMessage, func() { wrapQuotes(0, 7, 2).Entries() }) + assert.PanicsWithValue(groupBlockLengthMessage, func() { wrapQuotes(0, 0, 3).Entries() }) + + // From version 1 the entry also holds side, and the constant field adds no bytes. + assert.NotPanics(func() { wrapQuotes(1, 9, 2).Entries() }) + assert.PanicsWithValue(groupBlockLengthMessage, func() { wrapQuotes(1, 8, 2).Entries() }) + + // An empty group consumes nothing, whatever its blockLength. + assert.NotPanics(func() { wrapQuotes(1, 0, 0).Entries() }) +} diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang/flyweight/GolangFlyweightGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang/flyweight/GolangFlyweightGenerator.java index 8fa84c80c6..134edb7c62 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang/flyweight/GolangFlyweightGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/golang/flyweight/GolangFlyweightGenerator.java @@ -70,6 +70,74 @@ public GolangFlyweightGenerator( this.outputManager = outputManager; } + // Records, per schema version, the end of the bytes the fixed fields occupy from that version on. Constant fields + // occupy no bytes and composite members newer than the acting version are only counted from their own version. + private static void collectFieldExtents( + final List tokens, + final int typeIndex, + final int baseOffset, + final int enclosingVersion, + final SortedMap extents) + { + final Token typeToken = tokens.get(typeIndex); + if (typeToken.isConstantEncoding()) + { + return; + } + + final int version = Math.max(enclosingVersion, typeToken.version()); + final int offset = baseOffset + typeToken.offset(); + if (Signal.BEGIN_COMPOSITE != typeToken.signal()) + { + extents.merge(version, offset + typeToken.encodedLength(), Math::max); + return; + } + + final int endIndex = typeIndex + typeToken.componentTokenCount() - 1; + for (int i = typeIndex + 1; i < endIndex; i += tokens.get(i).componentTokenCount()) + { + collectFieldExtents(tokens, i, offset, version, extents); + } + } + + private static String generateMinBlockLengthCheck(final List fields, final String indent) + { + final SortedMap extents = new TreeMap<>(); + for (int i = 0, size = fields.size(); i < size; ) + { + final Token fieldToken = fields.get(i); + if (fieldToken.signal() == Signal.BEGIN_FIELD && !fieldToken.isConstantEncoding()) + { + collectFieldExtents(fields, i + 1, 0, fieldToken.version(), extents); + } + + i += fieldToken.componentTokenCount(); + } + + extents.values().removeIf(end -> end <= 0); + if (extents.isEmpty()) + { + return ""; + } + + final StringBuilder sb = new StringBuilder(); + final Integer baseLength = extents.remove(0); + sb.append(indent).append("\tminBlockLength := uint64(").append(null == baseLength ? 0 : baseLength) + .append(")\n"); + extents.forEach( + (version, end) -> sb + .append(indent).append("\tif actingVersion >= ").append(version) + .append(" && minBlockLength < ").append(end).append(" {\n") + .append(indent).append("\t\tminBlockLength = ").append(end).append("\n") + .append(indent).append("\t}\n")); + sb.append(indent).append("\tif g.count > 0 && g.blockLength < minBlockLength {\n") + .append(indent) + .append("\t\tpanic(\"group blockLength is less than the fields present at the acting version [E108]\")\n") + .append(indent).append("\t}\n"); + + return sb.toString(); + } + private static void generateGroupClassHeader( final StringBuilder sb, final String groupClassName, @@ -155,13 +223,14 @@ private static void generateGroupClassHeader( indent + "\tg.bufferLength = bufferLength\n" + indent + "\tg.blockLength = uint64(dimensions.BlockLength())\n" + indent + "\tg.count = uint64(dimensions.NumInGroup())\n" + + "%4$s" + indent + "\tg.index = 0\n" + indent + "\tg.actingVersion = actingVersion\n" + indent + "\tg.initialPosition = *pos\n" + indent + "\tg.positionPtr = pos\n" + indent + "\t*g.positionPtr = *g.positionPtr + %1$d\n" + indent + "}\n", - dimensionHeaderLength, dimensionsClassName, groupClassName); + dimensionHeaderLength, dimensionsClassName, groupClassName, generateMinBlockLengthCheck(fields, indent)); final long minCount = numInGroupToken.encoding().applicableMinValue().longValue(); final String minCheck = minCount > 0 ? "count < " + minCount + " || " : ""; diff --git a/sbe-tool/src/test/resources/flyweight-group-block-length-schema.xml b/sbe-tool/src/test/resources/flyweight-group-block-length-schema.xml new file mode 100644 index 0000000000..a44856f8b0 --- /dev/null +++ b/sbe-tool/src/test/resources/flyweight-group-block-length-schema.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + 1 + 2 + + X + + + + + + + + + + + +