diff --git a/compiler2/PredefFunc.cc b/compiler2/PredefFunc.cc
index 04230343f5b3709c26e6962a81152bce9654e86d..07a2dc7e8d5615f330fa9f1f28af4975b8217c83 100644
--- a/compiler2/PredefFunc.cc
+++ b/compiler2/PredefFunc.cc
@@ -818,7 +818,7 @@ static CharCoding::CharCodingType is_utf8(size_t length, const unsigned char* st
       // the second and third (and so on) UTF-8 byte looks like 10xx xxxx      
       while (0 < noofUTF8 ) {
         ++i;
-        if (!(strptr[i] & MSB) || (strptr[i] & MSBmin1) || i >= length) { // if not like this: 10xx xxxx
+        if (i >= length || !(strptr[i] & MSB) || (strptr[i] & MSBmin1)) { // if not like this: 10xx xxxx
           return CharCoding::UNKNOWN;
         }
         --noofUTF8;
diff --git a/core/Addfunc.cc b/core/Addfunc.cc
index c41f59b49117189ffa25488ad1979d42d49a0ee7..651cfeed2ab63d0321d0b45509f2d70015ea6e92 100644
--- a/core/Addfunc.cc
+++ b/core/Addfunc.cc
@@ -239,7 +239,7 @@ static CharCoding::CharCodingType is_utf8 ( const OCTETSTRING& ostr )
       while (0 < noofUTF8 ) {
         ++i;
   //std::cout << "mask & strptr[" << i << "] " << std::hex << (int)strptr[i]  << std::endl;
-        if (!(strptr[i] & MSB) || (strptr[i] & MSBmin1) || i >= ostr.lengthof()) { // if not like this: 10xx xxxx
+        if (i >= ostr.lengthof() || !(strptr[i] & MSB) || (strptr[i] & MSBmin1)) { // if not like this: 10xx xxxx
           return CharCoding::UNKNOWN;
         }
         --noofUTF8;