Tk Source Code

Artifact [0c879efa]
Login

Artifact 0c879efab429a9c4e8c9842057c6a3b6c0508cc4:

Attachment "fix.patch" to ticket [e4336bef] added by anonymous 2017-02-17 17:03:57. (unpublished)
diff -ur tk8.6.6-orig/generic/tkImgPhoto.c tk8.6.6/generic/tkImgPhoto.c
--- tk8.6.6-orig/generic/tkImgPhoto.c	2017-02-12 14:17:54.023799062 +0100
+++ tk8.6.6/generic/tkImgPhoto.c	2017-02-17 14:58:07.734121510 +0100
@@ -2686,7 +2686,7 @@
 				 * messages, or NULL. */
     Tk_PhotoHandle handle,	/* Opaque handle for the photo image to be
 				 * updated. */
-    register Tk_PhotoImageBlock *blockPtr,
+    Tk_PhotoImageBlock *blockPtr,
 				/* Pointer to a structure describing the pixel
 				 * data to be copied into the image. */
     int x, int y,		/* Coordinates of the top-left pixel to be
@@ -2697,6 +2697,8 @@
 				 * transparent pixels. */
 {
     register PhotoMaster *masterPtr = (PhotoMaster *) handle;
+    register Tk_PhotoImageBlock sourceBlock;
+    unsigned char *memToFree;
     int xEnd, yEnd, greenOffset, blueOffset, alphaOffset;
     int wLeft, hLeft, wCopy, hCopy, pitch;
     unsigned char *srcPtr, *srcLinePtr, *destPtr, *destLinePtr;
@@ -2724,11 +2726,43 @@
 	return TCL_OK;
     }
 
+    /*
+     * Fix for bug e4336bef5d:
+     * 
+     * Make a local copy of *blockPtr, as we might have to change some
+     * of its fields and don't want to interfere with the caller's data.
+     * 
+     * If source and destination are the same image, create a copy  of the 
+     * source data in our local sourceBlock.
+     * 
+     * To find out, just comparing the pointers is not enough - they might have
+     * different values and still point to the same block of memory. (e.g.
+     * if the -from option was passed to [imageName copy])
+     */
+    sourceBlock = *blockPtr;
+    memToFree = NULL;
+    if (sourceBlock.pixelPtr >= masterPtr->pix32 
+	    && sourceBlock.pixelPtr <= masterPtr->pix32 + masterPtr->width
+	    * masterPtr->height * 4) {
+	sourceBlock.pixelPtr = attemptckalloc(sourceBlock.height 
+	    * sourceBlock.pitch);
+	if (sourceBlock.pixelPtr == NULL) {
+	    if (interp != NULL) {
+		Tcl_SetObjResult(interp, Tcl_NewStringObj(
+			TK_PHOTO_ALLOC_FAILURE_MESSAGE, -1));
+		Tcl_SetErrorCode(interp, "TK", "MALLOC", NULL);
+	    }
+	    return TCL_ERROR;
+	}
+	memToFree = sourceBlock.pixelPtr;
+	memcpy(sourceBlock.pixelPtr, blockPtr->pixelPtr, sourceBlock.height 
+	    * sourceBlock.pitch);
+    }
+    
+    
     xEnd = x + width;
     yEnd = y + height;
     if ((xEnd > masterPtr->width) || (yEnd > masterPtr->height)) {
-	int sameSrc = (blockPtr->pixelPtr == masterPtr->pix32);
-
 	if (ImgPhotoSetSize(masterPtr, MAX(xEnd, masterPtr->width),
 		MAX(yEnd, masterPtr->height)) == TCL_ERROR) {
 	    if (interp != NULL) {
@@ -2736,11 +2770,7 @@
 			TK_PHOTO_ALLOC_FAILURE_MESSAGE, -1));
 		Tcl_SetErrorCode(interp, "TK", "MALLOC", NULL);
 	    }
-	    return TCL_ERROR;
-	}
-	if (sameSrc) {
-	    blockPtr->pixelPtr = masterPtr->pix32;
-	    blockPtr->pitch = masterPtr->width * 4;
+	    goto errorExit;
 	}
     }
 
