@@ -652,7 +652,20 @@ PHP_FUNCTION(imagesetstyle)
652652 stylearr = safe_emalloc (sizeof (int ), num_styles , 0 );
653653
654654 ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (styles ), item ) {
655- stylearr [index ++ ] = zval_get_long (item );
655+ bool failed = false;
656+ ZVAL_DEREF (item );
657+ zend_long tmp = zval_try_get_long (item , & failed );
658+ if (failed ) {
659+ efree (stylearr );
660+ zend_argument_value_error (2 , "value must be of type int, %s given" , zend_zval_type_name (item ));
661+ RETURN_THROWS ();
662+ }
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 ;
656669 } ZEND_HASH_FOREACH_END ();
657670
658671 gdImageSetStyle (im , stylearr , index );
@@ -3648,7 +3661,20 @@ static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
36483661 colors = emalloc (num_colors * sizeof (int ));
36493662
36503663 ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (hash_colors ), color ) {
3651- * (colors + i ++ ) = (int ) zval_get_long (color );
3664+ bool failed = false;
3665+ ZVAL_DEREF (color );
3666+ zend_long tmp = zval_try_get_long (color , & failed );
3667+ if (failed ) {
3668+ efree (colors );
3669+ zend_argument_value_error (5 , "value must be of type int, %s given" , zend_zval_type_name (color ));
3670+ RETURN_THROWS ();
3671+ }
3672+ if (tmp < 0 || ZEND_LONG_INT_OVFL (tmp )) {
3673+ efree (colors );
3674+ zend_argument_value_error (5 , "value must be between 0 and %d" , INT_MAX );
3675+ RETURN_THROWS ();
3676+ }
3677+ colors [i ++ ] = (int ) tmp ;
36523678 } ZEND_HASH_FOREACH_END ();
36533679
36543680 RETVAL_BOOL (gdImageScatterColor (im , (int )scatter_sub , (int )scatter_plus , colors , num_colors ));
@@ -3822,6 +3848,22 @@ PHP_FUNCTION(imageantialias)
38223848}
38233849/* }}} */
38243850
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+
38253867/* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
38263868PHP_FUNCTION (imagecrop )
38273869{
@@ -3840,28 +3882,36 @@ PHP_FUNCTION(imagecrop)
38403882 im = php_gd_libgdimageptr_from_zval_p (IM );
38413883
38423884 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "x" , sizeof ("x" ) - 1 )) != NULL ) {
3843- rect .x = zval_get_long (tmp );
3885+ if (!_php_gd_zval_try_get_c_int (tmp , "x" , & rect .x )) {
3886+ RETURN_THROWS ();
3887+ }
38443888 } else {
38453889 zend_argument_value_error (2 , "must have an \"x\" key" );
38463890 RETURN_THROWS ();
38473891 }
38483892
38493893 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "y" , sizeof ("y" ) - 1 )) != NULL ) {
3850- rect .y = zval_get_long (tmp );
3894+ if (!_php_gd_zval_try_get_c_int (tmp , "y" , & rect .y )) {
3895+ RETURN_THROWS ();
3896+ }
38513897 } else {
38523898 zend_argument_value_error (2 , "must have a \"y\" key" );
38533899 RETURN_THROWS ();
38543900 }
38553901
38563902 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "width" , sizeof ("width" ) - 1 )) != NULL ) {
3857- rect .width = zval_get_long (tmp );
3903+ if (!_php_gd_zval_try_get_c_int (tmp , "width" , & rect .width )) {
3904+ RETURN_THROWS ();
3905+ }
38583906 } else {
38593907 zend_argument_value_error (2 , "must have a \"width\" key" );
38603908 RETURN_THROWS ();
38613909 }
38623910
38633911 if ((tmp = zend_hash_str_find (Z_ARRVAL_P (z_rect ), "height" , sizeof ("height" ) - 1 )) != NULL ) {
3864- rect .height = zval_get_long (tmp );
3912+ if (!_php_gd_zval_try_get_c_int (tmp , "height" , & rect .height )) {
3913+ RETURN_THROWS ();
3914+ }
38653915 } else {
38663916 zend_argument_value_error (2 , "must have a \"height\" key" );
38673917 RETURN_THROWS ();
0 commit comments