Be more defensive in decoding node id table in blocks.

This commit is contained in:
Sascha L. Teichmann 2015-08-23 22:48:54 +02:00
parent dc2845abb1
commit 011cd0e4f4

View File

@ -176,11 +176,24 @@ func NewDecodedBlock(data []byte, colors *Colors) (db *DecodedBlock, err error)
offset++ offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:])) numMappings := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2 offset += 2
for i := 0; i < numMappings; i++ { const outOfBounds = "Offset in node id table out of bounds. Ignored."
for i, n := 0, len(data); i < numMappings; i++ {
if offset+1 >= n {
log.Println(outOfBounds)
break
}
nodeId := int32(binary.BigEndian.Uint16(data[offset:])) nodeId := int32(binary.BigEndian.Uint16(data[offset:]))
offset += 2 offset += 2
if offset+1 >= n {
log.Println(outOfBounds)
break
}
nameLen := int(binary.BigEndian.Uint16(data[offset:])) nameLen := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2 offset += 2
if offset+nameLen-1 >= n {
log.Println(outOfBounds)
break
}
name := string(data[offset : offset+nameLen]) name := string(data[offset : offset+nameLen])
offset += nameLen offset += nameLen
switch name { switch name {
@ -191,7 +204,7 @@ func NewDecodedBlock(data []byte, colors *Colors) (db *DecodedBlock, err error)
default: default:
if index, found := colors.NameIndex[name]; found { if index, found := colors.NameIndex[name]; found {
indexMap[nodeId] = index indexMap[nodeId] = index
if colors.IsTransparent(index) { if !transparent && colors.IsTransparent(index) {
transparent = true transparent = true
} }
} else { } else {