Crash when displaying an (invalid) utf8 string


#1

Take the JUCE demo and add this at the end of the IntroScreen constructor:

 

        String s = String::fromUTF8("foo \xb4 foo");
        DBG("string=" << s.toRawUTF8() << " length=" << s.length() << " isValid:" << CharPointer_UTF8::isValidString(s.toRawUTF8(), s.length()));
        versionLabel.setText(s, dontSendNotification);

 

On the mac, it will crash in OSXTypeFace::getGlyphposition. Apparently it is because s.toCFString returns a null CFStringRef. I agree that the supplied string is not a valid utf8 input, however a crash is a bit harsh here.

Note that CharPointer_UTF8::isValidString returns true for that string, so maybe this validation should be made stricter. I believe the fix should be:

            int bit = 0x40;
            int numExtraValues = 0;
           
+            if ((byte & bit) != bit) return false;
           
            while ((byte & bit) != 0)

(this is because the character \xb4 is of the form 10xxxxxx , while valid non-ascii sequences should begin with 11xxxxxx )

 


#2

Thanks! The world is now a safer place..