Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GsAmqpBytes class>>fromStringEncodeAsUtf8: #8

Open
feldti opened this issue Apr 20, 2024 · 3 comments
Open

GsAmqpBytes class>>fromStringEncodeAsUtf8: #8

feldti opened this issue Apr 20, 2024 · 3 comments

Comments

@feldti
Copy link

feldti commented Apr 20, 2024

This method is a helper method and only called by methods in AmqpFieldValue. In my code I had instances of Unicode16 and they are delivered to this method (via byteObj) and the method crashes. This may happen, when you build a dictionary in your code and then you want to build an instance of GsAmqpTableEntryArray from a dictionary.

fromStringEncodeAsUtf8: byteObj
^(byteObj size == 0) "Handle both empty byteObj and nil"
ifTrue: [self emptyBytes]
ifFalse:
[| cba res |GsAmqpFieldValue
byteObj _validateClasses: { String . MultiByteString } . "Prevent assertion failure in slow builds where byteObj class == Utf8 "
cba := CByteArray gcMalloc: byteObj size.
cba encodeUTF8From: byteObj into: 0 allowCodePointZero: false.
(res := self new) setBytes: cba .
res]

Here a

GsAmqpBytes fromStringEncodeAsUtf8: '0061_vallée_de_l-ernz_az_en_gm_zs-ddxml'

@feldti
Copy link
Author

feldti commented Apr 21, 2024

Ok, I would argue, that this is a bug in Gemstone/S. You may strip it down to:

| cByteArray sourceString |

sourceString := 'grüne' .
cByteArray := CByteArray gcMalloc: sourceString size.
cByteArray encodeUTF8From: sourceString into: 0 allowCodePointZero: false

@feldti
Copy link
Author

feldti commented Apr 21, 2024

I made a bug request at GemtalkSystems

@feldti
Copy link
Author

feldti commented Apr 23, 2024

Ok, after getting an answer from GemtalkSystem, the method mentioned above should coded in that way. Notice the usage of #sizeForEncodeAsUTF8 and consider instances of MultiByteString. The original error message is - by the way - a bug. They check for the size of the CByteArray and in an error case they throw an exception, but the false one.

So here is the corrected method:

GsAmqpBytes>>fromStringEncodeAsUtf8: byteObj
^(byteObj size == 0) "Handle both empty byteObj and nil"
ifTrue: [self emptyBytes]
ifFalse:
[| cba res |GsAmqpFieldValue
byteObj _validateClasses: { String . MultiByteString } . "Prevent assertion failure in slow builds where byteObj class == Utf8 "
cba := CByteArray gcMalloc: byteObj sizeForEncodeAsUTF8.
cba encodeUTF8From: byteObj into: 0 allowCodePointZero: false.
(res := self new) setBytes: cba .
res]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant