Skip to content

Commit

Permalink
Fix nested structs on 32bit archs
Browse files Browse the repository at this point in the history
Not entirely sure why this fails, but this fixes it in the meanwhile.

Fixes #314
  • Loading branch information
arp242 committed Sep 23, 2021
1 parent f50d86c commit 1891d01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 11 additions & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,17 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
if typeIsHash(tomlTypeOfGo(frv)) {
fieldsSub = append(fieldsSub, append(start, f.Index...))
} else {
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
// Copy so it works correct on 32bit archs; not clear why this
// is needed. See #314, and https://www.reddit.com/r/golang/comments/pnx8v4
// This also works fine on 64bit, but 32bit archs are somewhat
// rare and this is a wee bit faster.
if math.MaxInt == math.MaxInt32 {
copyStart := make([]int, len(start))
copy(copyStart, start)
fieldsDirect = append(fieldsDirect, append(copyStart, f.Index...))
} else {
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
}
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,21 @@ Fun = "why would you do this?"
}
}

// Would previously fail on 32bit architectures; can test with:
// GOARCH=386 go test -c && ./toml.test
// GOARCH=arm GOARM=7 go test -c && qemu-arm ./toml.test
func TestEncode32bit(t *testing.T) {
type Inner struct {
A, B, C string
}
type Outer struct{ Inner }

encodeExpected(t, "embedded anonymous untagged struct",
Outer{Inner{"a", "b", "c"}},
"A = \"a\"\nB = \"b\"\nC = \"c\"\n",
nil)
}

func encodeExpected(t *testing.T, label string, val interface{}, want string, wantErr error) {
t.Helper()

Expand All @@ -420,8 +435,6 @@ func encodeExpected(t *testing.T, label string, val interface{}, want string, wa
want = strings.TrimSpace(want)
if want != have {
t.Errorf("\nhave:\n%s\nwant:\n%s\n", have, want)
// v, _ := json.MarshalIndent(val, "", " ")
// t.Log(string(v))
}
})
}

0 comments on commit 1891d01

Please sign in to comment.