]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/RegularExpressionDxe: Add null pointer check
authorDongao Guo <dongao.guo@intel.com>
Thu, 11 Oct 2018 06:57:03 +0000 (14:57 +0800)
committerLiming Gao <liming.gao@intel.com>
Mon, 15 Oct 2018 07:55:53 +0000 (15:55 +0800)
There are five check not necessary in logic ,just for pass static
analysis. More detail please refer to comment in code.
And the rest changes are all accepted by owner, they are reasonable.

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Dongao Guo <dongao.guo@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdeModulePkg/Universal/RegularExpressionDxe/Oniguruma/regcomp.c
MdeModulePkg/Universal/RegularExpressionDxe/Oniguruma/regexec.c

index cc061780c7ea659bf03600dd45bc2acb97d35c93..f5db3ea6e8c92eb2728a89ea95787c895ba2a4b2 100644 (file)
@@ -3546,8 +3546,12 @@ expand_case_fold_string_alt(int item_num, OnigCaseFoldCodeItem items[], UChar *p
     if (IS_NULL(an)) {\r
       goto mem_err2;\r
     }\r
-\r
-    if (items[i].byte_len != slen) {\r
+    //The NULL pointer check is not necessary. It is added just for pass static\r
+    //analysis. When condition "items[i].byte_len != slen" is true, "varlen = 1"\r
+    //in line 3503 will be reached ,so that "if (IS_NULL(var_anode)) return ONIGERR_MEMORY"\r
+    //in line 3510 will be executed, so the null pointer has been checked before\r
+    //deferenced in line 3584.\r
+    if (items[i].byte_len != slen && IS_NOT_NULL(var_anode)) {\r
       Node *rem;\r
       UChar *q = p + items[i].byte_len;\r
 \r
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
+    //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
@@ -4298,6 +4303,11 @@ map_search_backward(OnigEncoding enc, UChar map[],
   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
@@ -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
-\r
+  if (range == 0) goto fail;\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
-          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
@@ -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
       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
@@ -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
+          // 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