diff --git a/algorithms/cpp/UTF8Validation/UTF8Validation.cpp b/algorithms/cpp/UTF8Validation/UTF8Validation.cpp index 619b8ab63..5b0fe7ac1 100644 --- a/algorithms/cpp/UTF8Validation/UTF8Validation.cpp +++ b/algorithms/cpp/UTF8Validation/UTF8Validation.cpp @@ -67,6 +67,10 @@ class Solution { return false; } + // invalid utf-8 as it doesn't have enough 10xxxxxx + if (i + len > data.size()) { + return false; + } for (int j=i+1; j < i+len; j++) { //checking 10xxxxxx if ( (data[j] & 0xC0) != 0x80 ) { @@ -75,11 +79,6 @@ class Solution { } i += len ; - - if (i > data.size()) { - return false; - } - } return true; }