Fix slice clash

This commit is contained in:
Neale Pickett 2018-07-23 21:17:41 -06:00
parent 495a68ce4b
commit 5d09c5eb96
2 changed files with 16 additions and 4 deletions

View File

@ -280,7 +280,14 @@ func (g GapString) Hexdump() string {
return out.String() return out.String()
} }
// XXX: These should probably move to Packet func (g GapString) Uint32LE() (uint32, GapString) {
return binary.LittleEndian.Uint32(g.Slice(0, 4).Bytes()), g.Slice(4, g.Length())
}
func (g GapString) Uint16LE() (uint16, GapString) {
return binary.LittleEndian.Uint16(g.Slice(0, 2).Bytes()), g.Slice(2, g.Length())
}
func (g GapString) Utf16(order binary.ByteOrder, fill string) string { func (g GapString) Utf16(order binary.ByteOrder, fill string) string {
in := g.Bytes([]byte(fill)...) in := g.Bytes([]byte(fill)...)
ints := make([]uint16, len(in)/2) ints := make([]uint16, len(in)/2)

View File

@ -85,16 +85,21 @@ func (stream *Stream) Read(length int) (Utterance, error) {
stream.pending.When = u.When stream.pending.When = u.When
} }
pendingLen := stream.pending.Data.Length()
// If we got nothing, it's the end of the stream // If we got nothing, it's the end of the stream
if stream.pending.Data.Length() == 0 { if pendingLen == 0 {
return Utterance{}, io.EOF return Utterance{}, io.EOF
} }
sliceLen := length
if sliceLen > pendingLen {
sliceLen = pendingLen
}
ret := Utterance{ ret := Utterance{
Data: stream.pending.Data.Slice(0, length), Data: stream.pending.Data.Slice(0, sliceLen),
When: stream.pending.When, When: stream.pending.When,
} }
stream.pending.Data = stream.pending.Data.Slice(length, stream.pending.Data.Length()) stream.pending.Data = stream.pending.Data.Slice(sliceLen, pendingLen)
return ret, nil return ret, nil
} }