From 01a22f1c5755b9d734b8aa885b738b0054f45185 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 28 Sep 2023 15:23:51 +0000 Subject: [PATCH 1/2] Proposed fix for [0df5906dd7] --- generic/tclMain.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/generic/tclMain.c b/generic/tclMain.c index 628deaab993e..906c197e0012 100644 --- a/generic/tclMain.c +++ b/generic/tclMain.c @@ -29,6 +29,7 @@ */ static const char DEFAULT_PRIMARY_PROMPT[] = "% "; +static const char ENCODING_ERROR[] = "\n\t(encoding error in stderr)"; /* * This file can be compiled on Windows in UNICODE mode, as well as on all @@ -249,7 +250,9 @@ Tcl_SourceRCFile( if (Tcl_EvalFile(interp, fullName) != TCL_OK) { chan = Tcl_GetStdChannel(TCL_STDERR); if (chan) { - Tcl_WriteObj(chan, Tcl_GetObjResult(interp)); + if (Tcl_WriteObj(chan, Tcl_GetObjResult(interp)) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } } @@ -375,7 +378,9 @@ Tcl_MainEx( if (chan) { Tcl_WriteChars(chan, "application-specific initialization failed: ", -1); - Tcl_WriteObj(chan, Tcl_GetObjResult(interp)); + if (Tcl_WriteObj(chan, Tcl_GetObjResult(interp)) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } } @@ -415,7 +420,9 @@ Tcl_MainEx( Tcl_DecrRefCount(keyPtr); if (valuePtr) { - Tcl_WriteObj(chan, valuePtr); + if (Tcl_WriteObj(chan, valuePtr) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } } Tcl_WriteChars(chan, "\n", 1); Tcl_DecrRefCount(options); @@ -528,7 +535,9 @@ Tcl_MainEx( if (code != TCL_OK) { chan = Tcl_GetStdChannel(TCL_STDERR); if (chan) { - Tcl_WriteObj(chan, Tcl_GetObjResult(interp)); + if (Tcl_WriteObj(chan, Tcl_GetObjResult(interp)) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } } else if (is.tty) { @@ -537,7 +546,9 @@ Tcl_MainEx( (void)Tcl_GetStringFromObj(resultPtr, &length); chan = Tcl_GetStdChannel(TCL_STDOUT); if ((length > 0) && chan) { - Tcl_WriteObj(chan, resultPtr); + if (Tcl_WriteObj(chan, resultPtr) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } Tcl_DecrRefCount(resultPtr); @@ -802,7 +813,9 @@ StdinProc( chan = Tcl_GetStdChannel(TCL_STDERR); if (chan != NULL) { - Tcl_WriteObj(chan, Tcl_GetObjResult(interp)); + if (Tcl_WriteObj(chan, Tcl_GetObjResult(interp)) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } } else if (isPtr->tty) { @@ -812,7 +825,9 @@ StdinProc( Tcl_IncrRefCount(resultPtr); (void)Tcl_GetStringFromObj(resultPtr, &length); if ((length > 0) && (chan != NULL)) { - Tcl_WriteObj(chan, resultPtr); + if (Tcl_WriteObj(chan, resultPtr) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } Tcl_DecrRefCount(resultPtr); @@ -883,7 +898,9 @@ Prompt( "\n (script that generates prompt)"); chan = Tcl_GetStdChannel(TCL_STDERR); if (chan != NULL) { - Tcl_WriteObj(chan, Tcl_GetObjResult(interp)); + if (Tcl_WriteObj(chan, Tcl_GetObjResult(interp)) < 0) { + Tcl_WriteChars(chan, ENCODING_ERROR, -1); + } Tcl_WriteChars(chan, "\n", 1); } goto defaultPrompt; From 54252490a8ab860e77ed8e49b440755a97251b50 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 28 Sep 2023 18:15:37 +0000 Subject: [PATCH 2/2] Handle ::tcl::Bgerror the same way --- generic/tclEvent.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/generic/tclEvent.c b/generic/tclEvent.c index 541e70876287..0cf0df116ea8 100644 --- a/generic/tclEvent.c +++ b/generic/tclEvent.c @@ -97,6 +97,8 @@ static int inExit = 0; static int subsystemsInitialized = 0; +static const char ENCODING_ERROR[] = "\n\t(encoding error in stderr)"; + /* * This variable contains the application wide exit handler. It will be called * by Tcl_Exit instead of the C-runtime exit if this variable is set to a @@ -294,9 +296,13 @@ HandleBgErrors( Tcl_WriteChars(errChannel, "error in background error handler:\n", -1); if (valuePtr) { - Tcl_WriteObj(errChannel, valuePtr); + if (Tcl_WriteObj(errChannel, valuePtr) < 0) { + Tcl_WriteChars(errChannel, ENCODING_ERROR, -1); + } } else { - Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp)); + if (Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp)) < 0) { + Tcl_WriteChars(errChannel, ENCODING_ERROR, -1); + } } Tcl_WriteChars(errChannel, "\n", 1); Tcl_Flush(errChannel); @@ -483,18 +489,22 @@ TclDefaultBgErrorHandlerObjCmd( if (Tcl_FindCommand(interp, "bgerror", NULL, TCL_GLOBAL_ONLY) == NULL) { Tcl_RestoreInterpState(interp, saved); - Tcl_WriteObj(errChannel, Tcl_GetVar2Ex(interp, - "errorInfo", NULL, TCL_GLOBAL_ONLY)); + if (Tcl_WriteObj(errChannel, Tcl_GetVar2Ex(interp, + "errorInfo", NULL, TCL_GLOBAL_ONLY)) < 0) { + Tcl_WriteChars(errChannel, ENCODING_ERROR, -1); + } Tcl_WriteChars(errChannel, "\n", -1); } else { Tcl_DiscardInterpState(saved); - Tcl_WriteChars(errChannel, - "bgerror failed to handle background error.\n", -1); - Tcl_WriteChars(errChannel, " Original error: ", -1); - Tcl_WriteObj(errChannel, tempObjv[1]); - Tcl_WriteChars(errChannel, "\n", -1); - Tcl_WriteChars(errChannel, " Error in bgerror: ", -1); - Tcl_WriteObj(errChannel, resultPtr); + Tcl_WriteChars(errChannel, "bgerror failed to handle" + " background error.\n Original error: ", -1); + if (Tcl_WriteObj(errChannel, tempObjv[1]) < 0) { + Tcl_WriteChars(errChannel, ENCODING_ERROR, -1); + } + Tcl_WriteChars(errChannel, "\n Error in bgerror: ", -1); + if (Tcl_WriteObj(errChannel, resultPtr) < 0) { + Tcl_WriteChars(errChannel, ENCODING_ERROR, -1); + } Tcl_WriteChars(errChannel, "\n", -1); } Tcl_DecrRefCount(resultPtr);