Index: generic/tclUtf.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclUtf.c,v
retrieving revision 1.35
diff -u -r1.35 tclUtf.c
--- generic/tclUtf.c	21 Jul 2005 14:38:51 -0000	1.35
+++ generic/tclUtf.c	7 Sep 2005 14:29:06 -0000
@@ -168,45 +168,47 @@
 	buf[0] = (char) ch;
 	return 1;
     }
-    if (ch <= 0x7FF) {
-	buf[1] = (char) ((ch | 0x80) & 0xBF);
-	buf[0] = (char) ((ch >> 6) | 0xC0);
-	return 2;
-    }
-    if (ch <= 0xFFFF) {
+    if (ch >= 0) {
+	if (ch <= 0x7FF) {
+	    buf[1] = (char) ((ch | 0x80) & 0xBF);
+	    buf[0] = (char) ((ch >> 6) | 0xC0);
+	    return 2;
+	}
+	if (ch <= 0xFFFF) {
 	three:
-	buf[2] = (char) ((ch | 0x80) & 0xBF);
-	buf[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	buf[0] = (char) ((ch >> 12) | 0xE0);
-	return 3;
-    }
+	    buf[2] = (char) ((ch | 0x80) & 0xBF);
+	    buf[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    buf[0] = (char) ((ch >> 12) | 0xE0);
+	    return 3;
+	}
 
 #if TCL_UTF_MAX > 3
-    if (ch <= 0x1FFFFF) {
-	buf[3] = (char) ((ch | 0x80) & 0xBF);
-	buf[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	buf[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
-	buf[0] = (char) ((ch >> 18) | 0xF0);
-	return 4;
-    }
-    if (ch <= 0x3FFFFFF) {
-	buf[4] = (char) ((ch | 0x80) & 0xBF);
-	buf[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	buf[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
-	buf[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
-	buf[0] = (char) ((ch >> 24) | 0xF8);
-	return 5;
-    }
-    if (ch <= 0x7FFFFFFF) {
-	buf[5] = (char) ((ch | 0x80) & 0xBF);
-	buf[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	buf[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
-	buf[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
-	buf[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
-	buf[0] = (char) ((ch >> 30) | 0xFC);
-	return 6;
-    }
+	if (ch <= 0x1FFFFF) {
+	    buf[3] = (char) ((ch | 0x80) & 0xBF);
+	    buf[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    buf[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
+	    buf[0] = (char) ((ch >> 18) | 0xF0);
+	    return 4;
+	}
+	if (ch <= 0x3FFFFFF) {
+	    buf[4] = (char) ((ch | 0x80) & 0xBF);
+	    buf[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    buf[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
+	    buf[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
+	    buf[0] = (char) ((ch >> 24) | 0xF8);
+	    return 5;
+	}
+	if (ch <= 0x7FFFFFFF) {
+	    buf[5] = (char) ((ch | 0x80) & 0xBF);
+	    buf[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    buf[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
+	    buf[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
+	    buf[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
+	    buf[0] = (char) ((ch >> 30) | 0xFC);
+	    return 6;
+	}
 #endif
+    }
 
     ch = 0xFFFD;
     goto three;
Index: tests/utf.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/utf.test,v
retrieving revision 1.12
diff -u -r1.12 utf.test
--- tests/utf.test	8 Oct 2003 15:24:21 -0000	1.12
+++ tests/utf.test	7 Sep 2005 14:29:08 -0000
@@ -29,6 +29,9 @@
 test utf-1.4 {Tcl_UniCharToUtf: 3 byte sequences} {
     set x "\u4e4e"
 } [bytestring "\xe4\xb9\x8e"]
+test utf-1.5 {Tcl_UniCharToUtf: negative Tcl_UniChar} {
+    string length [format %c -1]
+} 1
 
 test utf-2.1 {Tcl_UtfToUniChar: low ascii} {
     string length "abc"
