# [PATCH] Fix segfault in filter extension when using callbacks

- Thread: https://externals.io/message/35768
- Original archive: https://news-web.php.net/php.internals/35768
- Started by Arnar Mar Sig on 2008-02-24T15:37:28+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #35768 — Arnar Mar Sig — 2008-02-24T15:37:28+00:00

URL: https://externals.io/message/35768#35768

The filter extensinon will segfault if a callback filter returns  
FALSE, this little patch fixes that.

There is also no way to set default value if the callback fails  
because of a design flow. When dealing with callbacks the 'options'  
key in the options parameters just be a valid callback, but to set a  
default value then this key must be a array that includes the key  
'default'.


Test case:
function filter_test($str) { return FALSE; }
var_dump(filter_var('durty/boy', FILTER_CALLBACK, array(
   'options'   => 'filter_test',
)));


Arnar Mar Sig

---

## #35769 — Arnar Mar Sig — 2008-02-24T15:48:50+00:00

URL: https://externals.io/message/35768#35769 · In reply to: #35768

Erm, the attachement got stripped away, heres the patch..

--- php-5.2.5/ext/filter/filter.c   2007-04-04 20:50:26.000000000 +0000
+++ php-5.2.5-fixed/ext/filter/filter.c 2008-02-24 15:14:19.000000000  
+0000
@@ -322,7 +322,7 @@
     filter_func.function(*value, flags, options, charset TSRMLS_CC);

     if (
-       options &&
+       options && (Z_TYPE_P(options) == IS_ARRAY || Z_TYPE_P(options)  
== IS_OBJECT) &&
         ((flags & `FILTER_NULL_ON_FAILURE` && Z_TYPE_PP(value) ==  
IS_NULL) ||
         (!(flags & FILTER_NULL_ON_FAILURE) && Z_TYPE_PP(value) ==  
IS_BOOL && Z_LVAL_PP(value) == 0)) &&
         zend_hash_exists(HASH_OF(options), "default",  
sizeof("default"))

Arnar Mar Sig



> The filter extensinon will segfault if a callback filter returns  
> FALSE, this little patch fixes that.
>
> There is also no way to set default value if the callback fails  
> because of a design flow. When dealing with callbacks the 'options'  
> key in the options parameters just be a valid callback, but to set a  
> default value then this key must be a array that includes the key  
> 'default'.
>
>
> Test case:
> function filter_test($str) { return FALSE; }
> var_dump(filter_var('durty/boy', FILTER_CALLBACK, array(
>  'options'   => 'filter_test',
> )));
>
>
> Arnar Mar Sig

---

## #35771 — Felipe Pena — 2008-02-24T18:36:06+00:00

URL: https://externals.io/message/35768#35771 · In reply to: #35769

Em Dom, 2008-02-24 às 15:48 +0000, Arnar Mar Sig escreveu:
> Erm, the attachement got stripped away, heres the patch..
> 
> --- php-5.2.5/ext/filter/filter.c   2007-04-04 20:50:26.000000000 +0000
> +++ php-5.2.5-fixed/ext/filter/filter.c 2008-02-24 15:14:19.000000000  
> +0000
> @@ -322,7 +322,7 @@
>      filter_func.function(*value, flags, options, charset TSRMLS_CC);
> 
>      if (
> -       options &&
> +       options && (Z_TYPE_P(options) == IS_ARRAY || Z_TYPE_P(options)  
> == IS_OBJECT) &&
>          ((flags & `FILTER_NULL_ON_FAILURE` && Z_TYPE_PP(value) ==  
> IS_NULL) ||
>          (!(flags & FILTER_NULL_ON_FAILURE) && Z_TYPE_PP(value) ==  
> IS_BOOL && Z_LVAL_PP(value) == 0)) &&
>          zend_hash_exists(HASH_OF(options), "default",  
> sizeof("default"))
> 
> Arnar Mar Sig


Fixed in CVS.

Thanks.

-- 
Regards,
Felipe Pena.

---

## #35772 — Pierre Joye — 2008-02-24T18:45:58+00:00

URL: https://externals.io/message/35768#35772 · In reply to: #35771

Hi Arnar, Felipe!


> Em Dom, 2008-02-24 às 15:48 +0000, Arnar Mar Sig escreveu:
>  Fixed in CVS.
>

Thanks for your patches and quick reaction :)

Can you add a test while being at it please?

Cheers,
-- 
Pierre
http://blog.thepimp.net | http://www.libgd.org

---

## #35773 — Felipe Pena — 2008-02-24T18:51:34+00:00

URL: https://externals.io/message/35768#35773 · In reply to: #35772

Em Dom, 2008-02-24 às 19:45 +0100, Pierre Joye escreveu:
> Hi Arnar, Felipe!
> 
> 
> > […]
> 
> Thanks for your patches and quick reaction :)
> 
> Can you add a test while being at it please?
> 
> Cheers,

Done! ;)
-- 
Regards,
Felipe Pena.
