Skip to content

Invalid test cases for runtime.Marshaler #1501

Closed
@movsb

Description

🐛 Bug Report

Taking address of any element in this slice produces the same value (because the element is an empty struct):

var marshalers [3]dummyMarshaler
specs := []struct {
opt runtime.ServeMuxOption
wantIn runtime.Marshaler
wantOut runtime.Marshaler
}{
{
opt: runtime.WithMarshalerOption(runtime.MIMEWildcard, &marshalers[0]),
wantIn: &marshalers[0],
wantOut: &marshalers[0],
},
{
opt: runtime.WithMarshalerOption("application/x-in", &marshalers[1]),
wantIn: &marshalers[1],
wantOut: &marshalers[0],
},
{
opt: runtime.WithMarshalerOption("application/x-out", &marshalers[2]),
wantIn: &marshalers[1],
wantOut: &marshalers[2],
},
}

So, these test cases almost won't work as expected.

As per the last line of golang spec:

A struct or array type has size zero if it contains no fields (or elements, respectively) that have a size greater than zero.

Two distinct zero-size variables may have the same address in memory.

To Reproduce

See the code at playground:

package main

import (
	"fmt"
)

type S struct {
}

type T struct {
	b bool
}

func main() {
	var s [3]S
	var t [3]T

	fmt.Printf("%p,%p,%p\n", &s[0], &s[1], &s[2])
	fmt.Printf("%p,%p,%p\n", &t[0], &t[1], &t[2])
}

Outputs:

0x58fd18,0x58fd18,0x58fd18
0xc00009400b,0xc00009400c,0xc00009400d

Your Environment

Any.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions