Improvements to SysGetEnvironmentList implementations lazarus / 64-FPC development ---------------------------------------------------------------------- lagprogramming · 2023-08-08 09:57 packages/fcl-base/src/custapp.pp has the following procedure Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean); var   s : string;   i,j,count : longint; begin  count:=GetEnvironmentVariableCount;  for j:=1 to count  do    begin  s:=GetEnvironmentString(j);    If NamesOnly then      begin      I:=pos('=',s);      If (I>1) then        S:=Copy(S,1,I-1);      end;  List.Add(S);    end;end; S:=Copy(S,1,I-1); has been replaced with SetLength(S,I-1); After that, variables have been changed from longints to integers and named to uppercase in order to mimic the variable names of the other routines found in the same unit. Added and removed some spaces, too. After that I've searched for Procedure SysGetEnvironmentList in the fpc directory in order to make sure the modified code looks like other procedures. So, I've had to replace S:=Copy(S,1,I-1); with S:=SetLength(S,I-1); in other routines, too. In addition to that, the OS2 version of SysGetEnvironmentList had a bug: the S variable was untyped. The netwlibc version had the If (I>0) then condition instead of If (I>1) then. I've changed it to match all other implementations. It's this modification that I'd like FPC developers to double check. If NamesOnly was true, for a string like "=value", the original code would have considered an empty('') string as the name, in contradiction with the other implementations which would consider the entire "=value" string as a name. In this scenario, the modified code considers the entire "=value" line as the string name, as it is considered in all other implementations. I'd like FPC developers to make sure that this is the right behaviour. Now all of them should look alike. Have a look and test. I attach a patch with the following content: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i diff --git a/packages/fcl-base/src/custapp.pp b/packages/fcl- base/src/custapp.ppindex 45a37c8854..abc36a4ff4 100644--- a/packages/fcl-base/src/custapp.pp+++ b/packages/fcl- base/src/custapp.pp@@ -183,22 +183,22 @@ function TCustomApplication.GetExeName: string; end; {$endif darwin} -Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);+Procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);  var-   s : string;-   i,j,count : longint;+   S : String;+   I,J,Count : Integer;  begin-  count:=GetEnvironmentVariableCount;-  for j:=1 to count  do+  Count:=GetEnvironmentVariableCount;+  for J:=1 to Count do     begin-    s:=GetEnvironmentString(j);+    S:=GetEnvironmentString(J);     If NamesOnly then       begin-      I:=pos('=',s);+      I:=Pos('=',S);       If (I>1) then-        S:=Copy(S,1,I-1);+        SetLength(S,I-1);       end;     List.Add(S);     end;diff --git a/packages/fcl- base/src/go32v2/custapp.inc b/packages/fcl- base/src/go32v2/custapp.incindex d684059fb9..9ef83aff17 100644--- a/packages/fcl-base/src/go32v2/custapp.inc+++ b/packages/fcl- base/src/go32v2/custapp.inc@@ -13,7 +13,7 @@   *********************** ***********************************************} -Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);+Procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);  Var   P : PPAnsiChar;@@ -31,7 +31,7 @@         begin         I:=Pos('=',S);       If (I>1) then-          S:=Copy(S,1,I-1);+          SetLength(S,I-1);         end;       List.Add(S);       Inc(P);diff --git a/packages/fcl-base/src/netwlibc/custapp.inc b/packages/fcl- base/src/netwlibc/custapp.incindex 9911f3ed74..5bb53d8523 100644--- a/packages/fcl-base/src/netwlibc/custapp.inc+++ b/packages/fcl- base/src/netwlibc/custapp.inc@@ -13,7 +13,7 @@   ********************* *************************************************} -Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);+Procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);  Var   P : PPAnsiChar;@@ -30,8 +30,8 @@       If NamesOnly then         begin       I:=Pos('=',S);-        If (I>0) then-          S:=Copy(S,1,I-1);+        If (I>1) then+          SetLength(S,I-1);       end;       List.Add(S);       Inc(P);diff --git a/packages/fcl- base/src/os2/custapp.inc b/packages/fcl-base/src/os2/custapp.incindex 77224ca05f..a2561b383b 100644--- a/packages/fcl- base/src/os2/custapp.inc+++ b/packages/fcl-base/src/os2/custapp.inc@@ -30,10 +30,10 @@ function EnvStr (Index: longint): ; end;  -procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);+procedure SysGetEnvironmentList(List : TStrings; NamesOnly : Boolean);  Var-  S : ;+  S : String;   J,I : Integer;  begin@@ -45,7 +45,7 @@ procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean);       begin       I:=Pos('=',S);       If (I>1) then-        S:=Copy(S,1,I-1);+        SetLength(S,I-1);       end;     List.Add(S);     end; ---------------------------------------------------------------------- AlexTP · 2023-08-08 10:43 Posted to https://gitlab.com/freepascal.org/fpc/source/-/issues/40380