Skip to content
Tcl Source Code View Ticket
Login

View Ticket

uninitialized value in format-2.18

Closed / Fixedf3cb2a32d60cf1386b08497b85434cf0177e7c6dCreated 4 years agoUpdated 4 years ago
Descriptionplain2023-03-06 17:24:24
A valgrind run exposes trouble in test format-2.18

==19961== Conditional jump or move depends on uninitialised value(s)
==19961==    at 0x5A7CF7: Tcl_UniCharToUtf (tclUtf.c:231)
==19961==    by 0x59952D: ExtendStringRepWithUnicode (tclStringObj.c:4853)
==19961==    by 0x5993C0: UpdateStringOfString (tclStringObj.c:4794)
==19961==    by 0x57025B: Tcl_GetStringFromObj (tclObj.c:1705)
==19961==    by 0x595DBD: Tcl_AppendFormatToObj (tclStringObj.c:2955)
==19961==    by 0x59603F: Tcl_Format (tclStringObj.c:3029)
==19961==    by 0x488590: TclCompileFormatCmd (tclCompCmds.c:3190)
==19961==    by 0x50A4E3: TclAttemptCompileProc (tclEnsemble.c:3298)
==19961==    by 0x4E6873: CompileCmdCompileProc (tclCompile.c:1993)
==19961==    by 0x4E6EE2: CompileCommandTokens (tclCompile.c:2109)
==19961==    by 0x4E745C: TclCompileScript (tclCompile.c:2269)
==19961==    by 0x4E41FE: TclSetByteCodeFromAny (tclCompile.c:838)
==19961==    by 0x5111D6: TclCompileObj (tclExecute.c:1776)
==19961==    by 0x437DC0: TclNREvalObjEx (tclBasic.c:6732)
==19961==    by 0x5868E5: TclNRUplevelObjCmd (tclProc.c:985)
==19961==    by 0x434E1F: Dispatch (tclBasic.c:4997)
==19961==    by 0x434EA7: TclNRRunCallbacks (tclBasic.c:5037)
==19961==    by 0x434773: Tcl_EvalObjv (tclBasic.c:4756)
==19961==    by 0x436DD5: TclEvalEx (tclBasic.c:5913)
==19961==    by 0x559F7D: Tcl_FSEvalFileEx (tclIOUtil.c:1782)
==19961==    by 0x5669A6: Tcl_MainEx (tclMain.c:405)
==19961==    by 0x410188: main (tclAppInit.c:94)
Referenced by5
  • ca0e3d0976fix for [f3cb2a32d6] Add initialization to allocation of string rep buffer to resolve valgrind reports on use of uninitialized memory check-in2023-03-23 07:27:28
  • 24e82d736eCandidate fix for [f3cb2a32d6] Add initialization to allocation of string rep buffer to resolve valgrind reports on use of uninitialized memory check-in2023-03-20 18:45:28
  • 8d939a1c22Additional fix for [f3cb2a32d6]: uninitialized value in format-2.18 This commit causes a lot of breackage .... check-in2023-03-09 21:35:07
  • b01a3c55edFix [f3cb2a32d6]: uninitialized value in format-2.18. Also remove repeated call to Tcl_DStringInit() check-in2023-03-07 07:03:36
  • 05e83f801aProposed fix for [f3cb2a32d6]: uninitialized value in format-2.18 check-in2023-03-06 19:58:53
jan.nijtmans Ticket [f3cb2a32d6]: 3 changes [History diff]2023-03-06 19:39:01
jan.nijtmanswiki2023-03-06 20:55:51

Proposed fix [here]

In Tcl 8.7, if Tcl_UniCharToUtf() is fed a lower surrogate, it checks if the previous Tcl_UniCharToUtf() call produced an upper surrogate. This previous result is stored in dst[0] and dst[1], but in this situation those two bytes are uninitialized. Initializing dst[0] just before the Tcl_UniCharToUtf() should help.

jan.nijtmansplain2023-03-07 07:07:52
Fixed [b01a3c55eda6af6d|] here (for 8.7 only, not needed in 9.0)
dgpplain2023-03-08 17:37:37
This change shifts the failure to a similar problem in
the following test format-2.19:

---- format-2.19 start
==6436== Conditional jump or move depends on uninitialised value(s)
==6436==    at 0x5A7E59: Tcl_UniCharToUtf (tclUtf.c:231)
==6436==    by 0x59968F: ExtendStringRepWithUnicode (tclStringObj.c:4854)
==6436==    by 0x59951B: UpdateStringOfString (tclStringObj.c:4794)
==6436==    by 0x5703B6: Tcl_GetStringFromObj (tclObj.c:1705)
==6436==    by 0x593752: Tcl_AppendObjToObj (tclStringObj.c:1862)
==6436==    by 0x4E59AC: TclWordKnownAtCompileTime (tclCompile.c:1783)
==6436==    by 0x488512: TclCompileFormatCmd (tclCompCmds.c:3170)
==6436==    by 0x50A63E: TclAttemptCompileProc (tclEnsemble.c:3298)
==6436==    by 0x4E69CE: CompileCmdCompileProc (tclCompile.c:1993)
==6436==    by 0x4E703D: CompileCommandTokens (tclCompile.c:2109)
==6436==    by 0x4E75B7: TclCompileScript (tclCompile.c:2269)
==6436==    by 0x4E4359: TclSetByteCodeFromAny (tclCompile.c:838)
==6436==    by 0x511331: TclCompileObj (tclExecute.c:1776)
==6436==    by 0x437F1B: TclNREvalObjEx (tclBasic.c:6732)
==6436==    by 0x586A40: TclNRUplevelObjCmd (tclProc.c:985)
==6436==    by 0x434F7A: Dispatch (tclBasic.c:4997)
==6436==    by 0x435002: TclNRRunCallbacks (tclBasic.c:5037)
==6436==    by 0x4348CE: Tcl_EvalObjv (tclBasic.c:4756)
==6436==    by 0x436F30: TclEvalEx (tclBasic.c:5913)
==6436==    by 0x55A0D8: Tcl_FSEvalFileEx (tclIOUtil.c:1782)
==6436==    by 0x566B01: Tcl_MainEx (tclMain.c:405)
==6436==    by 0x410188: main (tclAppInit.c:94)
==6436== 
++++ format-2.19 PASSED

looking....
dgpplain2023-03-08 19:31:51
In test format-2.19 the access to uninitialized memory happens
when generating the string rep of the argument "%s\uDE02".  The
first two characters are written as single-byte encoded UTF-8
into dst.  Then the third character needs to be written starting
at the 3rd byte of the string rep.  Since the character U+DE02
is a low surrogate, it reads the 3rd and 4th byte to check for
residue of a high surrogate, but those bytes are uninitialized.

Moving the initialization to \0 inside the loop disrupts the
surrogate history mechanism, so that's not an answer.

Maybe a conditional initialization is possible, but getting really
messy with callers of Tcl_UniCharToUtf() burdened with a lot
of specialized knowledge to make the call in ways that reliably
avoid this issue.
dgpplain2023-03-20 18:46:36
Candidate fix on a branch for review.  Adds initialization to the
allocation routine most often at work.
jan.nijtmanswiki2023-03-22 21:24:53

"Candidate fix" looks good to me. Thanks!

Log in (anonymous is fine) to add a comment.