Skip to content

Commit 51ea33c

Browse files
authored
Fix possible out of bound array access (#254)
Should check array size before accessing the second to fourth bytes of utf8 as there might be not enough data left in the array.
1 parent 36168cc commit 51ea33c

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

algorithms/cpp/UTF8Validation/UTF8Validation.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class Solution {
6767
return false;
6868
}
6969

70+
// invalid utf-8 as it doesn't have enough 10xxxxxx
71+
if (i + len > data.size()) {
72+
return false;
73+
}
7074

7175
for (int j=i+1; j < i+len; j++) { //checking 10xxxxxx
7276
if ( (data[j] & 0xC0) != 0x80 ) {
@@ -75,11 +79,6 @@ class Solution {
7579
}
7680

7781
i += len ;
78-
79-
if (i > data.size()) {
80-
return false;
81-
}
82-
8382
}
8483
return true;
8584
}

0 commit comments

Comments
 (0)