$NetBSD: patch-al,v 1.1 2007/04/06 12:48:42 markd Exp $

--- src/corelib/tools/qstring.cpp
+++ src/corelib/tools/qstring.cpp
@@ -3342,6 +3342,7 @@ QString QString::fromUtf8(const char *st
     result.resize(size); // worst case
     ushort *qch = result.d->data;
     uint uc = 0;
+    uint min_uc = 0;
     int need = 0;
     int error = -1;
     uchar ch;
@@ -3359,6 +3360,12 @@ QString QString::fromUtf8(const char *st
                         ushort low = uc%0x400 + 0xdc00;
                         *qch++ = high;
                         *qch++ = low;
+                    } else if ((uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff) || (uc >= 0xfffe)) {
+			// overlong seqence, UTF16 surrogate or BOM
+                        i = error;
+                        qch = addOne(qch, result);
+                        *qch++ = 0xdbff;
+                        *qch++ = 0xde00 + ((uchar)str[i]);
                     } else {
                         *qch++ = uc;
                     }
@@ -3381,14 +3388,17 @@ QString QString::fromUtf8(const char *st
                 uc = ch & 0x1f;
                 need = 1;
                 error = i;
+                min_uc = 0x80;
             } else if ((ch & 0xf0) == 0xe0) {
                 uc = ch & 0x0f;
                 need = 2;
                 error = i;
+                min_uc = 0x800;
             } else if ((ch&0xf8) == 0xf0) {
                 uc = ch & 0x07;
                 need = 3;
                 error = i;
+                min_uc = 0x10000;
             } else {
                 // Error
                 qch = addOne(qch, result);
