Skip to content

CFArray: fix off-by-one in CFArrayGetLastIndexOfValue#46

Open
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cfarray-lastindex-off-by-one
Open

CFArray: fix off-by-one in CFArrayGetLastIndexOfValue#46
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cfarray-lastindex-off-by-one

Conversation

@DTW-Thalion

Copy link
Copy Markdown
Contributor

CFArrayGetLastIndexOfValue() began its backward scan at
range.location + range.length, i.e. one past the last index in the range
(the last valid index is range.location + range.length - 1). The first
comparison therefore read one element beyond the range; when the range
covers a full array whose count equals its capacity this is an
out-of-bounds read (AddressSanitizer: heap-buffer-overflow READ in
CFArrayGetValueAtIndex, from CFArrayGetLastIndexOfValue). The forward
counterpart, CFArrayGetFirstIndexOfValue(), already stops correctly at
range.location + range.length.

Start the scan at range.location + range.length - 1. An empty range now
also skips the loop instead of reading at range.location. Adds a
regression test.

CFArrayGetLastIndexOfValue() began its backward scan at
range.location + range.length, i.e. one past the last index in the range
(the last valid index is range.location + range.length - 1).  The first
comparison therefore read one element beyond the range; when the range
covers a full array whose count equals its capacity this is an
out-of-bounds read (AddressSanitizer: heap-buffer-overflow READ in
CFArrayGetValueAtIndex, from CFArrayGetLastIndexOfValue).  The forward
counterpart, CFArrayGetFirstIndexOfValue(), already stops correctly at
range.location + range.length.

Start the scan at range.location + range.length - 1.  An empty range now
also skips the loop instead of reading at range.location.  Adds a
regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant