Преглед изворни кода

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 -
svenbarth пре 10 година
родитељ
комит
dcfd734bbf
1 измењених фајлова са 13 додато и 1 уклоњено
  1. 13 1
      rtl/inc/cgeneric.inc

+ 13 - 1
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