summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjan.nijtmans <jan.nijtmans@noemail.net>2012-02-29 21:17:35 (GMT)
committerjan.nijtmans <jan.nijtmans@noemail.net>2012-02-29 21:17:35 (GMT)
commit1aa7e0c105a1be02fe08ad22b0e8440d7a2f34c5 (patch)
tree73adf39b2338a6a278e999cfa368b35c224c87d3
parent3ea24a35037f84f573ce72b735926dc744196e6d (diff)
parent9253fa7cdc71af53f6b11cabe637f0de5815c51d (diff)
downloadtcl-1aa7e0c105a1be02fe08ad22b0e8440d7a2f34c5.zip
tcl-1aa7e0c105a1be02fe08ad22b0e8440d7a2f34c5.tar.gz
tcl-1aa7e0c105a1be02fe08ad22b0e8440d7a2f34c5.tar.bz2
[Bug 3466099] BOM in Unicode
FossilOrigin-Name: e17fc0e07b10b56f70e42164b20cbb4c570555b8
-rw-r--r--ChangeLog6
-rw-r--r--generic/tclEncoding.c4
-rw-r--r--generic/tclIOUtil.c22
-rw-r--r--tests/source.test15
4 files changed, 41 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 0dbd68c..90f1e18 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2012-02-29 Jan Nijtmans <nijtmans@users.sf.net>
+
+ * generic/tclIOUtil.c: [Bug 3466099] BOM in Unicode
+ * generic/tclEncoding.c:
+ * tests/source.test
+
2012-02-09 Don Porter <dgp@users.sourceforge.net>
* generic/tclStringObj.c: [Bug 3484402] Correct Off-By-One
diff --git a/generic/tclEncoding.c b/generic/tclEncoding.c
index 9432c05..a4a7027 100644
--- a/generic/tclEncoding.c
+++ b/generic/tclEncoding.c
@@ -764,13 +764,13 @@ Tcl_GetEncodingNames(interp)
int
Tcl_SetSystemEncoding(interp, name)
Tcl_Interp *interp; /* Interp for error reporting, if not NULL. */
- CONST char *name; /* The name of the desired encoding, or NULL
+ CONST char *name; /* The name of the desired encoding, or NULL/""
* to reset to default encoding. */
{
Tcl_Encoding encoding;
Encoding *encodingPtr;
- if (name == NULL) {
+ if (!name || !*name) {
Tcl_MutexLock(&encodingMutex);
encoding = defaultEncoding;
encodingPtr = (Encoding *) encoding;
diff --git a/generic/tclIOUtil.c b/generic/tclIOUtil.c
index b54b76b..5a8d022 100644
--- a/generic/tclIOUtil.c
+++ b/generic/tclIOUtil.c
@@ -1755,11 +1755,25 @@ Tcl_FSEvalFile(interp, pathPtr)
* [Bug: 2040]
*/
Tcl_SetChannelOption(interp, chan, "-eofchar", "\32");
- if (Tcl_ReadChars(chan, objPtr, -1, 0) < 0) {
- Tcl_Close(interp, chan);
+ /* Try to read first character of stream, so we can
+ * check for utf-8 BOM to be handled especially.
+ */
+ if (Tcl_ReadChars(chan, objPtr, 1, 0) < 0) {
+ Tcl_Close(interp, chan);
Tcl_AppendResult(interp, "couldn't read file \"",
- Tcl_GetString(pathPtr),
- "\": ", Tcl_PosixError(interp), (char *) NULL);
+ Tcl_GetString(pathPtr), "\": ", Tcl_PosixError(interp), NULL);
+ goto end;
+ }
+ string = Tcl_GetString(objPtr);
+ /*
+ * If first character is not a BOM, append the remaining characters,
+ * otherwise replace them [Bug 3466099].
+ */
+ if (Tcl_ReadChars(chan, objPtr, -1,
+ memcmp(string, "\xef\xbf\xbe", 3)) < 0) {
+ Tcl_Close(interp, chan);
+ Tcl_AppendResult(interp, "couldn't read file \"",
+ Tcl_GetString(pathPtr), "\": ", Tcl_PosixError(interp), NULL);
goto end;
}
if (Tcl_Close(interp, chan) != TCL_OK) {
diff --git a/tests/source.test b/tests/source.test
index 3a1454c..1e5b732 100644
--- a/tests/source.test
+++ b/tests/source.test
@@ -118,6 +118,21 @@ test source-2.6 {source error conditions} -setup {
{couldn't read file "*_non_existent_": no such file or directory} \
{POSIX ENOENT {no such file or directory}}]
+test source-2.7 {utf-8 with BOM} -setup {
+ set sourcefile [makeFile {} source.file]
+ set saveencoding [encoding system]
+} -body {
+ encoding system utf-8
+ set out [open $sourcefile w]
+ puts $out "\ufffeset y new-y"
+ close $out
+ set y old-y
+ source $sourcefile
+ return $y
+} -cleanup {
+ removeFile $sourcefile
+ encoding system $saveencoding
+} -result {new-y}
test source-3.1 {return in middle of source file} -setup {
set sourcefile [makeFile {