cvs: php4 /ext/standard reg.c
| From: | Hartmut Holzgraefe | Date: | Tue, 11 Dec 2001 19:28:31 +0000 |
| Subject: | cvs: php4 /ext/standard reg.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-8580@lists.php.net to get a copy of this message | ||
hholzgra Tue Dec 11 14:28:31 2001 EDT
Modified files:
/php4/ext/standard reg.c
Log:
ereg functions will now take in account the real number of
subexpressions instead of simply assuming a fixed count of 10
Index: php4/ext/standard/reg.c
diff -u php4/ext/standard/reg.c:1.53 php4/ext/standard/reg.c:1.54
--- php4/ext/standard/reg.c:1.53 Tue Dec 11 10:30:36 2001
+++ php4/ext/standard/reg.c Tue Dec 11 14:28:31 2001
@@ -17,7 +17,7 @@
| Jaakko Hyvätti <jaakko@hyvatti.iki.fi> |
+----------------------------------------------------------------------+
*/
-/* $Id: reg.c,v 1.53 2001/12/11 15:30:36 sebastian Exp $ */
+/* $Id: reg.c,v 1.54 2001/12/11 19:28:31 hholzgra Exp $ */
#include <stdio.h>
#include "php.h"
@@ -106,10 +106,6 @@
}
-/* This is the maximum number of (..) constructs we'll generate from a
- call to ereg() or eregi() with the optional third argument. */
-#define NS 10
-
/* {{{ php_reg_eprint
* php_reg_eprint - convert error number to name
*/
@@ -159,7 +155,7 @@
**findin, /* String to apply expression to */
**array = NULL; /* Optional register array */
regex_t re;
- regmatch_t subs[NS];
+ regmatch_t *subs;
int err, i, match_len, string_len;
int copts = 0;
off_t start, end;
@@ -199,11 +195,19 @@
convert_to_string_ex(findin);
string = estrndup(Z_STRVAL_PP(findin), Z_STRLEN_PP(findin));
+ /* allocate storage for (sub-)expression-matches */
+ subs = (regmatch_t *)ecalloc(sizeof(regmatch_t),re.re_nsub+1);
+ if (!subs) {
+ php_error(E_WARNING, "Unable to allocate memory in php_ereg");
+ RETURN_FALSE;
+ }
+
/* actually execute the regular expression */
- err = regexec(&re, string, (size_t) NS, subs, 0);
+ err = regexec(&re, string, re.re_nsub+1, subs, 0);
if (err && err != REG_NOMATCH) {
php_reg_eprint(err, &re);
regfree(&re);
+ efree(subs);
RETURN_FALSE;
}
match_len = 1;
@@ -215,13 +219,14 @@
buf = emalloc(string_len);
if (!buf) {
php_error(E_WARNING, "Unable to allocate memory in php_ereg");
+ efree(subs);
RETURN_FALSE;
}
zval_dtor(*array); /* start with clean array */
array_init(*array);
- for (i = 0; i < NS; i++) {
+ for (i = 0; i <= re.re_nsub; i++) {
start = subs[i].rm_so;
end = subs[i].rm_eo;
if (start != -1 && end > 0 && start < string_len && end <
string_len && start < end) {
@@ -230,6 +235,7 @@
add_index_bool(*array, i, 0);
}
}
+ efree(subs);
efree(buf);
}
@@ -266,7 +272,7 @@
PHPAPI char *php_reg_replace(const char *pattern, const char *replace, const char *string, int
icase, int extended)
{
regex_t re;
- regmatch_t subs[NS];
+ regmatch_t *subs;
char *buf, /* buf is where we build the replaced string */
*nbuf, /* nbuf is used when we grow the buffer */
@@ -289,12 +295,21 @@
return ((char *) -1);
}
+
+ /* allocate storage for (sub-)expression-matches */
+ subs = (regmatch_t *)ecalloc(sizeof(regmatch_t),re.re_nsub+1);
+ if (!subs) {
+ php_error(E_WARNING, "Unable to allocate memory in php_ereg_replace");
+ return ((char *) -1);
+ }
+
/* start with a buffer that is twice the size of the stringo
we're doing replacements in */
buf_len = 2 * string_len + 1;
buf = emalloc(buf_len * sizeof(char));
if (!buf) {
- php_error(E_WARNING, "Unable to allocate memory in php_reg_replace");
+ php_error(E_WARNING, "Unable to allocate memory in php_ereg_replace");
+ efree(subs);
regfree(&re);
return ((char *) -1);
}
@@ -302,10 +317,12 @@
err = pos = 0;
buf[0] = '\0';
while (!err) {
- err = regexec(&re, &string[pos], (size_t) NS, subs, (pos ? REG_NOTBOL : 0));
+ err = regexec(&re, &string[pos], re.re_nsub+1, subs, (pos ? REG_NOTBOL : 0));
if (err && err != REG_NOMATCH) {
php_reg_eprint(err, &re);
+ efree(subs);
+ efree(buf);
regfree(&re);
return ((char *) -1);
}
@@ -396,6 +413,7 @@
}
/* don't want to leak memory .. */
+ efree(subs);
regfree(&re);
/* whew. */