-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
In those two function, if we do this
if num < 0 {
panic("num < 0")
}
And if invoker use num=-1 to invoke those function,
it can not get any error message. So i think maybe
if num < 0 {
return errors.New("length less than zero")
}
is more reasonable.
Lines 139 to 178 in 836b571
| func (w *serializeBuffer) PrependBytes(num int) ([]byte, error) { | |
| if num < 0 { | |
| panic("num < 0") | |
| } | |
| if w.start < num { | |
| toPrepend := w.prepended | |
| if toPrepend < num { | |
| toPrepend = num | |
| } | |
| w.prepended += toPrepend | |
| length := cap(w.data) + toPrepend | |
| newData := make([]byte, length) | |
| newStart := w.start + toPrepend | |
| copy(newData[newStart:], w.data[w.start:]) | |
| w.start = newStart | |
| w.data = newData[:toPrepend+len(w.data)] | |
| } | |
| w.start -= num | |
| return w.data[w.start : w.start+num], nil | |
| } | |
| func (w *serializeBuffer) AppendBytes(num int) ([]byte, error) { | |
| if num < 0 { | |
| panic("num < 0") | |
| } | |
| initialLength := len(w.data) | |
| if cap(w.data)-initialLength < num { | |
| toAppend := w.appended | |
| if toAppend < num { | |
| toAppend = num | |
| } | |
| w.appended += toAppend | |
| newData := make([]byte, cap(w.data)+toAppend) | |
| copy(newData[w.start:], w.data[w.start:]) | |
| w.data = newData[:initialLength] | |
| } | |
| // Grow the buffer. We know it'll be under capacity given above. | |
| w.data = w.data[:initialLength+num] | |
| return w.data[initialLength:], nil | |
| } |
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels