summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpooryorick <com.digitalsmarties@pooryorick.com>2023-04-25 20:34:03 (GMT)
committerpooryorick <com.digitalsmarties@pooryorick.com>2023-04-25 20:34:03 (GMT)
commit76130df46050131c3a1c4ec22d6adbfaa637f2a7 (patch)
tree2f0666ba0ac12b5d3dd0368cdbdc86073d55aef7
parent025b74f7c7add01c5ca6654b03c29241c0845def (diff)
downloadtcl-76130df46050131c3a1c4ec22d6adbfaa637f2a7.zip
tcl-76130df46050131c3a1c4ec22d6adbfaa637f2a7.tar.gz
tcl-76130df46050131c3a1c4ec22d6adbfaa637f2a7.tar.bz2
Fix for issue [f5eadcbf9a], passing pointer to uninitialized memory leads
Tcl_UniCharToUtf() to corrupt data.
-rw-r--r--generic/tclEncoding.c43
-rw-r--r--generic/tclStringObj.c6
-rw-r--r--generic/tclUtf.c16
3 files changed, 65 insertions, 0 deletions
diff --git a/generic/tclEncoding.c b/generic/tclEncoding.c
index 2b8e8c0..851ae64 100644
--- a/generic/tclEncoding.c
+++ b/generic/tclEncoding.c
@@ -2532,6 +2532,14 @@ UtfToUtfProc(
flags |= PTR2INT(clientData);
dstEnd = dst + dstLen - ((flags & ENCODING_UTF) ? TCL_UTF_MAX : 6);
+
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, dstLen);
+
profile = CHANNEL_PROFILE_GET(flags);
for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) {
@@ -2746,6 +2754,13 @@ Utf32ToUtfProc(
}
result = TCL_OK;
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, dstLen);
+
/*
* Check alignment with utf-32 (4 == sizeof(UTF-32))
*/
@@ -3015,6 +3030,13 @@ Utf16ToUtfProc(
}
result = TCL_OK;
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, dstLen);
+
/*
* Check alignment with utf-16 (2 == sizeof(UTF-16))
*/
@@ -3428,6 +3450,13 @@ TableToUtfProc(
dstStart = dst;
dstEnd = dst + dstLen - TCL_UTF_MAX;
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, dstLen);
+
toUnicode = (const unsigned short *const *) dataPtr->toUnicode;
prefixBytes = dataPtr->prefixBytes;
pageZero = toUnicode[0];
@@ -3669,6 +3698,13 @@ Iso88591ToUtfProc(
dstStart = dst;
dstEnd = dst + dstLen - TCL_UTF_MAX;
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, dstLen);
+
result = TCL_OK;
for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) {
Tcl_UniChar ch = 0;
@@ -3908,6 +3944,13 @@ EscapeToUtfProc(
dstStart = dst;
dstEnd = dst + dstLen - TCL_UTF_MAX;
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, dstLen);
+
state = PTR2INT(*statePtr);
if (flags & TCL_ENCODING_START) {
state = 0;
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c
index fb7294b..3b1f0fb 100644
--- a/generic/tclStringObj.c
+++ b/generic/tclStringObj.c
@@ -4497,6 +4497,12 @@ ExtendStringRepWithUnicode(
copyBytes:
dst = objPtr->bytes + origLength;
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(dst, 0, stringPtr->allocated - origLength);
for (i = 0; i < numChars; i++) {
dst += Tcl_UniCharToUtf(unicode[i], dst);
}
diff --git a/generic/tclUtf.c b/generic/tclUtf.c
index cc5769f..42d2bea 100644
--- a/generic/tclUtf.c
+++ b/generic/tclUtf.c
@@ -348,6 +348,14 @@ Tcl_UniCharToUtfDString(
p = string;
wEnd = uniStr + uniLength;
+
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Once TCL_UTF_MAX == 3 is removed and Tcl_UniCharToUtf restored to its
+ * prior non-stateful nature, this call to memset can also be removed.
+ */
+ memset(p, 0, Tcl_DStringLength(dsPtr) - oldLength);
+
for (w = uniStr; w < wEnd; ) {
p += Tcl_UniCharToUtf(*w, p);
w++;
@@ -391,6 +399,14 @@ Tcl_Char16ToUtfDString(
p = string;
wEnd = uniStr + uniLength;
+
+ /* Initialize the buffer so that some random data doesn't trick
+ * Tcl_UniCharToUtf() into thinking it should combine surrogate pairs.
+ * Because TCL_COMBINE is used here, memset() is required even when
+ * TCL_UTF_MAX == 4.
+ */
+ memset(p, 0, Tcl_DStringLength(dsPtr) - oldLength);
+
for (w = uniStr; w < wEnd; ) {
if (!len && ((*w & 0xFC00) != 0xDC00)) {
/* Special case for handling high surrogates. */