[Pcre-svn] [499] code/trunk: Add some checks for the eint ve…

Página Inicial
Delete this message
Autor: Subversion repository
Data:  
Para: pcre-svn
Assunto: [Pcre-svn] [499] code/trunk: Add some checks for the eint vector size and the list of compile-time error
Revision: 499
          http://vcs.pcre.org/viewvc?view=rev&revision=499
Author:   ph10
Date:     2010-03-03 20:09:39 +0000 (Wed, 03 Mar 2010)


Log Message:
-----------
Add some checks for the eint vector size and the list of compile-time error
texts.

Modified Paths:
--------------
    code/trunk/ChangeLog
    code/trunk/pcre_compile.c
    code/trunk/pcre_internal.h
    code/trunk/pcreposix.c


Modified: code/trunk/ChangeLog
===================================================================
--- code/trunk/ChangeLog    2010-03-03 19:29:38 UTC (rev 498)
+++ code/trunk/ChangeLog    2010-03-03 20:09:39 UTC (rev 499)
@@ -29,6 +29,15 @@
     in pcre_dfa_exec.c. This could lead to memory accesses outsize the vectors.
     I've fixed the data, and added a kludgy way of testing at compile time that 
     the lengths are correct (equal to the number of opcodes).  
+    
+8.  Following on from 7, I added a similar kludge to check the length of the 
+    eint vector in pcreposix.c. 
+    
+9.  Error texts for pcre_compile() are held as one long string to avoid too 
+    much relocation at load time. To find a text, the string is searched, 
+    counting zeros. There was no check for running off the end of the string,
+    which could happen if a new error number was added without updating the
+    string. 



Version 8.01 19-Jan-2010

Modified: code/trunk/pcre_compile.c
===================================================================
--- code/trunk/pcre_compile.c    2010-03-03 19:29:38 UTC (rev 498)
+++ code/trunk/pcre_compile.c    2010-03-03 20:09:39 UTC (rev 499)
@@ -263,8 +263,12 @@
 it is now one long string. We cannot use a table of offsets, because the
 lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
 simply count through to the one we want - this isn't a performance issue
-because these strings are used only when there is a compilation error. */
+because these strings are used only when there is a compilation error. 


+Each substring ends with \0 to insert a null character. This includes the final
+substring, so that the whole string ends with \0\0, which can be detected when
+counting through. */
+
static const char error_texts[] =
"no error\0"
"\\ at end of pattern\0"
@@ -344,9 +348,8 @@
"digit expected after (?+\0"
"] is an invalid data character in JavaScript compatibility mode\0"
/* 65 */
- "different names for subpatterns of the same number are not allowed";
+ "different names for subpatterns of the same number are not allowed\0";

-
/* Table to identify digits and hex digits. This is used when compiling
patterns. Note that the tables in chartables are dependent on the locale, and
may mark arbitrary characters as digits - but the PCRE compiling code expects
@@ -503,7 +506,11 @@
find_error_text(int n)
{
const char *s = error_texts;
-for (; n > 0; n--) while (*s++ != 0) {};
+for (; n > 0; n--)
+ {
+ while (*s++ != 0) {};
+ if (*s == 0) return "Error text not found (please report)";
+ }
return s;
}


Modified: code/trunk/pcre_internal.h
===================================================================
--- code/trunk/pcre_internal.h    2010-03-03 19:29:38 UTC (rev 498)
+++ code/trunk/pcre_internal.h    2010-03-03 20:09:39 UTC (rev 499)
@@ -1502,8 +1502,9 @@


#define RREF_ANY 0xffff

-/* Error code numbers. They are given names so that they can more easily be
-tracked. */
+/* Compile time error code numbers. They are given names so that they can more
+easily be tracked. When a new number is added, the table called eint in
+pcreposix.c must be updated. */

 enum { ERR0,  ERR1,  ERR2,  ERR3,  ERR4,  ERR5,  ERR6,  ERR7,  ERR8,  ERR9,
        ERR10, ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19,
@@ -1511,7 +1512,7 @@
        ERR30, ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39,
        ERR40, ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47, ERR48, ERR49,
        ERR50, ERR51, ERR52, ERR53, ERR54, ERR55, ERR56, ERR57, ERR58, ERR59,
-       ERR60, ERR61, ERR62, ERR63, ERR64, ERR65 };
+       ERR60, ERR61, ERR62, ERR63, ERR64, ERR65, ERRCOUNT };


/* The real format of the start of the pcre block; the index of names and the
code vector run on as long as necessary after the end. We store an explicit

Modified: code/trunk/pcreposix.c
===================================================================
--- code/trunk/pcreposix.c    2010-03-03 19:29:38 UTC (rev 498)
+++ code/trunk/pcreposix.c    2010-03-03 20:09:39 UTC (rev 499)
@@ -344,6 +344,8 @@


 if (rc == 0) rc = nmatch;    /* All captured slots were filled in */


+/* Successful match */
+
if (rc >= 0)
{
size_t i;
@@ -360,22 +362,33 @@
return 0;
}

-else
+/* Unsuccessful match */
+
+if (allocated_ovector) free(ovector);
+switch(rc)
   {
-  if (allocated_ovector) free(ovector);
-  switch(rc)
-    {
-    case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
-    case PCRE_ERROR_NULL: return REG_INVARG;
-    case PCRE_ERROR_BADOPTION: return REG_INVARG;
-    case PCRE_ERROR_BADMAGIC: return REG_INVARG;
-    case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
-    case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
-    case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE;
-    case PCRE_ERROR_BADUTF8: return REG_INVARG;
-    case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG;
-    default: return REG_ASSERT;
-    }
+/* ========================================================================== */
+  /* These cases are never obeyed. This is a fudge that causes a compile-time
+  error if the vector eint, which is indexed by compile-time error number, is
+  not the correct length. It seems to be the only way to do such a check at
+  compile time, as the sizeof() operator does not work in the C preprocessor.
+  As all the PCRE_ERROR_xxx values are negative, we can use 0 and 1. */ 
+
+  case 0:
+  case (sizeof(eint)/sizeof(int) == ERRCOUNT):
+  return REG_ASSERT;
+/* ========================================================================== */
+
+  case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
+  case PCRE_ERROR_NULL: return REG_INVARG;
+  case PCRE_ERROR_BADOPTION: return REG_INVARG;
+  case PCRE_ERROR_BADMAGIC: return REG_INVARG;
+  case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
+  case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
+  case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE;
+  case PCRE_ERROR_BADUTF8: return REG_INVARG;
+  case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG;
+  default: return REG_ASSERT;
   }
 }