mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Ensure Marshallable.SizeBytes() always works on a typed nil pointer.
This lets go-marshal replace various calls to binary.Size() throughout the sentry without requiring concrete objects. PiperOrigin-RevId: 295299965
This commit is contained in:
@@ -301,7 +301,7 @@ func (g *interfaceGenerator) emitMarshallable() {
|
||||
primitiveSize += size
|
||||
} else {
|
||||
g.recordUsedMarshallable(t.Name)
|
||||
dynamicSizeTerms = append(dynamicSizeTerms, fmt.Sprintf("%s.SizeBytes()", g.fieldAccessor(n)))
|
||||
dynamicSizeTerms = append(dynamicSizeTerms, fmt.Sprintf("(*%s)(nil).SizeBytes()", t.Name))
|
||||
}
|
||||
},
|
||||
selector: func(n, tX, tSel *ast.Ident) {
|
||||
|
||||
@@ -167,11 +167,26 @@ func (g *testGenerator) emitTestWriteToUnmarshalPreservesData() {
|
||||
})
|
||||
}
|
||||
|
||||
func (g *testGenerator) emitTestSizeBytesOnTypedNilPtr() {
|
||||
g.inTestFunction("TestSizeBytesOnTypedNilPtr", func() {
|
||||
g.emit("var x %s\n", g.typeName())
|
||||
g.emit("sizeFromConcrete := x.SizeBytes()\n")
|
||||
g.emit("sizeFromTypedNilPtr := (*%s)(nil).SizeBytes()\n\n", g.typeName())
|
||||
|
||||
g.emit("if sizeFromTypedNilPtr != sizeFromConcrete {\n")
|
||||
g.inIndent(func() {
|
||||
g.emit("t.Fatalf(\"SizeBytes() on typed nil pointer (%v) doesn't match size returned by a concrete object (%v).\\n\", sizeFromTypedNilPtr, sizeFromConcrete)")
|
||||
})
|
||||
g.emit("}\n")
|
||||
})
|
||||
}
|
||||
|
||||
func (g *testGenerator) emitTests() {
|
||||
g.emitTestNonZeroSize()
|
||||
g.emitTestSuspectAlignment()
|
||||
g.emitTestMarshalUnmarshalPreservesData()
|
||||
g.emitTestWriteToUnmarshalPreservesData()
|
||||
g.emitTestSizeBytesOnTypedNilPtr()
|
||||
}
|
||||
|
||||
func (g *testGenerator) write(out io.Writer) error {
|
||||
|
||||
@@ -48,6 +48,10 @@ type Marshallable interface {
|
||||
|
||||
// SizeBytes is the size of the memory representation of a type in
|
||||
// marshalled form.
|
||||
//
|
||||
// SizeBytes must handle a nil receiver. Practically, this means SizeBytes
|
||||
// cannot deference any fields on the object implementing it (but will
|
||||
// likely make use of the type of these fields).
|
||||
SizeBytes() int
|
||||
|
||||
// MarshalBytes serializes a copy of a type to dst. dst must be at least
|
||||
|
||||
Reference in New Issue
Block a user