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.
This commit is contained in:
Adrien Destugues
2016-09-29 22:56:37 +02:00
parent 509e0b5802
commit f638c82a22
3 changed files with 9 additions and 12 deletions
@@ -524,16 +524,19 @@ translate_from_text(BPositionIO* source, const char* encoding, bool forceEncodin
BMallocIO encodingIO; BMallocIO encodingIO;
BNode* node = dynamic_cast<BNode*>(source); BNode* node = dynamic_cast<BNode*>(source);
BString name(encoding);
if (node != NULL) { if (node != NULL) {
// determine encoding, if available // determine encoding, if available
bool hasAttribute = false; bool hasAttribute = false;
if (encoding != NULL && !forceEncoding) { if (encoding != NULL && !forceEncoding) {
BString name; attr_info info;
if ((node->ReadAttrString("be:encoding", &name) == B_OK) node->GetAttrInfo("be:encoding", &info);
&& (name.Length() > 0)) {
if ((info.type == B_STRING_TYPE) && (node->ReadAttrString(
"be:encoding", &name) == B_OK)) {
encoding = name.String(); encoding = name.String();
hasAttribute = true; hasAttribute = true;
} else { } else if (info.type == B_INT32_TYPE) {
// Try the BeOS version of the atribute, which used an int32 // Try the BeOS version of the atribute, which used an int32
// and a well-known list of encodings. // and a well-known list of encodings.
int32 value; int32 value;
@@ -542,8 +545,8 @@ translate_from_text(BPositionIO* source, const char* encoding, bool forceEncodin
if (bytesRead == (ssize_t)sizeof(value)) { if (bytesRead == (ssize_t)sizeof(value)) {
hasAttribute = true; hasAttribute = true;
if (value != 65535) { if (value != 65535) {
const BCharacterSet* characterSet = NULL; const BCharacterSet* characterSet
characterSet = BCharacterSetRoster::GetCharacterSetByConversionID(value); = BCharacterSetRoster::GetCharacterSetByConversionID(value);
if (characterSet != NULL) if (characterSet != NULL)
encoding = characterSet->GetName(); encoding = characterSet->GetName();
} }
@@ -603,7 +606,6 @@ translate_from_text(BPositionIO* source, const char* encoding, bool forceEncodin
status = codec.Decode(pos, bytes, status = codec.Decode(pos, bytes,
(char*)encodingBuffer.Buffer(), encodingLength); (char*)encodingBuffer.Buffer(), encodingLength);
if (status < B_OK) { if (status < B_OK) {
puts("oops");
return status; return status;
} }
-1
View File
@@ -141,7 +141,6 @@ StyledEditApp::StyledEditApp()
sWindowRect.top *= factor; sWindowRect.top *= factor;
sWindowRect.right *= factor; sWindowRect.right *= factor;
sWindowRect.bottom *= factor; sWindowRect.bottom *= factor;
sWindowRect.PrintToStream();
} }
-4
View File
@@ -87,10 +87,6 @@ BTextEncoding::Decode(const char* input, size_t& inputLength, char* output,
ucnv_convertEx(fUtf8Converter, fConverter, &target, output + outputLength, ucnv_convertEx(fUtf8Converter, fConverter, &target, output + outputLength,
&base, input + inputLength, NULL, NULL, NULL, NULL, FALSE, TRUE, &base, input + inputLength, NULL, NULL, NULL, NULL, FALSE, TRUE,
&error); &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 // 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). // the input data (for example if it is cut in the middle of an utf-8 char).