Fix reading UTF-8 strings (from OPC UA nodes) #832
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When reading string values via the OPC UA protocol a
StringIndexOutOfBoundsException
was thrown whenever the string contained non-ASCII characters. Upon further inspection, the problem could be pinned down to the logic inreadString()
ofReadBufferByteBased
.The
readString()
function inReadBufferByteBased
could result in aStringIndexOutOfBoundsException
upon callingsubstring()
. Reason is that the calculatedrealLength
is in bytes and the length of the created string is the number of characters. For UTF encodings, multiple bytes can be just one character. This causesrealLength
to be longer than the actual string and thus thesubstring()
-call to fail.This PR fixes the issue by applying
realLength
to the byte-length instead of the string length: The byte array is sliced to lengthrealLength
and then converted to the final string.substring()
isn't used anymore.The fix can be verified via the included regression test and has additionally been tested locally using the OPC UA protocol.
Maybe other protocols were also affected?
Additionally I've included some other minor fixes for the logic. Check the commits for these.
Regards,
Marc