Skip to content

Commit 3d7f5ef

Browse files
committed
time: wrong Sign Cast in Time.UnmarshalBinary (V2 Format)
Fixes #78528
1 parent 081aa64 commit 3d7f5ef

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/time/time.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ func (t *Time) UnmarshalBinary(data []byte) error {
15521552
buf = buf[4:]
15531553
offset := int(int16(buf[1])|int16(buf[0])<<8) * 60
15541554
if version == timeBinaryVersionV2 {
1555-
offset += int(buf[2])
1555+
offset += int(int8(buf[2]))
15561556
}
15571557

15581558
*t = Time{}

src/time/time_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,29 @@ func TestMarshalBinaryVersion2(t *testing.T) {
17521752
}
17531753
}
17541754

1755+
func TestMarshalBinaryVersion2Bugfix(t *testing.T) {
1756+
offsetSec := -5*3600 - 30*60 - 45 // -19845
1757+
loc := FixedZone("LMT", offsetSec)
1758+
1759+
t1 := Date(2024, 6, 15, 12, 0, 0, 0, loc)
1760+
b, err := t1.MarshalBinary()
1761+
if err != nil {
1762+
t.Errorf("Failed to Marshal, error = %v", err)
1763+
}
1764+
1765+
t2 := Time{}
1766+
err = t2.UnmarshalBinary(b)
1767+
if err != nil {
1768+
t.Errorf("Failed to Unmarshal, error = %v", err)
1769+
}
1770+
1771+
_, t1OriginOffset := t1.Zone()
1772+
_, t2OriginOffset := t2.Zone()
1773+
if t1OriginOffset != t2OriginOffset {
1774+
t.Errorf("The result offsetSec: %d after Unmarshal is not matched original offsetSec: %d", t2OriginOffset, t1OriginOffset)
1775+
}
1776+
}
1777+
17551778
func TestUnmarshalTextAllocations(t *testing.T) {
17561779
in := []byte(testdataRFC3339UTC) // short enough to be stack allocated
17571780
if allocs := testing.AllocsPerRun(100, func() {

0 commit comments

Comments
 (0)