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

Issue in the new implementation of Utf8ToWideChar #296

Closed
syagrius opened this issue Sep 13, 2024 · 1 comment
Closed

Issue in the new implementation of Utf8ToWideChar #296

syagrius opened this issue Sep 13, 2024 · 1 comment

Comments

@syagrius
Copy link

Hi @synopse,

I think I found an issue in the new implemenation of Utf8ToWideChar.
I use Delphi 12.1, target Win32

I'm using the JSONToDataSet() function and this function fails with the latest revisions of Mormot2.

After tracing the code I've found an Issue:

Utf8ToWideChar() truncates the destination string.
The resulting string is shortened by 1 character.

How to reproduce

procedure  TTestCoreBase._Utf8ToWideChar;
var
  dest: array[0..9] of WideChar;
begin
  Utf8ToWideChar(@dest, '1234567890', 10, sizeof(dest));
  check( WideCharToString(Dest)='1234567890')   //<<   Returns '123456789'
end;

Workaround

Note: It seems that this fixes the problem, but I'm not certain about my correction

In unit mormot.core.unicode.pas

I replace lines 2870 and 2899:
if PtrUInt(@dest[1]) >= PtrUInt(endDest) then
with
if PtrUInt(@dest[1]) > PtrUInt(endDest) then

@syagrius syagrius changed the title Issue in new implementation of Utf8ToWideChar Issue in the new implementation of Utf8ToWideChar Sep 13, 2024
@synopse
Copy link
Owner

synopse commented Sep 13, 2024

The documentation states that
a WideChar(#0) is added at the end (if something is written)
So your destination should be defined as:

var
  dest: array[0..10] of WideChar;

For me, it works as expected and defined.
Especially you NEED to follow this requirement:
enough place must be available in dest buffer (guess is sourceBytes*3+2)
which was not the case in your code.

@synopse synopse closed this as completed Sep 13, 2024
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

2 participants