dedis/protobuf

Print error message instead of index-out-of-range panic (was: Should verify input-buffer)

Closed this issue · 3 comments

When the input-buffer is larger than the data, it will try to decode anyway, but fail on printing an error, as the index is out of range in

decode.go:107

if err != nil {
    return fmt.Errorf("FieldName %s: %v", fields[fieldi].Name, err)
}

I would change this to if err != nil && fieldi < len(fields) and add a case if len(fields) >= fieldi to print the other error.

Can you do this, please? Sounds like a good solution.

Can you do this, please? Sounds like a good solution.

Yes of course. I looked a little through the code:
The 2nd if statement: if len(fields) >= fieldi would always throw an error at the end because the way fieldi is computed here: https://github.com/dedis/protobuf/blob/master/decode.go#L88-L90
Wouldn't it make more sense to move
https://github.com/dedis/protobuf/blob/master/decode.go#L104-L108
into the if statement above it: https://github.com/dedis/protobuf/blob/master/decode.go#L92-L102
This would have the same effect. If the buffer is "too long" and/or contains some junk, calling value(...) would either return an error or panic.