Browse Source

Don’t use local shortstring in generic fpc_pchar_to_shortstr, move directly to the destination.

Rika Ichinose 2 years ago
parent
commit
8d1d763a1a
1 changed files with 14 additions and 8 deletions
  1. 14 8
      rtl/inc/generic.inc

+ 14 - 8
rtl/inc/generic.inc

@@ -994,18 +994,24 @@ end;
 procedure fpc_pchar_to_shortstr(out res : shortstring;p:PAnsiChar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
 var
   l : ObjpasInt;
-  s: shortstring;
 begin
   if p=nil then
-    l:=0
-  else
-    l:=strlen(p);
+    begin
+      res[0]:=#0;
+      exit;
+    end;
+{ On platforms where IndexByte with len > 0 will not read the invalid memory past the null terminator, high(res) can be used as a limit. }
+{$if defined(cpui386) or defined(cpux86_64)}
+  l:=IndexByte(p^,high(res),0);
+  if l<0 then
+    l:=high(res);
+{$else IndexByte(p^,high(res),0) can crash}
+  l:=strlen(p);
   if l>high(res) then
     l:=high(res);
-  if l>0 then
-    move(p^,s[1],l);
-  s[0]:=chr(l);
-  res:=s;
+{$endif IndexByte(p^,high(res),0) can crash}
+  move(p^,res[1],l);
+  res[0]:=chr(l);
 end;