@@ -2307,8 +2307,48 @@ void gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, int
23072307 _gdImageFilledVRectangle (im , x1 , y1 , x2 , y2 , color );
23082308}
23092309
2310+ static int _gdValidateCopyRectBounds (
2311+ const gdImagePtr dst ,
2312+ const gdImagePtr src ,
2313+ int dstX , int dstY ,
2314+ int srcX , int srcY ,
2315+ int w , int h
2316+ ) {
2317+ /* Check for null pointers */
2318+ if (!dst || !src ) {
2319+ return 0 ;
2320+ }
2321+
2322+ /* Check for overflow in dstX + w */
2323+ if (w > 0 && dstX > INT_MAX - w ) {
2324+ return 0 ;
2325+ }
2326+
2327+ /* Check for overflow in dstY + h */
2328+ if (h > 0 && dstY > INT_MAX - h ) {
2329+ return 0 ;
2330+ }
2331+
2332+ /* Check for overflow in srcX + w */
2333+ if (w > 0 && srcX > INT_MAX - w ) {
2334+ return 0 ;
2335+ }
2336+
2337+ /* Check for overflow in srcY + h */
2338+ if (h > 0 && srcY > INT_MAX - h ) {
2339+ return 0 ;
2340+ }
2341+
2342+ return 1 ;
2343+ }
2344+
2345+
23102346void gdImageCopy (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h )
23112347{
2348+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2349+ return ;
2350+ }
2351+
23122352 int c ;
23132353 int x , y ;
23142354 int tox , toy ;
@@ -2390,6 +2430,10 @@ void gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX,
23902430 so it doesn't pay attention to the alpha channel. */
23912431void gdImageCopyMerge (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
23922432{
2433+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2434+ return ;
2435+ }
2436+
23932437 int c , dc ;
23942438 int x , y ;
23952439 int tox , toy ;
@@ -2430,6 +2474,10 @@ void gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int s
24302474 so it doesn't pay attention to the alpha channel. */
24312475void gdImageCopyMergeGray (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24322476{
2477+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2478+ return ;
2479+ }
2480+
24332481 int c , dc ;
24342482 int x , y ;
24352483 int tox , toy ;
@@ -2484,6 +2532,10 @@ void gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, i
24842532
24852533void gdImageCopyResized (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
24862534{
2535+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2536+ return ;
2537+ }
2538+
24872539 int c ;
24882540 int x , y ;
24892541 int tox , toy ;
@@ -2594,6 +2646,10 @@ void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int
25942646
25952647void gdImageCopyResampled (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
25962648{
2649+ if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2650+ return ;
2651+ }
2652+
25972653 int x , y ;
25982654 double sy1 , sy2 , sx1 , sx2 ;
25992655
0 commit comments