fix some overflows

types
Jordan Orelli 8 years ago
parent d7a9bd98bf
commit 701d08ec7c

@ -99,6 +99,26 @@ func ReadVarInt32(r Reader) uint32 {
return uint32(x) return uint32(x)
} }
// reads a 32bit varint
func ReadVarInt16(r Reader) uint16 {
var (
x uint64
b uint64
shift uint
)
for ; shift < 16; shift += 7 {
b = r.ReadBits(8)
if r.Err() != nil {
return 0
}
x |= b & 0x7f << shift
if b&0x80 == 0 {
return uint16(x)
}
}
return uint16(x)
}
func ReadBool(r Reader) bool { func ReadBool(r Reader) bool {
return r.ReadBits(1) != 0 return r.ReadBits(1) != 0
} }

@ -50,7 +50,7 @@ type array_t struct {
} }
func (t *array_t) nü() value { return array{t: t, slots: make([]value, t.count)} } func (t *array_t) nü() value { return array{t: t, slots: make([]value, t.count)} }
func (t array_t) typeName() string { return fmt.Sprintf("array:%s", t.elem.typeName()) } func (t array_t) typeName() string { return fmt.Sprintf("%s[%d]", t.elem.typeName(), t.count) }
type array struct { type array struct {
t *array_t t *array_t

@ -48,11 +48,7 @@ type uint8_v uint8
func (v uint8_v) tÿpe() tÿpe { return uint8_t } func (v uint8_v) tÿpe() tÿpe { return uint8_t }
func (v *uint8_v) read(r bit.Reader) error { func (v *uint8_v) read(r bit.Reader) error {
u := bit.ReadVarInt(r) *v = uint8_v(r.ReadBits(8))
if u > 1<<8-1 {
return fmt.Errorf("uint8 overflow: %d", u)
}
*v = uint8_v(u)
return r.Err() return r.Err()
} }
@ -75,11 +71,7 @@ type uint16_v uint16
func (v uint16_v) tÿpe() tÿpe { return uint16_t } func (v uint16_v) tÿpe() tÿpe { return uint16_t }
func (v *uint16_v) read(r bit.Reader) error { func (v *uint16_v) read(r bit.Reader) error {
u := bit.ReadVarInt(r) *v = uint16_v(bit.ReadVarInt16(r))
if u > 1<<16-1 {
return fmt.Errorf("uint16 overflow: %d", u)
}
*v = uint16_v(u)
return r.Err() return r.Err()
} }

@ -2,6 +2,7 @@ package ent
import ( import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"os"
"strconv" "strconv"
"github.com/jordanorelli/hyperstone/bit" "github.com/jordanorelli/hyperstone/bit"
@ -178,7 +179,8 @@ func (e *Env) syncBaselineTable(t *stbl.Table) {
Debug.Printf("selections: %v", selections) Debug.Printf("selections: %v", selections)
for _, s := range selections { for _, s := range selections {
if err := s.fillSlots(ent, r); err != nil { if err := s.fillSlots(ent, r); err != nil {
Debug.Printf("unable to fill selection %s for %s: %v", s, className, err) Debug.Printf("syncBaseline fill error: %v", err)
os.Exit(1)
} }
} }
} }

@ -2,6 +2,7 @@ package ent
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/jordanorelli/hyperstone/bit" "github.com/jordanorelli/hyperstone/bit"
@ -80,7 +81,7 @@ func (t cutl_vector_t) typeName() string {
} }
type cutl_vector struct { type cutl_vector struct {
t tÿpe t *cutl_vector_t
slots []value slots []value
} }
@ -98,3 +99,14 @@ func (v cutl_vector) String() string {
} }
return fmt.Sprintf("%s(%d)%v", v.t.typeName(), len(v.slots), v.slots) return fmt.Sprintf("%s(%d)%v", v.t.typeName(), len(v.slots), v.slots)
} }
func (v *cutl_vector) slotType(int) tÿpe { return v.t.elem }
func (v *cutl_vector) slotName(n int) string { return strconv.Itoa(n) }
func (v *cutl_vector) setSlotValue(slot int, val value) {
if slot == len(v.slots) {
v.slots = append(v.slots, val)
} else {
v.slots[slot] = val
}
}
func (v *cutl_vector) getSlotValue(slot int) value { return v.slots[slot] }

@ -32,7 +32,7 @@ func (s selection) fillSlotsIter(offset int, dest slotted, path string, r bit.Re
case 1: case 1:
v := dest.slotType(slot).nü() v := dest.slotType(slot).nü()
if err := v.read(r); err != nil { if err := v.read(r); err != nil {
return fmt.Errorf("unable to fill selection: %v", err) return fmt.Errorf("unable to fill selection %v for %s.%s (%s): %v", s, path, dest.slotName(slot), dest.slotType(slot).typeName(), err)
} }
old := dest.getSlotValue(slot) old := dest.getSlotValue(slot)
dest.setSlotValue(slot, v) dest.setSlotValue(slot, v)

Loading…
Cancel
Save