@@ -2326,8 +2326,48 @@ void gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, int
23262326 _gdImageFilledVRectangle (im , x1 , y1 , x2 , y2 , color );
23272327}
23282328
2329+ static int _gdValidateCopyRectBounds (
2330+ const gdImagePtr dst ,
2331+ const gdImagePtr src ,
2332+ int dstX , int dstY ,
2333+ int srcX , int srcY ,
2334+ int w , int h
2335+ ) {
2336+ /* Check for null pointers */
2337+ if (!dst || !src ) {
2338+ return 0 ;
2339+ }
2340+
2341+ /* Check for overflow in dstX + w */
2342+ if (w > 0 && dstX > INT_MAX - w ) {
2343+ return 0 ;
2344+ }
2345+
2346+ /* Check for overflow in dstY + h */
2347+ if (h > 0 && dstY > INT_MAX - h ) {
2348+ return 0 ;
2349+ }
2350+
2351+ /* Check for overflow in srcX + w */
2352+ if (w > 0 && srcX > INT_MAX - w ) {
2353+ return 0 ;
2354+ }
2355+
2356+ /* Check for overflow in srcY + h */
2357+ if (h > 0 && srcY > INT_MAX - h ) {
2358+ return 0 ;
2359+ }
2360+
2361+ return 1 ;
2362+ }
2363+
2364+
23292365void gdImageCopy (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h )
23302366{
2367+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2368+ return ;
2369+ }
2370+
23312371 int c ;
23322372 int x , y ;
23332373 int tox , toy ;
@@ -2409,6 +2449,10 @@ void gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX,
24092449 so it doesn't pay attention to the alpha channel. */
24102450void gdImageCopyMerge (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24112451{
2452+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2453+ return ;
2454+ }
2455+
24122456 int c , dc ;
24132457 int x , y ;
24142458 int tox , toy ;
@@ -2449,6 +2493,10 @@ void gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int s
24492493 so it doesn't pay attention to the alpha channel. */
24502494void gdImageCopyMergeGray (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24512495{
2496+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2497+ return ;
2498+ }
2499+
24522500 int c , dc ;
24532501 int x , y ;
24542502 int tox , toy ;
@@ -2503,6 +2551,10 @@ void gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, i
25032551
25042552void gdImageCopyResized (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
25052553{
2554+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2555+ return ;
2556+ }
2557+
25062558 int c ;
25072559 int x , y ;
25082560 int tox , toy ;
@@ -2613,6 +2665,10 @@ void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int
26132665
26142666void gdImageCopyResampled (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
26152667{
2668+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2669+ return ;
2670+ }
2671+
26162672 int x , y ;
26172673 double sy1 , sy2 , sx1 , sx2 ;
26182674
0 commit comments