moov-io / iso8583

A golang implementation to marshal and unmarshal iso8583 message.
https://moov.io
Apache License 2.0
353 stars 105 forks source link

Panic on deserialising a short message #159

Closed peterebden closed 2 years ago

peterebden commented 2 years ago

Message.Unpack() panics if given an input shorter than 4 bytes:

    t.Run("Test unpacking an empty input", func(t *testing.T) {
        message := NewMessage(spec)
        message.Unpack(nil)
    })
--- FAIL: TestMessage (0.00s)
    --- FAIL: TestMessage/Test_unpacking_an_empty_input (0.00s)
panic: runtime error: slice bounds out of range [:4] with capacity 3 [recovered]
    panic: runtime error: slice bounds out of range [:4] with capacity 3

goroutine 25 [running]:
testing.tRunner.func1.2({0x6130a0, 0xc0000ae438})
    testing/testing.go:1360 +0x24e
testing.tRunner.func1()
    testing/testing.go:1363 +0x39f
panic({0x6130a0, 0xc0000ae438})
    runtime/panic.go:838 +0x207
github.com/moov-io/iso8583/encoding.asciiEncoder.Decode({}, {0xc0001310f8?, 0x0?, 0x7ff419b0a5e0?}, 0x8?)
    github.com/moov-io/iso8583/encoding/ascii.go:23 +0x145
github.com/moov-io/iso8583/field.(*String).Unpack(0xc0000e5420, {0xc0001310f8, 0x3, 0x3})
    github.com/moov-io/iso8583/field/string.go:84 +0xf3
github.com/moov-io/iso8583.(*Message).Unpack(0xc000098fc0, {0xc0001310f8, 0x3, 0x3})
    github.com/moov-io/iso8583/message.go:186 +0x256
github.com/moov-io/iso8583.TestMessage.func4(0x0?)
    github.com/moov-io/iso8583/message_test.go:189 +0x4c
testing.tRunner(0xc000164ea0, 0xc000097740)
    testing/testing.go:1410 +0x102
created by testing.(*T).Run
    testing/testing.go:1457 +0x35f
exit status 2
FAIL    github.com/moov-io/iso8583  0.010s
alovak commented 2 years ago

@peterebden please check #161 it should solve the issue. Thanks for reporting it!

peterebden commented 2 years ago

Thank you for fixing it!