@@ -660,7 +660,12 @@ PHP_FUNCTION(imagesetstyle)
660660 zend_argument_value_error (2 , "value must be of type int, %s given" , zend_zval_type_name (item ));
661661 RETURN_THROWS ();
662662 }
663- stylearr [index ++ ] = tmp ;
663+ if (ZEND_LONG_EXCEEDS_INT (tmp )) {
664+ efree (stylearr );
665+ zend_argument_value_error (2 , "value must be between %d and %d" , INT_MIN , INT_MAX );
666+ RETURN_THROWS ();
667+ }
668+ stylearr [index ++ ] = (int ) tmp ;
664669 } ZEND_HASH_FOREACH_END ();
665670
666671 gdImageSetStyle (im , stylearr , index );
@@ -3669,7 +3674,7 @@ static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
36693674 zend_argument_value_error (5 , "value must be between 0 and %d" , INT_MAX );
36703675 RETURN_THROWS ();
36713676 }
3672- * ( colors + i ++ ) = (int ) tmp ;
3677+ colors [ i ++ ] = (int ) tmp ;
36733678 } ZEND_HASH_FOREACH_END ();
36743679
36753680 RETVAL_BOOL (gdImageScatterColor (im , (int )scatter_sub , (int )scatter_plus , colors , num_colors ));
@@ -3843,6 +3848,22 @@ PHP_FUNCTION(imageantialias)
38433848}
38443849/* }}} */
38453850
3851+ static bool _php_gd_zval_try_get_c_int (zval * tmp , const char * field , int * res ) {
3852+ zend_long r ;
3853+ bool failed = false;
3854+ r = zval_try_get_long (tmp , & failed );
3855+ if (failed ) {
3856+ zend_argument_value_error (2 , "\"%s\" key must be of type int, %s given" , field , zend_zval_type_name (tmp ));
3857+ return false;
3858+ }
3859+ if (UNEXPECTED (ZEND_LONG_EXCEEDS_INT (r ))) {
3860+ zend_argument_value_error (2 , "\"%s\" key must be between %d and %d" , field , INT_MIN , INT_MAX );
3861+ return false;
3862+ }
3863+ * res = (int )r ;
3864+ return true;
3865+ }
3866+
38463867/* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
38473868PHP_FUNCTION (imagecrop )
38483869{
@@ -3852,7 +3873,6 @@ PHP_FUNCTION(imagecrop)
38523873 gdRect rect ;
38533874 zval * z_rect ;
38543875 zval * tmp ;
3855- zend_long r ;
38563876
38573877 ZEND_PARSE_PARAMETERS_START (2 , 2 )
38583878 Z_PARAM_OBJECT_OF_CLASS (IM , gd_image_ce )
@@ -3862,48 +3882,36 @@ PHP_FUNCTION(imagecrop)
38623882 im = php_gd_libgdimageptr_from_zval_p (IM );
38633883
38643884 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "x" , sizeof ("x" ) - 1 )) != NULL ) {
3865- r = zval_get_long (tmp );
3866- if (ZEND_LONG_EXCEEDS_INT (r )) {
3867- zend_argument_value_error (2 , "\"x\" key must be between %d and %d\n" , INT_MIN , INT_MAX );
3885+ if (!_php_gd_zval_try_get_c_int (tmp , "x" , & rect .x )) {
38683886 RETURN_THROWS ();
38693887 }
3870- rect .x = (int )r ;
38713888 } else {
38723889 zend_argument_value_error (2 , "must have an \"x\" key" );
38733890 RETURN_THROWS ();
38743891 }
38753892
38763893 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "y" , sizeof ("y" ) - 1 )) != NULL ) {
3877- r = zval_get_long (tmp );
3878- if (ZEND_LONG_EXCEEDS_INT (r )) {
3879- zend_argument_value_error (2 , "\"y\" key must be between %d and %d\n" , INT_MIN , INT_MAX );
3894+ if (!_php_gd_zval_try_get_c_int (tmp , "y" , & rect .y )) {
38803895 RETURN_THROWS ();
38813896 }
3882- rect .y = (int )r ;
38833897 } else {
38843898 zend_argument_value_error (2 , "must have a \"y\" key" );
38853899 RETURN_THROWS ();
38863900 }
38873901
38883902 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "width" , sizeof ("width" ) - 1 )) != NULL ) {
3889- r = zval_get_long (tmp );
3890- if (ZEND_LONG_EXCEEDS_INT (r )) {
3891- zend_argument_value_error (2 , "\"width\" key must be between %d and %d\n" , INT_MIN , INT_MAX );
3903+ if (!_php_gd_zval_try_get_c_int (tmp , "width" , & rect .width )) {
38923904 RETURN_THROWS ();
38933905 }
3894- rect .width = (int )r ;
38953906 } else {
38963907 zend_argument_value_error (2 , "must have a \"width\" key" );
38973908 RETURN_THROWS ();
38983909 }
38993910
39003911 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "height" , sizeof ("height" ) - 1 )) != NULL ) {
3901- r = zval_get_long (tmp );
3902- if (ZEND_LONG_EXCEEDS_INT (r )) {
3903- zend_argument_value_error (2 , "\"height\" key must be between %d and %d\n" , INT_MIN , INT_MAX );
3912+ if (!_php_gd_zval_try_get_c_int (tmp , "height" , & rect .height )) {
39043913 RETURN_THROWS ();
39053914 }
3906- rect .height = (int )r ;
39073915 } else {
39083916 zend_argument_value_error (2 , "must have a \"height\" key" );
39093917 RETURN_THROWS ();
0 commit comments