Skip to content

Commit cf93077

Browse files
authored
rlp: finalize listIterator on parse error to prevent non-advancing loops (#33245)
The list iterator previously returned true on parse errors without advancing the input, which could lead to non-advancing infinite loops for callers that do not check Err() inside the loop; to make iteration safe while preserving error visibility, Next() now marks the iterator as finished when readKind fails, returning true for the error step so existing users that check Err() can handle it, and then false on subsequent calls, and the function comment was updated to document this behavior and the need to check Err().
1 parent 62334a9 commit cf93077

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

rlp/iterator.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,24 @@ func NewListIterator(data RawValue) (*listIterator, error) {
3737
return it, nil
3838
}
3939

40-
// Next forwards the iterator one step, returns true if it was not at end yet
40+
// Next forwards the iterator one step.
41+
// Returns true if there is a next item or an error occurred on this step (check Err()).
42+
// On parse error, the iterator is marked finished and subsequent calls return false.
4143
func (it *listIterator) Next() bool {
4244
if len(it.data) == 0 {
4345
return false
4446
}
4547
_, t, c, err := readKind(it.data)
48+
if err != nil {
49+
it.next = nil
50+
it.err = err
51+
// Mark iteration as finished to avoid potential infinite loops on subsequent Next calls.
52+
it.data = nil
53+
return true
54+
}
4655
it.next = it.data[:t+c]
4756
it.data = it.data[t+c:]
48-
it.err = err
57+
it.err = nil
4958
return true
5059
}
5160

0 commit comments

Comments
 (0)