Index: generic/tclUtf.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclUtf.c,v
retrieving revision 1.30.2.2
diff -u -r1.30.2.2 tclUtf.c
--- generic/tclUtf.c	8 Oct 2003 14:21:20 -0000	1.30.2.2
+++ generic/tclUtf.c	7 Sep 2005 14:19:26 -0000
@@ -169,45 +169,47 @@
 	str[0] = (char) ch;
 	return 1;
     }
-    if (ch <= 0x7FF) {
-	str[1] = (char) ((ch | 0x80) & 0xBF);
-	str[0] = (char) ((ch >> 6) | 0xC0);
-	return 2;
-    }
-    if (ch <= 0xFFFF) {
+    if (ch >= 0) {
+	if (ch <= 0x7FF) {
+	    str[1] = (char) ((ch | 0x80) & 0xBF);
+	    str[0] = (char) ((ch >> 6) | 0xC0);
+	    return 2;
+	}
+	if (ch <= 0xFFFF) {
 	three:
-	str[2] = (char) ((ch | 0x80) & 0xBF);
-	str[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	str[0] = (char) ((ch >> 12) | 0xE0);
-	return 3;
-    }
+	    str[2] = (char) ((ch | 0x80) & 0xBF);
+	    str[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    str[0] = (char) ((ch >> 12) | 0xE0);
+	    return 3;
+	}
 
 #if TCL_UTF_MAX > 3
-    if (ch <= 0x1FFFFF) {
-	str[3] = (char) ((ch | 0x80) & 0xBF);
-	str[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	str[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
-	str[0] = (char) ((ch >> 18) | 0xF0);
-	return 4;
-    }
-    if (ch <= 0x3FFFFFF) {
-	str[4] = (char) ((ch | 0x80) & 0xBF);
-	str[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	str[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
-	str[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
-	str[0] = (char) ((ch >> 24) | 0xF8);
-	return 5;
-    }
-    if (ch <= 0x7FFFFFFF) {
-	str[5] = (char) ((ch | 0x80) & 0xBF);
-	str[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
-	str[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
-	str[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
-	str[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
-	str[0] = (char) ((ch >> 30) | 0xFC);
-	return 6;
-    }
+	if (ch <= 0x1FFFFF) {
+	    str[3] = (char) ((ch | 0x80) & 0xBF);
+	    str[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    str[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
+	    str[0] = (char) ((ch >> 18) | 0xF0);
+	    return 4;
+	}
+	if (ch <= 0x3FFFFFF) {
+	    str[4] = (char) ((ch | 0x80) & 0xBF);
+	    str[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    str[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
+	    str[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
+	    str[0] = (char) ((ch >> 24) | 0xF8);
+	    return 5;
+	}
+	if (ch <= 0x7FFFFFFF) {
+	    str[5] = (char) ((ch | 0x80) & 0xBF);
+	    str[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
+	    str[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
+	    str[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
+	    str[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
+	    str[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.8.14.4
diff -u -r1.8.14.4 utf.test
--- tests/utf.test	8 Oct 2003 15:24:01 -0000	1.8.14.4
+++ tests/utf.test	7 Sep 2005 14:19:26 -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"
