Skip to content

Commit a225404

Browse files
author
livecodealex
committed
[[ ByteOffset ]] Fixed bug causing incorrect results
1 parent d632901 commit a225404

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

docs/notes/bugfix-byteoffset.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fixed bug causing byteOffset to behave incorectly in certain cases

libfoundation/src/foundation-data.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,11 +798,18 @@ bool MCDataFirstIndexOf(MCDataRef p_data, MCDataRef p_chunk, MCRange p_range, ui
798798
if (p_range.length == 0)
799799
return false;
800800

801-
uindex_t t_limit, t_chunk_byte_count;
801+
uindex_t t_limit, t_chunk_byte_count, t_data_byte_count;
802802
t_chunk_byte_count = MCDataGetLength(p_chunk);
803+
t_data_byte_count = MCDataGetLength(p_data);
804+
803805
if (t_chunk_byte_count == 0)
804806
return false;
805-
t_limit = p_range . offset + p_range . length - t_chunk_byte_count + 1;
807+
else if (t_data_byte_count < t_chunk_byte_count)
808+
return false;
809+
else if (p_range.length < t_chunk_byte_count)
810+
return false;
811+
812+
t_limit = p_range . offset + t_data_byte_count- t_chunk_byte_count + 1;
806813

807814
const byte_t *t_bytes = MCDataGetBytePtr(p_data);
808815
const byte_t *t_chunk_bytes = MCDataGetBytePtr(p_chunk);

0 commit comments

Comments
 (0)