From f638c82a22f6c759ae2027a668ce854a5a3e04cb Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 29 Sep 2016 22:43:31 +0200 Subject: [PATCH] StyledText: fix reading of utf-8 documents BNode::ReadAttrString does not check for the attribute to actually be a string. It will return B_OK (and useless data) if called on a B_INT32_TYPE attribute. Check the attribute type before reading. Also remove various debug ouput leftovers. May fix #12976. --- src/add-ons/translators/stxt/STXTTranslator.cpp | 16 +++++++++------- src/apps/stylededit/StyledEditApp.cpp | 1 - src/kits/locale/TextEncoding.cpp | 4 ---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/add-ons/translators/stxt/STXTTranslator.cpp b/src/add-ons/translators/stxt/STXTTranslator.cpp index e71a49a588..101ba42df9 100644 --- a/src/add-ons/translators/stxt/STXTTranslator.cpp +++ b/src/add-ons/translators/stxt/STXTTranslator.cpp @@ -524,16 +524,19 @@ translate_from_text(BPositionIO* source, const char* encoding, bool forceEncodin BMallocIO encodingIO; BNode* node = dynamic_cast(source); + BString name(encoding); if (node != NULL) { // determine encoding, if available bool hasAttribute = false; if (encoding != NULL && !forceEncoding) { - BString name; - if ((node->ReadAttrString("be:encoding", &name) == B_OK) - && (name.Length() > 0)) { + attr_info info; + node->GetAttrInfo("be:encoding", &info); + + if ((info.type == B_STRING_TYPE) && (node->ReadAttrString( + "be:encoding", &name) == B_OK)) { encoding = name.String(); hasAttribute = true; - } else { + } else if (info.type == B_INT32_TYPE) { // Try the BeOS version of the atribute, which used an int32 // and a well-known list of encodings. int32 value; @@ -542,8 +545,8 @@ translate_from_text(BPositionIO* source, const char* encoding, bool forceEncodin if (bytesRead == (ssize_t)sizeof(value)) { hasAttribute = true; if (value != 65535) { - const BCharacterSet* characterSet = NULL; - characterSet = BCharacterSetRoster::GetCharacterSetByConversionID(value); + const BCharacterSet* characterSet + = BCharacterSetRoster::GetCharacterSetByConversionID(value); if (characterSet != NULL) encoding = characterSet->GetName(); } @@ -603,7 +606,6 @@ translate_from_text(BPositionIO* source, const char* encoding, bool forceEncodin status = codec.Decode(pos, bytes, (char*)encodingBuffer.Buffer(), encodingLength); if (status < B_OK) { - puts("oops"); return status; } diff --git a/src/apps/stylededit/StyledEditApp.cpp b/src/apps/stylededit/StyledEditApp.cpp index 66b9b46ddf..7e3ecdc08d 100644 --- a/src/apps/stylededit/StyledEditApp.cpp +++ b/src/apps/stylededit/StyledEditApp.cpp @@ -141,7 +141,6 @@ StyledEditApp::StyledEditApp() sWindowRect.top *= factor; sWindowRect.right *= factor; sWindowRect.bottom *= factor; - sWindowRect.PrintToStream(); } diff --git a/src/kits/locale/TextEncoding.cpp b/src/kits/locale/TextEncoding.cpp index 39f291f791..7bfe3b40f7 100644 --- a/src/kits/locale/TextEncoding.cpp +++ b/src/kits/locale/TextEncoding.cpp @@ -87,10 +87,6 @@ BTextEncoding::Decode(const char* input, size_t& inputLength, char* output, ucnv_convertEx(fUtf8Converter, fConverter, &target, output + outputLength, &base, input + inputLength, NULL, NULL, NULL, NULL, FALSE, TRUE, &error); - if (!U_SUCCESS(error)) { - printf("zz %s\n", u_errorName(error)); - } - // inputLength is set to the number of bytes consumed. We may not use all of // the input data (for example if it is cut in the middle of an utf-8 char).