Skip to content

Commit af8ecb3

Browse files
marandallcmb69
authored andcommitted
Warnings for image colour range check now throw exceptions
1 parent 6c6c109 commit af8ecb3

File tree

3 files changed

+48
-86
lines changed

3 files changed

+48
-86
lines changed

ext/gd/gd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,8 @@ PHP_FUNCTION(imagelayereffect)
11091109

11101110
#define CHECK_RGBA_RANGE(component, name) \
11111111
if (component < 0 || component > gd##name##Max) { \
1112-
php_error_docref(NULL, E_WARNING, #name " component is out of range"); \
1113-
RETURN_FALSE; \
1112+
zend_throw_error(NULL, #name " component is out of range, must be between 0 and %d (inclusive)", gd##name##Max); \
1113+
return; \
11141114
}
11151115

11161116
/* {{{ proto int imagecolorallocatealpha(resource im, int red, int green, int blue, int alpha)

ext/gd/tests/imagecolorallocate_variation5.phpt

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ if(!function_exists('imagecreatetruecolor')) {
1111
?>
1212
--FILE--
1313
<?php
14+
require __DIR__ . '/func.inc';
15+
1416
/* Prototype : int imagecolorallocate(resource im, int red, int green, int blue)
1517
* Description: Allocate a color for an image
1618
* Source code: ext/gd/gd.c
@@ -38,14 +40,17 @@ $values = array(
3840

3941
// loop through each element of the array for blue
4042
foreach($values as $key => $value) {
41-
echo "\n--$key--\n";
42-
var_dump( imagecolorallocate($im, $value, $green, $blue) );
43-
var_dump( imagecolorallocate($im, $red, $value, $blue) );
44-
var_dump( imagecolorallocate($im, $red, $green, $value) );
43+
echo "\n--$key--\n";
44+
45+
trycatch_dump(
46+
fn() => imagecolorallocate($im, $value, $green, $blue),
47+
fn() => imagecolorallocate($im, $red, $value, $blue),
48+
fn() => imagecolorallocate($im, $red, $green, $value)
49+
);
4550
};
4651
?>
4752
===DONE===
48-
--EXPECTF--
53+
--EXPECT--
4954
*** Testing imagecolorallocate() : usage variations ***
5055

5156
--Octal 000--
@@ -59,15 +64,9 @@ int(657930)
5964
int(657930)
6065

6166
--Octal -012--
62-
63-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
64-
bool(false)
65-
66-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
67-
bool(false)
68-
69-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
70-
bool(false)
67+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
68+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
69+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
7170

7271
--Octal 0377--
7372
int(16714250)
@@ -85,15 +84,9 @@ int(657930)
8584
int(657930)
8685

8786
--Hexa-decimal -0xA--
88-
89-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
90-
bool(false)
91-
92-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
93-
bool(false)
94-
95-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
96-
bool(false)
87+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
88+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
89+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
9790

9891
--Hexa-decimal 0xFF--
9992
int(16714250)

ext/gd/tests/imagecolorallocate_variation6.phpt

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ if(!function_exists('imagecreatetruecolor')) {
1111
?>
1212
--FILE--
1313
<?php
14+
require __DIR__ . '/func.inc';
15+
1416
/* Prototype : int imagecolorallocate(resource im, int red, int green, int blue)
1517
* Description: Allocate a color for an image
1618
* Source code: ext/gd/gd.c
@@ -34,75 +36,42 @@ foreach($values as $key => $value) {
3436
//Need to be created every time to get expected return value
3537
$im_palette = imagecreate(200, 200);
3638
$im_true_color = imagecreatetruecolor(200, 200);
37-
var_dump( imagecolorallocate($im_palette, $value, 0, 0) );
38-
var_dump( imagecolorallocate($im_true_color, $value, 0, 0) );
39-
var_dump( imagecolorallocate($im_palette, 0, $value, 0) );
40-
var_dump( imagecolorallocate($im_true_color, 0, $value, 0) );
41-
var_dump( imagecolorallocate($im_palette, 0, 0, $value) );
42-
var_dump( imagecolorallocate($im_true_color, 0, 0, $value) );
39+
40+
trycatch_dump(
41+
fn() => imagecolorallocate($im_palette, $value, 0, 0),
42+
fn() => imagecolorallocate($im_true_color, $value, 0, 0),
43+
fn() => imagecolorallocate($im_palette, 0, $value, 0),
44+
fn() => imagecolorallocate($im_true_color, 0, $value, 0),
45+
fn() => imagecolorallocate($im_palette, 0, 0, $value),
46+
fn() => imagecolorallocate($im_true_color, 0, 0, $value)
47+
);
4348
};
4449
?>
4550
===DONE===
46-
--EXPECTF--
51+
--EXPECT--
4752
*** Testing imagecolorallocate() : usage variations ***
4853

4954
--Decimal 256--
50-
51-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
52-
bool(false)
53-
54-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
55-
bool(false)
56-
57-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
58-
bool(false)
59-
60-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
61-
bool(false)
62-
63-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
64-
bool(false)
65-
66-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
67-
bool(false)
55+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
56+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
57+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
58+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
59+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
60+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
6861

6962
--Octal 0400--
70-
71-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
72-
bool(false)
73-
74-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
75-
bool(false)
76-
77-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
78-
bool(false)
79-
80-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
81-
bool(false)
82-
83-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
84-
bool(false)
85-
86-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
87-
bool(false)
63+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
64+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
65+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
66+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
67+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
68+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
8869

8970
--Hexa-decimal 0x100--
90-
91-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
92-
bool(false)
93-
94-
Warning: imagecolorallocate(): Red component is out of range in %s on line %d
95-
bool(false)
96-
97-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
98-
bool(false)
99-
100-
Warning: imagecolorallocate(): Green component is out of range in %s on line %d
101-
bool(false)
102-
103-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
104-
bool(false)
105-
106-
Warning: imagecolorallocate(): Blue component is out of range in %s on line %d
107-
bool(false)
71+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
72+
!! [Error] Red component is out of range, must be between 0 and 255 (inclusive)
73+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
74+
!! [Error] Green component is out of range, must be between 0 and 255 (inclusive)
75+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
76+
!! [Error] Blue component is out of range, must be between 0 and 255 (inclusive)
10877
===DONE===

0 commit comments

Comments
 (0)