From 07804e2482d2c474a7ae0abd43bee0e41afda40c Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Thu, 4 Feb 2010 09:44:16 +0000 Subject: [PATCH] - Fixed bug #50847 (strip_tags() removes all tags greater then 1023 bytes long) --- ext/standard/string.c | 46 +++++++++++++++++++----- ext/standard/tests/strings/bug50847.phpt | 10 ++++++ 2 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 ext/standard/tests/strings/bug50847.phpt diff --git a/ext/standard/string.c b/ext/standard/string.c index e537b310da..88b8099683 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -4243,7 +4243,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, { char *tbuf, *buf, *p, *tp, *rp, c, lc; int br, i=0, depth=0, in_q = 0; - int state = 0; + int state = 0, pos; if (stateptr) state = *stateptr; @@ -4256,7 +4256,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, br = 0; if (allow) { php_strtolower(allow, allow_len); - tbuf = emalloc(PHP_TAG_BUF_SIZE+1); + tbuf = emalloc(PHP_TAG_BUF_SIZE + 1); tp = tbuf; } else { tbuf = tp = NULL; @@ -4277,7 +4277,11 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, lc = '<'; state = 1; if (allow) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = '<'; } } else if (state == 1) { @@ -4292,7 +4296,11 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, br++; } } else if (allow && state == 1) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = c; } else if (state == 0) { *(rp++) = c; @@ -4306,7 +4314,11 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, br--; } } else if (allow && state == 1) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = c; } else if (state == 0) { *(rp++) = c; @@ -4328,7 +4340,11 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, lc = '>'; in_q = state = 0; if (allow) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = '>'; *tp='\0'; if (php_tag_find(tbuf, tp-tbuf, allow)) { @@ -4378,7 +4394,11 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, } else if (state == 0) { *(rp++) = c; } else if (allow && state == 1) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = c; } if (state && p != buf && (state == 1 || *(p-1) != '\\') && (!in_q || *p == in_q)) { @@ -4399,7 +4419,11 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, if (state == 0) { *(rp++) = c; } else if (allow && state == 1) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = c; } } @@ -4454,7 +4478,11 @@ reg_char: if (state == 0) { *(rp++) = c; } else if (allow && state == 1) { - tp = ((tp-tbuf) >= PHP_TAG_BUF_SIZE ? tbuf: tp); + if (tp - tbuf >= PHP_TAG_BUF_SIZE) { + pos = tp - tbuf; + tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1); + tp = tbuf + pos; + } *(tp++) = c; } break; diff --git a/ext/standard/tests/strings/bug50847.phpt b/ext/standard/tests/strings/bug50847.phpt new file mode 100644 index 0000000000..28e83f5111 --- /dev/null +++ b/ext/standard/tests/strings/bug50847.phpt @@ -0,0 +1,10 @@ +--TEST-- +Bug #50847 (strip_tags() removes all tags greater then 1023 bytes long) +--FILE-- +'; +var_dump(strip_tags($var, ""), strip_tags($var)); +?> +--EXPECT-- +string(2066) "" +string(0) "" -- 2.50.1