go-interpreter / wagon

wagon, a WebAssembly-based Go interpreter, for Go.
BSD 3-Clause "New" or "Revised" License
902 stars 150 forks source link

Read leb128 fail when trying to read 64 bit of small number #175

Closed anhcao142 closed 4 years ago

anhcao142 commented 4 years ago
r := bytes.NewReader([]byte{0x08})
n, err := ReadUint64(r)
if err != nil {
  t.Fatal(err)
}
fmt.Println(n)
// output: leb128: invalid uint
twitchyliquid64 commented 4 years ago

@sbinet are you familiar with the leb128 encoding?

laizy commented 4 years ago

@anhcao142 where is the ReadUint64 defined? there are ReadVarint64 and ReadVarUint32 in package leb128 , and they are works.

anhcao142 commented 4 years ago

Sorry, my bad, I actually mean ReadVarUint64 and let me check again

anhcao142 commented 4 years ago

@laizy It's actually a mistake from my side, I'm sorry for troubling you. But before closing, can I ask why you don't have ReadVarUint64 method?

laizy commented 4 years ago

@anhcao142 ,maybe it is not used currently? but readVarUint(r io.Reader, n uint) is a general-purpose function for read uintN(like uint7, uint16), so you can define ReadVarUint64 as :

func ReadVarUint64(r io.Reader) (uint64, error) {
    n, err := readVarUint(r, 64)
    if err != nil {
        return 0, err
    }
    return uint64(n), nil
}