]> granicus.if.org Git - php/commitdiff
Avoid strlen calls for regexes
authorAnatol Belski <ab@php.net>
Thu, 19 Apr 2018 11:28:24 +0000 (13:28 +0200)
committerAnatol Belski <ab@php.net>
Thu, 19 Apr 2018 11:29:58 +0000 (13:29 +0200)
ext/pgsql/pgsql.c

index 5f1960dd582c3baa6d13094410b4cd9f72b8731b..dccdfc2e9a8fa26717f7c94668f438a2c65eea8b 100644 (file)
@@ -5737,7 +5737,7 @@ static php_pgsql_data_type php_pgsql_get_data_type(const char *type_name, size_t
 /* {{{ php_pgsql_convert_match
  * test field value with regular expression specified.
  */
-static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , int icase)
+static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , size_t regex_len, int icase)
 {
        pcre2_code *re;
        PCRE2_SIZE           err_offset;
@@ -5759,7 +5759,7 @@ static int php_pgsql_convert_match(const char *str, size_t str_len, const char *
                options |= PCRE2_CASELESS;
        }
 
-       re = pcre2_compile((PCRE2_SPTR)regex, PCRE2_ZERO_TERMINATED, options, &errnumber, &err_offset, php_pcre_cctx());
+       re = pcre2_compile((PCRE2_SPTR)regex, regex_len, options, &errnumber, &err_offset, php_pcre_cctx());
        if (NULL == re) {
                PCRE2_UCHAR err_msg[256];
                pcre2_get_error_message(errnumber, err_msg, sizeof(err_msg));
@@ -5969,12 +5969,14 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                }
                                                else {
                                                        /* FIXME: better regex must be used */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([+-]{0,1}[0-9]+)$", 0) == FAILURE) {
+#define REGEX0 "^([+-]{0,1}[0-9]+)$"
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
                                                                ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
                                                        }
+#undef REGEX0
                                                }
                                                break;
 
@@ -6010,9 +6012,11 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                        ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
                                                }
                                                else {
+#define REGEX0 "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$"
+#define REGEX1 "^[+-]{0,1}(inf)(inity){0,1}$"
                                                        /* better regex? */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$", 0) == FAILURE) {
-                                                               if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[+-]{0,1}(inf)(inity){0,1}$", 1) == FAILURE) {
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
+                                                               if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
                                                                        err = 1;
                                                                } else {
                                                                        ZVAL_STRING(&new_val, Z_STRVAL_P(val));
@@ -6022,6 +6026,8 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                        else {
                                                                ZVAL_STRING(&new_val, Z_STRVAL_P(val));
                                                        }
+#undef REGEX0
+#undef REGEX1
                                                }
                                                break;
 
@@ -6119,7 +6125,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                }
                                                else {
                                                        /* better regex? */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[0-9]+$", 0) == FAILURE) {
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[0-9]+$", sizeof("^[0-9]+$")-1, 0) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
@@ -6159,17 +6165,21 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                        ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
                                                }
                                                else {
+#define REGEX0 "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$"
+#define REGEX1 "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$"
                                                        /* The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. See more in the doc.
                                                                The regex might still be not perfect, but catches the most of IP variants. We might decide to remove the regex
                                                                at all though and let the server side to handle it.*/
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$", 0) == FAILURE
-                                                               && php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$", 0) == FAILURE) {
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE
+                                                               && php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX1, sizeof(REGEX1)-1, 0) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
                                                                ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
                                                                php_pgsql_add_quotes(&new_val, 1);
                                                        }
+#undef REGEX0
+#undef REGEX1
                                                }
                                                break;
 
@@ -6196,13 +6206,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                } else if (!strcasecmp(Z_STRVAL_P(val), "now()")) {
                                                        ZVAL_STRINGL(&new_val, "NOW()", sizeof("NOW()")-1);
                                                } else {
+#define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$"
                                                        /* better regex? */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$", 1) == FAILURE) {
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
                                                                err = 1;
                                                        } else {
                                                                ZVAL_STRING(&new_val, Z_STRVAL_P(val));
                                                                php_pgsql_add_quotes(&new_val, 1);
                                                        }
+#undef REGEX0
                                                }
                                                break;
 
@@ -6226,14 +6238,16 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                        ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
                                                }
                                                else {
+#define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$"
                                                        /* FIXME: better regex must be used */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1) == FAILURE) {
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
                                                                ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
                                                                php_pgsql_add_quotes(&new_val, 1);
                                                        }
