Jelajahi Sumber

Fix error detection when calling pcre[2]_match

mingodad 8 tahun lalu
induk
melakukan
a2d31d0905
3 mengubah file dengan 52 tambahan dan 9 penghapusan
  1. 24 4
      SquiLu-ext/sq_pcre.cpp
  2. 24 4
      SquiLu-ext/sq_pcre2.cpp
  3. 4 1
      SquiLu/samples/test-pcre.nut

+ 24 - 4
SquiLu-ext/sq_pcre.cpp

@@ -273,8 +273,11 @@ static SQRESULT sq_pcre_match(HSQUIRRELVM v)
         else sq_pushstring(v, subject + start_pos, end_pos - start_pos);
         return 1;
     }
-    sq_pushbool(v,SQFalse);
-    return 1;
+    if(rc < -2) //only no matching errore
+    {
+        return sq_throwerror(v, _SC("pcre2_match error %d"), (int)rc);
+    }
+    return 0;
 }
 
 static SQRESULT sq_pcre_gmatch(HSQUIRRELVM v)
@@ -323,10 +326,22 @@ static SQRESULT sq_pcre_gmatch(HSQUIRRELVM v)
         }
         i = sq_call(v, rc+1, SQFalse, SQTrue);
         if(i < 0) return i;
+
+        SQObjectType rtype = sq_gettype(v, -1);
+        SQBool keep_matching = SQFalse;
+        if(rtype == OT_BOOL) {
+            sq_getbool(v, -1, &keep_matching);
+        }
+
+        if(!keep_matching) break;
+
         start_offset = self->ovector[(rc*2)-1]; //the last match + 1
     }
-    sq_pushbool(v,SQFalse);
-    return 1;
+    if(rc < -2) //only no matching errore
+    {
+        return sq_throwerror(v, _SC("pcre2_match error %d"), (int)rc);
+    }
+    return 0;
 }
 
 #include "sqstdblobimpl.h"
@@ -478,6 +493,11 @@ static SQRESULT sq_pcre_gsub(HSQUIRRELVM v)
 		start_offset = self->ovector[(rc*2)-1]; //the last match + 1
 	}
 
+    if(rc < -2) //only no matching errore
+    {
+        return sq_throwerror(v, _SC("pcre_match error %d"), (int)rc);
+    }
+
     if(str_size) blob.Write(str+start_offset, str_size-start_offset);
 	sq_pushstring(v, (const SQChar *)blob.GetBuf(), blob.Len());
 	return 1;

+ 24 - 4
SquiLu-ext/sq_pcre2.cpp

@@ -33,6 +33,7 @@ local library_functions = [
     ["int", "pcre2_set_recursion_limit", "pcre2_match_context *mcontext, uint32_t value"],
     ["int", "pcre2_set_callout", "pcre2_match_context *, int (*)(pcre2_callout_block *, void *), void *"],
     ["int", "pcre2_substitute", "const pcre2_code *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_SIZE, uint32_t, pcre2_match_data *, pcre2_match_context *, PCRE2_SPTR, PCRE2_SIZE, PCRE2_UCHAR *, PCRE2_SIZE *"],
+    //["int", "pcre2_get_error_message", "int errorcode, PCRE2_UCHAR *buffer, PCRE2_SIZE bufflen"],
 
     //next entry should be the last one
     //to make valid the test made on load_library function
@@ -355,8 +356,11 @@ static SQRESULT sq_pcre2_match(HSQUIRRELVM v)
         else sq_pushstring(v, subject + start_pos, end_pos - start_pos);
         return 1;
     }
-    sq_pushbool(v,SQFalse);
-    return 1;
+    if(rc < -2) //only no matching errore
+    {
+        return sq_throwerror(v, _SC("pcre2_match error %d"), (int)rc);
+    }
+    return 0;
 }
 
 static SQRESULT sq_pcre2_gmatch(HSQUIRRELVM v)
@@ -404,10 +408,21 @@ static SQRESULT sq_pcre2_gmatch(HSQUIRRELVM v)
         }
         i = sq_call(v, param_count, SQFalse, SQTrue);
         if(i < 0) return i;
+        SQObjectType rtype = sq_gettype(v, -1);
+        SQBool keep_matching = SQFalse;
+        if(rtype == OT_BOOL) {
+            sq_getbool(v, -1, &keep_matching);
+        }
+
+        if(!keep_matching) break;
+
         start_offset = self->ovector[(rc*2)-1]; //the last match + 1
     }
-    sq_pushbool(v,SQFalse);
-    return 1;
+    if(rc < -2) //only no matching errore
+    {
+        return sq_throwerror(v, _SC("pcre2_match error %d"), (int)rc);
+    }
+    return 0;
 }
 
 #include "sqstdblobimpl.h"
@@ -558,6 +573,11 @@ static SQRESULT sq_pcre2_gsub(HSQUIRRELVM v)
 		start_offset = self->ovector[(rc*2)-1]; //the last match + 1
 	}
 
+    if(rc < -2) //only no matching errore
+    {
+        return sq_throwerror(v, _SC("pcre2_match error %d"), (int)rc);
+    }
+
     if(str_size) blob.Write(str+start_offset, str_size-start_offset);
 	sq_pushstring(v, (const SQChar *)blob.GetBuf(), blob.Len());
 	return 1;

+ 4 - 1
SquiLu/samples/test-pcre.nut

@@ -1,11 +1,14 @@
 //auto str = " this code 10.89.973.8.3.00.34-8 is special 23.456-2 car";
 auto str = " this code 10.89.973.8.3.00.34/8 is special 23.456/2 car";
 
+print("loadlib", sqpcre.loadlib("/libs/pcre-8.38/.libs/libpcre.so.1.2.6"));
+//print("loadlib", sqpcre.loadlib("/libs/pcre2-10.20/.libs/libpcre2-8.so"));
+
 //auto pcre = sqpcre(@"(\d+[.,\-])+\d+");
 //auto pcre = sqpcre(@"(?:\d+[.,\-/])+\d+");
 auto pcre = sqpcre(@"(?:\d+[.,\-/])+\d+");
 pcre.study(pcre.STUDY_JIT_COMPILE);
-print(pcre);
+print(pcre, typeof(pcre));
 print(pcre.version());
 
 print("match", pcre.match(str));