Tk Source Code

View Ticket
Login
Ticket UUID: a0241c0e257c3206d711ebfefbfaa76ff87bf18e
Title: photo image copy command can unexpectedly hang for 20+ seconds
Type: Bug Version: 8.6
Submitter: dusthillresident Created on: 2024-06-07 22:04:24
Subsystem: 41. Photo Images Assigned To: fvogel
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2024-06-09 12:39:08
Resolution: Fixed Closed By: fvogel
    Closed on: 2024-06-09 12:39:08
Description:

Hello,

With Tcl/Tk 8.6.12 on Linux and Windows (the latest version I have access to on both systems at the moment),

with the 'photo' images,

some image copy commands including both -to and -from options can hang up for very long amounts of time, up to 40 seconds on my system (Ryzen 5 3600, not a slow computer). It looks like it might be caused by out of bounds values being passed to the -from option.

Expectation: If the values passed to the options are incorrect, it should fail and raise an error, rather than attempt to do something that doesn't make sense and hang up for ~40 seconds.

Further details:

I am able to replicate it very reliably, but I'm not precisely sure what the trigger is. I feel like it seems to have something to do with the -from option with x,y values that are out of bounds (larger than the dimensions of the source image) but I am unsure.

Here is a test harness program that (on my system and in my testing) replicates the behaviour very reliably. Click and hold in the box, and slowly drag your mouse to the left, and then the app should freeze up, and after ~40 seconds, you should see a message in the terminal reporting how many microseconds it was hung up for.

package require Tk

proc plotImg {img src x y hflip vflip} {
 set w [image width $src]
 set h [image height $src]
 set bw [image width $img]
 set bh [image height $img]
 set xx 0
 set yy 0
 if {$x>=$bw || $y>=$bh} return
 if {$x<0} {incr w $x; set xx [expr {-$x}]; set x 0}
 if {$y<0} {incr h $y; set yy [expr {-$y}]; set y 0}
 if {$w<=0 || $h<=0} return
 if {$x+$w>$bw} {incr w [expr {-($x+$w-$bw)}]}
 if {$y+$h>$bh} {incr h [expr {-($y+$h-$bh)}]}
 if { $hflip || $vflip } {
  set tempimg [image create photo -width $w -height $h]
  $tempimg copy $src -subsamp [expr {$hflip ? -1 : 1}] [expr {$vflip ? -1 : 1}]
  # The bug is happening here. 
  set cmd "$img copy $tempimg -to $x $y [expr {$x+$w}] [expr {$y+$h}] -from $xx $yy"
  puts "running command: $cmd"
  puts [time $cmd]
  puts "command completed\n"
  image delete $tempimg
 } else {
  $img copy $src -to $x $y [expr {$x+$w}] [expr {$y+$h}] -from $xx $yy
 }
}


image create photo ball -width 20 -height 20
for {set x -9} {$x<=9} {incr x} {
 for {set y -9} {$y<=9} {incr y} {
  if {$x*$x+$y*$y<100} {
   ball put lightblue -to [expr {10+$x}] [expr {10+$y}]
  }
 }
}

image create photo testimg -width 150 -height 150
#testimg put black -to 0 0 640 480

pack [label .instruct -text "Click and hold in the centre of the box,\nand then drag your mouse pointer slowly to the left until the ball vanishes.\nNote that the window has become unresponsive (verify this by trying to use the 'Test Button').\nCheck in the terminal, sometimes after an amount of time has passed, the command returns after 20 seconds or so."]
pack [button .b -text "Test Button" -command {tk_messageBox -title "Hi" -message "Hi"}]
label .l -image testimg -relief solid -borderwidth 1 -background darkblue -cursor none
pack .l
bind .l <Motion> {
 testimg blank
 plotImg testimg ball %x %y   1 1
}
User Comments: fvogel added on 2024-06-09 12:39:08:
Merged.

fvogel added on 2024-06-08 09:10:13:

Now done. This is the correct fix, the code was checking twice the same thing by error.


fvogel added on 2024-06-08 09:00:56:

An alternative would be to refine the check here and spit the adequate error. That would be better, I'll have a look.


fvogel added on 2024-06-08 08:52:41:

I have added a new test, [25c8602c|imgPhoto-12.5], demonstrating the problem.

What is happening is that the programmer tries to copy a photo image into another photo image, and the area to copy is outside the limits of the source image.

The code does not detect this case, and computes and passes a negative block.width (or block.height) to Tk_PhotoPutZoomedBlock().

Tk_PhotoPutZoomedBlock() in turn transfers this negative width/height to Tk_PhotoPutBlock().

Then, Tk_PhotoPutBlock() iterates on, say, the width with a negative value. This loop has to span the entire range of an int until wLeft overflows, becomes negative and ends the loop! A piece of art...!

So the fix is to not allow this to happen, see [dbd79c00]. Since there is no point in copying from an area outside the limits of the source image, let's just silently do nothing, exactly the same way [3078902] was fixed for zero-sized width/height.

With this change the new test passes in no time, of course.


fvogel added on 2024-06-08 07:21:37:

Thanks for this high quality bug report!

I can reproduce indeed. The code loops for a very long time in Tk_PhotoPutBlock().

A shorter example script is:

package require Tk
image create photo ball -width 20 -height 20
image create photo testimg -width 150 -height 150
set tempimg [image create photo -width 9 -height 20]
$tempimg copy ball
testimg copy $tempimg -to 0 72 9 92 -from 11 0 ; # very long