This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Allow search to work on empty inversion lists
authorKarl Williamson <public@khwilliamson.com>
Sat, 18 Aug 2012 20:23:12 +0000 (14:23 -0600)
committerKarl Williamson <public@khwilliamson.com>
Sun, 26 Aug 2012 05:21:27 +0000 (23:21 -0600)
You cannot retrieve the array of an empty inversion list, so the code
has to be reordered to do that after the list is known to be non-empty.
I haven't been able to find a case where this currently fails, but
future commits open up the possibility.

regcomp.c

index eb7655e..263b7b4 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -7323,16 +7323,24 @@ Perl__invlist_search(pTHX_ SV* const invlist, const UV cp)
 
     IV low = 0;
     IV high = invlist_len(invlist);
-    const UV * const array = invlist_array(invlist);
+    const IV highest_element = high - 1;
+    const UV* array;
 
     PERL_ARGS_ASSERT__INVLIST_SEARCH;
 
-    /* If list is empty or the code point is before the first element, return
-     * failure. */
-    if (high == 0 || cp < array[0]) {
+    /* If list is empty, return failure. */
+    if (high == 0) {
        return -1;
     }
 
+    /* If the code point is before the first element, return failure.  (We
+     * can't combine this with the test above, because we can't get the array
+     * unless we know the list is non-empty) */
+    array = invlist_array(invlist);
+    if (cp < array[0]) {
+        return -1;
+    }
+
     /* Binary search.  What we are looking for is <i> such that
      * array[i] <= cp < array[i+1]
      * The loop below converges on the i+1. */