Skip to content
Tcl Source Code View Ticket
Login

View Ticket

crash for "fconfigure stdout" in Win32

Closed / Fixede87000d8425ab86af682dd8195b9d20826974ba8Created 6 years agoUpdated 6 years ago
Descriptionplain2020-08-11 11:41:36
When doing a "fconfigure stdout" or "chan configure stdout" on Win32
console channels, an immediate crash can happen. A potential fix is:

---8><------8><------8><------8><------8><------8><------8><------
Index: win/tclWinConsole.c
==================================================================
--- win/tclWinConsole.c
+++ win/tclWinConsole.c
@@ -1637,19 +1637,23 @@
     }
 
     if (valid) {
        return TCL_OK;
     }
-    if (infoPtr->flags & CONSOLE_READ_OPS) {
-       return Tcl_BadChannelOption(interp, optionName, "inputmode winsize");
-    } else {
-       return Tcl_BadChannelOption(interp, optionName, "");
+    if (len > 0) {
+       const char *optionList = "";
+
+       if (infoPtr->flags & CONSOLE_READ_OPS) {
+           optionList = "inputmode winsize";
+       }
+       return Tcl_BadChannelOption(interp, optionName, optionList);
     }
+    return TCL_OK;
 }
 ^L
 /*
  * Local Variables:
  * mode: c
  * c-basic-offset: 4
  * fill-column: 78
  * End:
  */

---8><------8><------8><------8><------8><------8><------8><------
Referenced by2
jan.nijtmans Ticket [e87000d842]: 3 changes [History diff]2020-08-11 11:58:21
jan.nijtmansplain2020-08-12 07:06:26
Fixed now in core-8-branch. (btw. "winsize" is an androwish extension).

Thanks!
chwplain2020-08-12 09:03:16
No, it is defined per tip#160 as can be seen in this check-in in core-8-branch:

https://core.tcl-lang.org/tcl/blame?filename=win/tclWinConsole.c&checkin=42f24469aa
jan.nijtmanswiki2020-08-12 10:32:46

>No, it is defined per tip#160

Indeed, which means that my fix was wrong (applied to the wrong lines in tclWinConsole.c. The actual problem was in Tcl\_BadChannelOption() which didn't protect against NULL argument. Should be fixed better now.

-winsize is a read-only option, that's what caused the confusion.

Thanks!

chwplain2020-08-12 11:39:27
I think the problem is somewhat different. In the Win32 console case
it starts with the (wrong) assumption, that the window size can be
determined on stdin (the input handle from GetStandardHandle(...)).
However, that information can only be read out from a console handle
with a screen buffer, i.e stdout if obtained from GetStandardHandle().

A proof of concept which properly supports -winsize is in the AndroWish
repo, see 

https://www.androwish.org/home/file?name=jni/tcl/win/tclWinConsole.c&ci=tip

Regarding the crash: if you look at the various implementations of
GetOptionsProc() most set a valid flag even in the optionName = NULL
case and return earlier than Tcl_BadChannelOption(). Others deal with
len > 0 (in case optionName is NULL, len is 0) and if len is zero don't
call Tcl_BadChannelOption() at all.
Log in (anonymous is fine) to add a comment.