From dcfd734bbffc0154cf74805523e3bd9907858222 Mon Sep 17 00:00:00 2001 From: svenbarth Date: Tue, 10 Mar 2015 18:34:04 +0000 Subject: [PATCH] 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 - --- rtl/inc/cgeneric.inc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/rtl/inc/cgeneric.inc b/rtl/inc/cgeneric.inc index 1fb91ac424..aadf96e44e 100644 --- a/rtl/inc/cgeneric.inc +++ b/rtl/inc/cgeneric.inc @@ -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