Author: amassari
Date: Mon Sep 7 10:11:28 2015
New Revision: 1701594
URL: http://svn.apache.org/r1701594
Log:
Don't assume we know the size of the decoded string, expand the buffer whenever
the transcoder reports it wrote 0 bytes (XERCESC-2052)
Modified:
xerces/c/trunk/src/xercesc/util/TransService.cpp
xerces/c/trunk/tests/src/DOM/DOMTest/DTest.cpp
Modified: xerces/c/trunk/src/xercesc/util/TransService.cpp
URL:
http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/TransService.cpp?rev=1701594&r1=1701593&r2=1701594&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/TransService.cpp (original)
+++ xerces/c/trunk/src/xercesc/util/TransService.cpp Mon Sep 7 10:11:28 2015
@@ -612,23 +612,30 @@ void TranscodeToStr::transcode(const XML
fString.reset((XMLByte*)fMemoryManager->allocate(allocSize),
fMemoryManager);
XMLSize_t charsDone = 0;
+ bool bufferExpanded = false;
while(charsDone < len) {
XMLSize_t charsRead = 0;
fBytesWritten += trans->transcodeTo(in + charsDone, len - charsDone,
fString.get() + fBytesWritten,
allocSize - fBytesWritten,
charsRead,
XMLTranscoder::UnRep_Throw);
- if(charsRead == 0)
- ThrowXMLwithMemMgr(TranscodingException,
XMLExcepts::Trans_BadSrcSeq, fMemoryManager);
-
- charsDone += charsRead;
-
- if((allocSize - fBytesWritten) < ((len - charsDone) * sizeof(XMLCh)))
+ if (charsRead == 0)
{
+ if (bufferExpanded)
+ {
+ ThrowXMLwithMemMgr(TranscodingException,
XMLExcepts::Trans_BadSrcSeq, fMemoryManager);
+ }
+ // it may have not enough space, try expanding the buffer
allocSize *= 2;
XMLByte *newBuf = (XMLByte*)fMemoryManager->allocate(allocSize);
memcpy(newBuf, fString.get(), fBytesWritten);
fString.reset(newBuf, fMemoryManager);
+ bufferExpanded = true;
+ }
+ else
+ {
+ charsDone += charsRead;
+ bufferExpanded = false;
}
}
Modified: xerces/c/trunk/tests/src/DOM/DOMTest/DTest.cpp
URL:
http://svn.apache.org/viewvc/xerces/c/trunk/tests/src/DOM/DOMTest/DTest.cpp?rev=1701594&r1=1701593&r2=1701594&view=diff
==============================================================================
--- xerces/c/trunk/tests/src/DOM/DOMTest/DTest.cpp (original)
+++ xerces/c/trunk/tests/src/DOM/DOMTest/DTest.cpp Mon Sep 7 10:11:28 2015
@@ -6061,5 +6061,17 @@ bool DOMTest::testUtilFunctions()
}
XMLString::release(&bytes);
+ // XERCESC-2052
+ // Input: U+4E2D U+56FD U+5236 U+9020 U+4E2D U+570B
U+88FD U+9020
+ // Expected byte sequence: E4 B8 AD E5 9B BD E5 88 B6 E9 80 A0 20 2F 20
E4 B8 AD E5 9C 8B E8 A3 BD E9 80 A0
+ const XMLCh xmlStr2[] = { 0x4E2D, 0x56FD, 0x5236, 0x9020, 0x20, 0x2F,
0x20, 0x4E2D, 0x570B, 0x88FD, 0x9020, 0x0000 };
+ char* bytes2 = (char*)TranscodeToStr(xmlStr2, "UTF-8").adopt();
+ if (!XMLString::equals(bytes2,
"\xE4\xB8\xAD\xE5\x9B\xBD\xE5\x88\xB6\xE9\x80\xA0\x20\x2F\x20\xE4\xB8\xAD\xE5\x9C\x8B\xE8\xA3\xBD\xE9\x80\xA0"))
+ {
+ fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
+ OK = false;
+ }
+ XMLString::release(&bytes2);
+
return OK;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]