]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/RegularExpressionDxe/Oniguruma/regexec.c
MdeModulePkg/RegularExpressionDxe: Add null pointer check
[mirror_edk2.git] / MdeModulePkg / Universal / RegularExpressionDxe / Oniguruma / regexec.c
index 26e7a3176cb4118132bfcadb3da504bb50c5b8a7..6a55045d64f5884674fff526f409c779c907962b 100644 (file)
@@ -4088,6 +4088,11 @@ slow_search_backward(OnigEncoding enc, UChar* target, UChar* target_end,
     s = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, adjust_text, s);\r
 \r
   while (s >= text) {\r
     s = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, adjust_text, s);\r
 \r
   while (s >= text) {\r
+    //if text is not null,the logic is correct.\r
+    //this function is only invoked by backward_search_range,parameter text come\r
+    //from range, which is checked by "if (range == 0) goto fail" in line 4512\r
+    //so the check is just for passing static analysis.\r
+    if(IS_NULL(s))break;\r
     if (*s == *target) {\r
       p = s + 1;\r
       t = target + 1;\r
     if (*s == *target) {\r
       p = s + 1;\r
       t = target + 1;\r
@@ -4298,6 +4303,11 @@ map_search_backward(OnigEncoding enc, UChar map[],
   const UChar *s = text_start;\r
 \r
   while (s >= text) {\r
   const UChar *s = text_start;\r
 \r
   while (s >= text) {\r
+    //if text is not null,the logic is correct.\r
+    //this function is only invoked by backward_search_range,parameter text come\r
+    //from range, which is checked by "if (range == 0) goto fail" in line 4512\r
+    //so the check is just for passing static analysis.\r
+    if(IS_NULL(s))break;\r
     if (map[*s]) return (UChar* )s;\r
 \r
     s = onigenc_get_prev_char_head(enc, adjust_text, s);\r
     if (map[*s]) return (UChar* )s;\r
 \r
     s = onigenc_get_prev_char_head(enc, adjust_text, s);\r
@@ -4499,7 +4509,7 @@ backward_search_range(regex_t* reg, const UChar* str, const UChar* end,
                       UChar** low, UChar** high)\r
 {\r
   UChar *p;\r
                       UChar** low, UChar** high)\r
 {\r
   UChar *p;\r
-\r
+  if (range == 0) goto fail;\r
   range += reg->dmin;\r
   p = s;\r
 \r
   range += reg->dmin;\r
   p = s;\r
 \r
@@ -4550,7 +4560,7 @@ backward_search_range(regex_t* reg, const UChar* str, const UChar* end,
       case ANCHOR_BEGIN_LINE:\r
         if (!ON_STR_BEGIN(p)) {\r
           prev = onigenc_get_prev_char_head(reg->enc, str, p);\r
       case ANCHOR_BEGIN_LINE:\r
         if (!ON_STR_BEGIN(p)) {\r
           prev = onigenc_get_prev_char_head(reg->enc, str, p);\r
-          if (!ONIGENC_IS_MBC_NEWLINE(reg->enc, prev, end)) {\r
+          if (IS_NOT_NULL(prev) && !ONIGENC_IS_MBC_NEWLINE(reg->enc, prev, end)) {\r
             p = prev;\r
             goto retry;\r
           }\r
             p = prev;\r
             goto retry;\r
           }\r
@@ -4739,10 +4749,15 @@ onig_search_with_param(regex_t* reg, const UChar* str, const UChar* end,
       }\r
     }\r
     else if (reg->anchor & ANCHOR_SEMI_END_BUF) {\r
       }\r
     }\r
     else if (reg->anchor & ANCHOR_SEMI_END_BUF) {\r
+\r
       UChar* pre_end = ONIGENC_STEP_BACK(reg->enc, str, end, 1);\r
 \r
       max_semi_end = (UChar* )end;\r
       UChar* pre_end = ONIGENC_STEP_BACK(reg->enc, str, end, 1);\r
 \r
       max_semi_end = (UChar* )end;\r
-      if (ONIGENC_IS_MBC_NEWLINE(reg->enc, pre_end, end)) {\r
+      // only when str > end, pre_end will be null\r
+      // line 4659 "if (start > end || start < str) goto mismatch_no_msa"\r
+      // will guarantee str alwayls less than end\r
+      // so pre_end won't be null,this check is just for passing staic analysis\r
+      if (IS_NOT_NULL(pre_end) && ONIGENC_IS_MBC_NEWLINE(reg->enc, pre_end, end)) {\r
         min_semi_end = pre_end;\r
 \r
 #ifdef USE_CRNL_AS_LINE_TERMINATOR\r
         min_semi_end = pre_end;\r
 \r
 #ifdef USE_CRNL_AS_LINE_TERMINATOR\r
@@ -4891,6 +4906,16 @@ onig_search_with_param(regex_t* reg, const UChar* str, const UChar* end,
             MATCH_AND_RETURN_CHECK(orig_start);\r
             s = prev;\r
           }\r
             MATCH_AND_RETURN_CHECK(orig_start);\r
             s = prev;\r
           }\r
+          // if range is not null,the check is not necessary.\r
+          // the range is actually the pointer of the end of the matched string\r
+          // or assigned by "range = str" in line 4708. In RegularExpressionMatch\r
+          // protocol, the matched string is the parameter String. And str in\r
+          // line 4708 is the String,too. and the range is calculated from\r
+          // "Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start)" in\r
+          // line 146 in RegularExpressionDxe.c. RegularExpressionMatch ensure\r
+          // the String is not null,So in both situation, the range can not be NULL.\r
+          // This check is just for passing static analysis.\r
+          if(IS_NULL(s))break;\r
         } while (s >= range);\r
         goto mismatch;\r
       }\r
         } while (s >= range);\r
         goto mismatch;\r
       }\r