diff --git a/internal/sysenc/marshal.go b/internal/sysenc/marshal.go index 52056dff6..52339456a 100644 --- a/internal/sysenc/marshal.go +++ b/internal/sysenc/marshal.go @@ -106,7 +106,15 @@ func Unmarshal(data interface{}, buf []byte) error { rd.Reset(buf) - return binary.Read(rd, internal.NativeEndian, value) + if err := binary.Read(rd, internal.NativeEndian, value); err != nil { + return err + } + + if rd.Len() != 0 { + return fmt.Errorf("unmarshaling %T doesn't consume all data", data) + } + + return nil } } diff --git a/map_test.go b/map_test.go index 165434aa4..b33bc930e 100644 --- a/map_test.go +++ b/map_test.go @@ -230,6 +230,15 @@ func TestBatchAPIHash(t *testing.T) { } } +func TestMapLookupKeyTooSmall(t *testing.T) { + m := createArray(t) + defer m.Close() + + var small uint16 + qt.Assert(t, m.Put(uint32(0), uint32(1234)), qt.IsNil) + qt.Assert(t, m.Lookup(uint32(0), &small), qt.IsNotNil) +} + func TestBatchAPIMapDelete(t *testing.T) { if err := haveBatchAPI(); err != nil { t.Skipf("batch api not available: %v", err) @@ -1015,7 +1024,7 @@ func TestIterateEmptyMap(t *testing.T) { entries := m.Iterate() var key string - var value uint32 + var value uint64 if entries.Next(&key, &value) != false { t.Error("Empty hash should not be iterable") } @@ -1033,7 +1042,7 @@ func TestIterateEmptyMap(t *testing.T) { m := makeMap(t, mapType) entries := m.Iterate() var key string - var value uint32 + var value uint64 for entries.Next(&key, &value) { // Some empty arrays like sockmap don't return any keys. }