+#undef REGEX0
                                                }
                                                break;
 
@@ -6257,14 +6271,16 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                        ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
                                                }
                                                else {
+#define REGEX0 "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$"
                                                        /* FIXME: better regex must be used */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$", 1) == FAILURE) {
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
                                                                ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
                                                                php_pgsql_add_quotes(&new_val, 1);
                                                        }
+#undef REGEX0
                                                }
                                                break;
 
@@ -6305,45 +6321,45 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                           unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10
                                                           sec'.
                                                        */
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val),
-                                                                                                               "^(@?[ \\t]+)?("
-
-                                                                                                               /* Textual time units and their abbreviations: */
-                                                                                                               "(([-+]?[ \\t]+)?"
-                                                                                                               "[0-9]+(\\.[0-9]*)?[ \\t]*"
-                                                                                                               "(millenniums|millennia|millennium|mil|mils|"
-                                                                                                               "centuries|century|cent|c|"
-                                                                                                               "decades|decade|dec|decs|"
-                                                                                                               "years|year|y|"
-                                                                                                               "months|month|mon|"
-                                                                                                               "weeks|week|w|"
-                                                                                                               "days|day|d|"
-                                                                                                               "hours|hour|hr|hrs|h|"
-                                                                                                               "minutes|minute|mins|min|m|"
-                                                                                                               "seconds|second|secs|sec|s))+|"
-
-                                                                                                               /* Textual time units plus (dd)* hh[:mm[:ss]] */
-                                                                                                               "((([-+]?[ \\t]+)?"
-                                                                                                               "[0-9]+(\\.[0-9]*)?[ \\t]*"
-                                                                                                               "(millenniums|millennia|millennium|mil|mils|"
-                                                                                                               "centuries|century|cent|c|"
-                                                                                                               "decades|decade|dec|decs|"
-                                                                                                               "years|year|y|"
-                                                                                                               "months|month|mon|"
-                                                                                                               "weeks|week|w|"
-                                                                                                               "days|day|d))+"
-                                                                                                               "([-+]?[ \\t]+"
-                                                                                                               "([0-9]+[ \\t]+)+"                               /* dd */
-                                                                                                               "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */
-                                                                                                               ")?))"
-                                                                                                               "([ \\t]+ago)?$",
-                                                                                                               1) == FAILURE) {
+#define REGEX0 \
+       "^(@?[ \\t]+)?(" \
+       /* Textual time units and their abbreviations: */ \
+       "(([-+]?[ \\t]+)?" \
+       "[0-9]+(\\.[0-9]*)?[ \\t]*" \
+       "(millenniums|millennia|millennium|mil|mils|" \
+       "centuries|century|cent|c|" \
+       "decades|decade|dec|decs|" \
+       "years|year|y|" \
+       "months|month|mon|" \
+       "weeks|week|w|" \
+       "days|day|d|" \
+       "hours|hour|hr|hrs|h|" \
+       "minutes|minute|mins|min|m|" \
+       "seconds|second|secs|sec|s))+|" \
+       /* Textual time units plus (dd)* hh[:mm[:ss]] */ \
+       "((([-+]?[ \\t]+)?" \
+       "[0-9]+(\\.[0-9]*)?[ \\t]*" \
+       "(millenniums|millennia|millennium|mil|mils|" \
+       "centuries|century|cent|c|" \
+       "decades|decade|dec|decs|" \
+       "years|year|y|" \
+       "months|month|mon|" \
+       "weeks|week|w|" \
+       "days|day|d))+" \
+       "([-+]?[ \\t]+" \
+       "([0-9]+[ \\t]+)+"                               /* dd */ \
+       "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */ \
+       ")?))" \
+       "([ \\t]+ago)?$"
+
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
                                                                ZVAL_STRING(&new_val, Z_STRVAL_P(val));
                                                                php_pgsql_add_quotes(&new_val, 1);
                                                        }
+#undef REGEX0
                                                }
                                                break;
 
@@ -6416,13 +6432,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
                                                        ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
                                                }
                                                else {
-                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$", 1) == FAILURE) {
+#define REGEX0 "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$"
+                                                       if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
                                                                err = 1;
                                                        }
                                                        else {
                                                                ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
                                                                php_pgsql_add_quotes(&new_val, 1);
                                                        }
+#undef REGEX0
                                                }
                                                break;