Skip to content

Fixed missing zero terminating for strings, fixed conversion using UnicodeToUtf8, unicode2ascii

Sergey Larin requested to merge sergy.larin/fpcsrc:compiller-str-term0-fix into main
  • Fixed conversion of string constants when using unicode2ascii, Utf8ToUnicode, UnicodeToUtf8.
    The UnicodeToUtf8 function writes 0 to the end of the string, and memory must be allocated for this 0.
    The Utf8ToUnicode function does not write 0 to the end of the string, but, like UnicodeToUtf8, it returns +1 to the length of the resulting string.
    The unicode2ascii function does not write 0 to the end of the string, it returns the length of the string without the terminating 0.

  • In tstringconstnode, set the len after converting ascii2unicode.
    len is used in tcgstringconstnode.pass_generate_code as the HashTable key length,
    used in ppuwrite for the Attribute constructor parameter value.

  • Fixed a potential bug in the scanner when reading a string constant expression.
    The s: string argument is of type shortstring, i.e. s is not zero-terminated.

  • In TElfObjInput.ReadObjData also needs to terminate the strings shstrtab, strtab with zero.
    Judging by the string(PChar(@shstrtab[shdr.sh_name])) code, the terminating zero is needed.

  • Fixed conversion from UTF16 to UTF8 in the TStringConstNode.XMLPrintNodeData method.
    The number of UTF8 bytes can be greater than the length of the UTF16 string.

Also at the same time

  • Fixed using UnicodeToUtf8 in fpwidestring unit (issue #39746)
  • Fixed using UnicodeToUtf8 in android/cwstring.pp, nativent/sysutils.pp

Resolves #39746

Edited by Sergey Larin

Merge request reports