Tk Source Code

View Ticket
Login
Ticket UUID: 72de53dab222fcf7435acd200023165421a5ee9e
Title: cleanup unreachable code after Tcl_Panic() (tkWinDraw.c)
Type: RFE Version: 8.6.11
Submitter: chrstphrchvz Created on: 2021-05-08 09:02:42
Subsystem: 82. X11 Emulation Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Cosmetic
Status: Closed Last Modified: 2021-05-11 13:21:13
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2021-05-11 13:21:13
Description:

Tcl_Panic() never returns, so the statements after it (tkWinDraw.c line 589, in TkPutImage) are unreachable. Maybe they should be reordered or removed to improve code quality.

    if (!bitmap) {
	Tcl_Panic("Fail to allocate bitmap");
	DeleteDC(dcMem);
    	TkWinReleaseDrawableDC(d, dc, &state);
	return BadValue;
    }

User Comments: jan.nijtmans added on 2021-05-11 13:21:13:

Ok. Merged to trunk now.

Closing


fvogel added on 2021-05-10 17:03:12:
Sounds very reasonable. I'm assigning the ticket to you, Jan, for further processing. Thanks!

jan.nijtmans added on 2021-05-10 07:10:17:

Well, I don't want to introduce too much risk (with 8.7a5 at the doorstep), so merged this now to all branches.

However ... on MacOS TkPutImage() doesn't panic when the image cannot be drawn, it just doesn't draw the image. That's better than panic, and just kill the application. So, why not let Windows behave the same way as MacOS?

Let's not rush this into Tk 8.7a5, but I would be in favor to merge [696a5a0b] eventually. I don't think it's necessary to adapt the callers doing something if the call failed: It will be noticeable on-screen that the image is not drawn, that should be enough.


fvogel added on 2021-05-09 08:47:08:
Sure we could remove the panic and return BadValue. But then we should probably make use of this return value of TkPutImage(), which is currently not used anywhere in Tk (of course: so far TkPutImage() either returned Success or panic'ed). Callers of TkPutImage() currently do not feature the machinery to leave a message in the interp result or otherwise report an error. Is this really worth the effort? I don't think so, and this is perhaps the reason why Tcl_panic() was used instead.

jan.nijtmans added on 2021-05-08 22:59:14:

Sorry for jumping in late. But a Panic is used when the function doesn't have another way to report the error, or when it's impossible to recover from it. Here I see no reason to use panic at all, In this case, I suggest to remove the Panic


chrstphrchvz added on 2021-05-08 21:20:23:

There is an unconditional return at the end of the function, so I don’t see why keeping the unreachable return would be needed to suppress a warning in this case.


fvogel added on 2021-05-08 12:46:49:

Yes, we could reorder, or even remove what is after Tcl_Panic since as you said and as is documented Tcl_Panic does not return (because it calls abort()).

I think removal of the dead code is better since abort() will anyway kill the application and do the cleanup. I was originally slightly wondering however because that dead code was introduced in [251ce02cb8182c7a] which commit log says it adds a panic (which was already there!). But I think this was just a copy/paste from the previous commit log, which really added a panic at another place.

However I think we should keep the return statement to silence possible compiler warnings.

Would you be OK with [67b363a738] as a fix then?