summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorapnadkarni <apnmbx-wits@yahoo.com>2023-07-05 03:23:43 (GMT)
committerapnadkarni <apnmbx-wits@yahoo.com>2023-07-05 03:23:43 (GMT)
commitb49afc700b71491f3ae17396d4ce473d4b067712 (patch)
treefdd48c0873f6225b68aef5415f0b836b9a9bbd49
parent3d2de6ed070b76c6a985c1cfe0be7a50fb12b650 (diff)
downloadtcl-b49afc700b71491f3ae17396d4ce473d4b067712.zip
tcl-b49afc700b71491f3ae17396d4ce473d4b067712.tar.gz
tcl-b49afc700b71491f3ae17396d4ce473d4b067712.tar.bz2
Bug [5be203d6ca] - better fix and add test
-rw-r--r--generic/tclEncoding.c20
-rw-r--r--tests/utfext.test8
2 files changed, 14 insertions, 14 deletions
diff --git a/generic/tclEncoding.c b/generic/tclEncoding.c
index 42fcb7f..405c179 100644
--- a/generic/tclEncoding.c
+++ b/generic/tclEncoding.c
@@ -10,6 +10,7 @@
*/
#include "tclInt.h"
+#include <assert.h>
typedef size_t (LengthProc)(const char *src);
@@ -3397,16 +3398,13 @@ TableToUtfProc(
}
byte = *((unsigned char *) src);
if (prefixBytes[byte]) {
- src++;
- if (src >= srcEnd) {
+ if (src >= srcEnd-1) {
+ /* Prefix byte but nothing after it */
if (!(flags & TCL_ENCODING_END)) {
- /* Suffix bytes expected, don't consume prefix */
- src--;
+ /* More data to come */
result = TCL_CONVERT_MULTIBYTE;
break;
} else if (PROFILE_STRICT(flags)) {
- /* Truncation. Do not consume so error location correct */
- src--;
result = TCL_CONVERT_SYNTAX;
break;
} else if (PROFILE_REPLACE(flags)) {
@@ -3415,6 +3413,7 @@ TableToUtfProc(
ch = (unsigned) byte;
}
} else {
+ ++src;
ch = toUnicode[byte][*((unsigned char *)src)];
}
} else {
@@ -3448,14 +3447,7 @@ TableToUtfProc(
src++;
}
- if (src > srcEnd) {
- /*
- * This can happen in the case of a trailing truncated byte sequence
- * where src is incremented past the prefix byte which is the last.
- * See bug [5be203d6ca]
- */
- src--;
- }
+ assert(src <= srcEnd);
*srcReadPtr = src - srcStart;
*dstWrotePtr = dst - dstStart;
*dstCharsPtr = numChars;
diff --git a/tests/utfext.test b/tests/utfext.test
index bc996c9..a6c9dd8 100644
--- a/tests/utfext.test
+++ b/tests/utfext.test
@@ -93,6 +93,14 @@ test xx-bufferoverflow {buffer overflow Tcl_ExternalToUtf} -body {
# % testencoding Tcl_ExternalToUtf utf-8 abcdefgh {start end noterminate charlimit} {} 20 rv wv cv
# nospace {} abcÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
+test TableToUtf-bug-5be203d6ca {Bug 5be203d6ca - truncated prefix in table encoding} -body {
+ set src \x82\x4f\x82\x50\x82
+ lassign [testencoding Tcl_ExternalToUtf shiftjis $src {start} 0 16 srcRead dstWritten charsWritten] buf
+ set result [list [testencoding Tcl_ExternalToUtf shiftjis $src {start} 0 16 srcRead dstWritten charsWritten] $srcRead $dstWritten $charsWritten]
+ lappend result {*}[list [testencoding Tcl_ExternalToUtf shiftjis [string range $src $srcRead end] {end} 0 10 srcRead dstWritten charsWritten] $srcRead $dstWritten $charsWritten]
+} -result [list [list multibyte 0 \xEF\xBC\x90\xEF\xBC\x91\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF] 4 6 2 [list ok 0 \xC2\x82\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF] 1 2 1]
+
+
::tcltest::cleanupTests
return