Fix cycling with -dFPC_USE_LIBC on Linux systems to allow usage of FPC Linux programs on OSv.

memchr seems to have a bug on recent Linux systems if the count that is passed in is larger than the buffer (in our specific case the count is -1 to find the 0 byte of \0 terminated strings): the function "randomly" fails to find the byte and returns zero thus leading to for example incorrect parameter handling.

rtl/inc/cgeneric.inc:
  * use rawmemchr on Linux if -1 is passed as count

git-svn-id: trunk@30160 -
This commit is contained in:
svenbarth 2015-03-10 18:34:04 +00:00
parent af9798c761
commit dcfd734bbf

View File

@ -49,7 +49,14 @@ end;
{$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
{$define FPC_SYSTEM_HAS_INDEXBYTE}
{$ifdef LINUX}
{$define BUGGYMEMCHR}
{$endif}
function memchr(const buf; b: cint; len: size_t): pointer; cdecl; external 'c';
{$ifdef BUGGYMEMCHR}
function rawmemchr(const buf; b: cint): pointer; cdecl; external 'c';
{$endif BUGGYMEMCHR}
function IndexByte(Const buf;len:sizeint;b:byte):sizeint;{$ifdef SYSTEMINLINE}inline;{$endif}
var
@ -60,7 +67,12 @@ begin
{ simulate assembler implementations behaviour, which is expected }
{ fpc_pchar_to_ansistr in astrings.inc (interpret values < 0 as }
{ unsigned) }
res := memchr(buf,cint(b),size_t(sizeuint(len)));
{$ifdef BUGGYMEMCHR}
if len = -1 then
res := rawmemchr(buf,cint(b))
else
{$endif BUGGYMEMCHR}
res := memchr(buf,cint(b),size_t(sizeuint(len)));
if (res <> nil) then
IndexByte := SizeInt(res-@buf)
else