@@ -2759,14 +2789,14 @@
      * components, mark it as a color image.
      */
 
-    greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
-    blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
-    alphaOffset = blockPtr->offset[3];
-    if ((alphaOffset >= blockPtr->pixelSize) || (alphaOffset < 0)) {
+    greenOffset = sourceBlock.offset[1] - sourceBlock.offset[0];
+    blueOffset = sourceBlock.offset[2] - sourceBlock.offset[0];
+    alphaOffset = sourceBlock.offset[3];
+    if ((alphaOffset >= sourceBlock.pixelSize) || (alphaOffset < 0)) {
 	alphaOffset = 0;
 	sourceIsSimplePhoto = 1;
     } else {
-	alphaOffset -= blockPtr->offset[0];
+	alphaOffset -= sourceBlock.offset[0];
     }
     if ((greenOffset != 0) || (blueOffset != 0)) {
 	masterPtr->flags |= COLOR_IMAGE;
@@ -2786,13 +2816,13 @@
      * pixelSize == 3 and alphaOffset == 0. Maybe other cases too.
      */
 
-    if ((blockPtr->pixelSize == 4)
+    if ((sourceBlock.pixelSize == 4)
 	    && (greenOffset == 1) && (blueOffset == 2) && (alphaOffset == 3)
-	    && (width <= blockPtr->width) && (height <= blockPtr->height)
+	    && (width <= sourceBlock.width) && (height <= sourceBlock.height)
 	    && ((height == 1) || ((x == 0) && (width == masterPtr->width)
-		&& (blockPtr->pitch == pitch)))
+		&& (sourceBlock.pitch == pitch)))
 	    && (compRule == TK_PHOTO_COMPOSITE_SET)) {
-	memmove(destLinePtr, blockPtr->pixelPtr + blockPtr->offset[0],
+	memmove(destLinePtr, sourceBlock.pixelPtr + sourceBlock.offset[0],
 		((size_t)height * width * 4));
 
 	/*
@@ -2808,11 +2838,11 @@
      */
 
     for (hLeft = height; hLeft > 0;) {
-	int pixelSize = blockPtr->pixelSize;
+	int pixelSize = sourceBlock.pixelSize;
 	int compRuleSet = (compRule == TK_PHOTO_COMPOSITE_SET);
 
-	srcLinePtr = blockPtr->pixelPtr + blockPtr->offset[0];
-	hCopy = MIN(hLeft, blockPtr->height);
+	srcLinePtr = sourceBlock.pixelPtr + sourceBlock.offset[0];
+	hCopy = MIN(hLeft, sourceBlock.height);
 	hLeft -= hCopy;
 	for (; hCopy > 0; --hCopy) {
 	    /*
@@ -2823,10 +2853,10 @@
 
 	    if ((pixelSize == 4) && (greenOffset == 1)
 		    && (blueOffset == 2) && (alphaOffset == 3)
-		    && (width <= blockPtr->width)
+		    && (width <= sourceBlock.width)
 		    && compRuleSet) {
 		memcpy(destLinePtr, srcLinePtr, ((size_t)width * 4));
-		srcLinePtr += blockPtr->pitch;
+		srcLinePtr += sourceBlock.pitch;
 		destLinePtr += pitch;
 		continue;
 	    }
@@ -2837,7 +2867,7 @@
 
 	    destPtr = destLinePtr;
 	    for (wLeft = width; wLeft > 0;) {
-		wCopy = MIN(wLeft, blockPtr->width);
+		wCopy = MIN(wLeft, sourceBlock.width);
 		wLeft -= wCopy;
 		srcPtr = srcLinePtr;
 
@@ -2927,7 +2957,7 @@
 		    destPtr += 4;
 		}
 	    }
-	    srcLinePtr += blockPtr->pitch;
+	    srcLinePtr += sourceBlock.pitch;
 	    destLinePtr += pitch;
 	}
     }
@@ -3043,7 +3073,15 @@
 
     Tk_ImageChanged(masterPtr->tkMaster, x, y, width, height,
 	    masterPtr->width, masterPtr->height);
+    
+    ckfree(memToFree);
+    
     return TCL_OK;
+    
+  errorExit:
+    ckfree(memToFree);
+    
+    return TCL_ERROR;
 }
 
 /*
@@ -3070,7 +3108,7 @@
 				 * messages, or NULL. */
     Tk_PhotoHandle handle,	/* Opaque handle for the photo image to be
 				 * updated. */
-    register Tk_PhotoImageBlock *blockPtr,
+    Tk_PhotoImageBlock *blockPtr,
 				/* Pointer to a structure describing the pixel
 				 * data to be copied into the image. */
     int x, int y,		/* Coordinates of the top-left pixel to be
@@ -3085,6 +3123,8 @@
 				 * transparent pixels. */
 {
     register PhotoMaster *masterPtr = (PhotoMaster *) handle;
+    register Tk_PhotoImageBlock sourceBlock;
+    unsigned char *memToFree;
     int xEnd, yEnd, greenOffset, blueOffset, alphaOffset;
     int wLeft, hLeft, wCopy, hCopy, blockWid, blockHt;
     unsigned char *srcPtr, *srcLinePtr, *srcOrigPtr, *destPtr, *destLinePtr;
@@ -3120,12 +3160,42 @@
     if (width <= 0 || height <= 0) {
 	return TCL_OK;
     }
+    
+    /*
+     * Fix for Bug e4336bef5d:
+     * Make a local copy of *blockPtr, as we might have to change some
+     * of its fields and don't want to interfere with the caller's data.
+     * 
+     * If source and destination are the same image, create a copy  of the 
+     * source data in our local sourceBlock.
+     * 
+     * To find out, just comparing the pointers is not enough - they might have
+     * different values and still point to the same block of memory. (e.g.
+     * if the -from option was passed to [imageName copy])
+     */
+    sourceBlock = *blockPtr;
+    memToFree = NULL;
+    if (sourceBlock.pixelPtr >= masterPtr->pix32 
+	    && sourceBlock.pixelPtr <= masterPtr->pix32 + masterPtr->width
+	    * masterPtr->height * 4) {
+	sourceBlock.pixelPtr = attemptckalloc(sourceBlock.height 
+	    * sourceBlock.pitch);
+	if (sourceBlock.pixelPtr == NULL) {
+	    if (interp != NULL) {
+		Tcl_SetObjResult(interp, Tcl_NewStringObj(
+			TK_PHOTO_ALLOC_FAILURE_MESSAGE, -1));
+		Tcl_SetErrorCode(interp, "TK", "MALLOC", NULL);
+	    }
+	    return TCL_ERROR;
+	}
+	memToFree = sourceBlock.pixelPtr;
+	memcpy(sourceBlock.pixelPtr, blockPtr->pixelPtr, sourceBlock.height 
+	    * sourceBlock.pitch);
+    }
 
     xEnd = x + width;
     yEnd = y + height;
     if ((xEnd > masterPtr->width) || (yEnd > masterPtr->height)) {
-	int sameSrc = (blockPtr->pixelPtr == masterPtr->pix32);
-
 	if (ImgPhotoSetSize(masterPtr, MAX(xEnd, masterPtr->width),
 		MAX(yEnd, masterPtr->height)) == TCL_ERROR) {
 	    if (interp != NULL) {
@@ -3133,11 +3203,7 @@
 			TK_PHOTO_ALLOC_FAILURE_MESSAGE, -1));
 		Tcl_SetErrorCode(interp, "TK", "MALLOC", NULL);
 	    }
-	    return TCL_ERROR;
-	}
-	if (sameSrc) {
-	    blockPtr->pixelPtr = masterPtr->pix32;
-	    blockPtr->pitch = masterPtr->width * 4;
+	    goto errorExit;
 	}
     }
 
@@ -3156,14 +3222,14 @@
      * components, mark it as a color image.
      */
 
-    greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
-    blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
-    alphaOffset = blockPtr->offset[3];
-    if ((alphaOffset >= blockPtr->pixelSize) || (alphaOffset < 0)) {
+    greenOffset = sourceBlock.offset[1] - sourceBlock.offset[0];
+    blueOffset = sourceBlock.offset[2] - sourceBlock.offset[0];
+    alphaOffset = sourceBlock.offset[3];
+    if ((alphaOffset >= sourceBlock.pixelSize) || (alphaOffset < 0)) {
 	alphaOffset = 0;
 	sourceIsSimplePhoto = 1;
     } else {
-	alphaOffset -= blockPtr->offset[0];
+	alphaOffset -= sourceBlock.offset[0];
     }
     if ((greenOffset != 0) || (blueOffset != 0)) {
 	masterPtr->flags |= COLOR_IMAGE;
@@ -3174,21 +3240,21 @@
      * subsampling and zooming.
      */
 
-    blockXSkip = subsampleX * blockPtr->pixelSize;
-    blockYSkip = subsampleY * blockPtr->pitch;
+    blockXSkip = subsampleX * sourceBlock.pixelSize;
+    blockYSkip = subsampleY * sourceBlock.pitch;
     if (subsampleX > 0) {
-	blockWid = ((blockPtr->width + subsampleX - 1) / subsampleX) * zoomX;
+	blockWid = ((sourceBlock.width + subsampleX - 1) / subsampleX) * zoomX;
     } else if (subsampleX == 0) {
 	blockWid = width;
     } else {
-	blockWid = ((blockPtr->width - subsampleX - 1) / -subsampleX) * zoomX;
+	blockWid = ((sourceBlock.width - subsampleX - 1) / -subsampleX) * zoomX;
     }
     if (subsampleY > 0) {
-	blockHt = ((blockPtr->height + subsampleY - 1) / subsampleY) * zoomY;
+	blockHt = ((sourceBlock.height + subsampleY - 1) / subsampleY) * zoomY;
     } else if (subsampleY == 0) {
 	blockHt = height;
     } else {
-	blockHt = ((blockPtr->height - subsampleY - 1) / -subsampleY) * zoomY;
+	blockHt = ((sourceBlock.height - subsampleY - 1) / -subsampleY) * zoomY;
     }
 
     /*
@@ -3196,12 +3262,12 @@
      */
 
     destLinePtr = masterPtr->pix32 + (y * masterPtr->width + x) * 4;
-    srcOrigPtr = blockPtr->pixelPtr + blockPtr->offset[0];
+    srcOrigPtr = sourceBlock.pixelPtr + sourceBlock.offset[0];
     if (subsampleX < 0) {
-	srcOrigPtr += (blockPtr->width - 1) * blockPtr->pixelSize;
+	srcOrigPtr += (sourceBlock.width - 1) * sourceBlock.pixelSize;
     }
     if (subsampleY < 0) {
-	srcOrigPtr += (blockPtr->height - 1) * blockPtr->pitch;
+	srcOrigPtr += (sourceBlock.height - 1) * sourceBlock.pitch;
     }
 
     pitch = masterPtr->width * 4;
@@ -3351,7 +3417,15 @@
 
     Tk_ImageChanged(masterPtr->tkMaster, x, y, width, height, masterPtr->width,
 	    masterPtr->height);
+    
+    ckfree(memToFree);
+    
     return TCL_OK;
+    
+  errorExit:
+    ckfree(memToFree);
+    
+    return TCL_ERROR;
 }
 
 /*
diff -ur tk8.6.6-orig/tests/imgBmap.test tk8.6.6/tests/imgBmap.test
--- tk8.6.6-orig/tests/imgBmap.test	2017-02-12 14:17:54.037132462 +0100
+++ tk8.6.6/tests/imgBmap.test	2017-02-17 15:29:52.580201056 +0100
@@ -380,7 +380,8 @@
 test imageBmap-7.10 {ImgBmapCmd procedure} -body {
     i1 gorp
 } -returnCodes error -result {bad option "gorp": must be cget or configure}
-
+# Clean it up after use!!
+imageCleanup
 
 test imageBmap-8.1 {ImgBmapGet/Free procedures, shared instances} -setup {
     destroy .c
diff -ur tk8.6.6-orig/tests/imgPhoto.test tk8.6.6/tests/imgPhoto.test
--- tk8.6.6-orig/tests/imgPhoto.test	2017-02-12 14:17:54.040465812 +0100
+++ tk8.6.6/tests/imgPhoto.test	2017-02-17 15:11:01.414619748 +0100
@@ -936,6 +936,44 @@
     photo1 put "{#00ff00 #00ff00}" -to 2 0
     list [photo1 get 2 0] [photo1 get 3 0] [photo1 get 4 0]
 } -result {{0 255 0} {0 255 0} {255 0 0}}
+test imgPhoto-10.2 {Tk_ImgPhotoPutBlock, same source and dest img} -constraints {
+    hasTeapotPhoto
+} -setup {
+    imageCleanup
+} -body {
+    # Test for bug e4336bef5d
+    image create photo photo1 -file $teapotPhotoFile
+    image create photo photo2 -file $teapotPhotoFile
+    photo2 copy photo1 -to 1 2
+    photo1 copy photo1 -to 1 2
+    string equal [photo1 data] [photo2 data]
+} -cleanup {
+    imageCleanup
+} -result {1}
+test imgPhoto-10.3 {Tk_ImgPhotoPutBlock, same source and dest img} -constraints {
+    hasTeapotPhoto
+} -setup {
+    imageCleanup
+} -body {
+    # Test for bug e4336bef5d
+    image create photo photo1 -file $teapotPhotoFile
+    image create photo photo2 -file $teapotPhotoFile
+    photo2 copy photo1 -from 2 1 -to 4 5 300 300
+    photo1 copy photo1 -from 2 1 -to 4 5 300 300
+    string equal [photo1 data] [photo2 data]
+} -cleanup {
+    imageCleanup
+} -result {1}
+test imgPhoto-10.4 {Tk_ImgPhotoPutBlock, empty image} -setup {
+    imageCleanup
+} -body {
+    image create photo photo1
+    photo1 copy photo1 -to 0 5 10 20
+    list [image width photo1] [image height photo1]
+} -cleanup {
+    imageCleanup
+} -result {0 0}
+
 
 test imgPhoto-11.1 {Tk_FindPhoto} -setup {
     imageCleanup
@@ -955,6 +993,41 @@
 } -cleanup {
     image delete p3
 } -result {{19 92 192} {169 117 90} 512 512 {19 92 192}}
+test imgPhoto-12.2 {Tk_ImgPhotoPutZoomedBlock, same source and dest img} -constraints {
+    hasTeapotPhoto
+} -setup {
+    imageCleanup
+} -body {
+    # Test for bug e4336bef5d
+    image create photo photo1 -file $teapotPhotoFile
+    image create photo photo2 -file $teapotPhotoFile
+    photo2 copy photo1 -to 0 1 200 200 -zoom 2 3
+    photo1 copy photo1 -to 0 1 200 200 -zoom 2 3
+    string equal [photo1 data] [photo2 data]
+} -cleanup {
+    imageCleanup
+} -result {1}
+test imgPhoto-12.3 {Tk_ImgPhotoPutZoomedBlock, same source and dest img} -setup {
+    imageCleanup
+} -body {
+    # Test for bug e4336bef5d
+    image create photo photo1 -file $teapotPhotoFile
+    image create photo photo2 -file $teapotPhotoFile
+    photo2 copy photo1 -from 1 0 -to 4 5 300 300 -zoom 1 2
+    photo1 copy photo1 -from 1 0 -to 4 5 300 300 -zoom 1 2
+    string equal [photo1 data] [photo2 data]
+} -cleanup {
+    imageCleanup
+} -result {1}
+test imgPhoto-12.4 {Tk_ImgPhotoPutZoomedBlock, empty image} -setup {
+    imageCleanup
+} -body {
+    image create photo photo1
+    photo1 copy photo1 -to 0 5 10 20
+    list [image width photo1] [image height photo1]
+} -cleanup {
+    imageCleanup
+} -result {0 0}
 
 test imgPhoto-13.1 {check separation of images in different interpreters} -setup {
     imageCleanup