[fpc-devel] SetString difference between Ansistring and UnicodeString

Vincent Snijders vsnijders at vodafonevast.nl
Tue Oct 6 13:02:33 CEST 2009


While running valgrind on fpcdoc, I got a warning in
Procedure SetString (Out S : UnicodeString; Buf : PUnicodeChar; Len : SizeInt);

It assumes that the Buf is null terminated, and tries to use that information to 
truncate the length. The ansistring version:
Procedure SetString (Out S : AnsiString; Buf : PChar; Len : SizeInt); simply sets 
the length and moves the chars from Buf. Why is does the UnicodeString version only 
move the chars until the first #0000?

The reason ValGrind complains is probably that SetStrings assumes that Buf is null 
terminated:
       BufLen := IndexWord(Buf^, Len+1, 0);
IndexWord reads Len+1 words, I would change that code to
       BufLen := IndexWord(Buf^, Len, 0);
At least, if this scanning is actually useful for UnicodeStrings, unlike for 
Ansistrings. Otherwiser it may as well be removed.

Vincent



More information about the fpc-devel mailing list