Fix Normalize_Pathname to avoid a constraint error.

Tested on x86_64-pc-linux-gnu, committed on trunk

2018-05-29  Pascal Obry  <o...@adacore.com>

gcc/ada/

        * libgnat/s-os_lib.adb (Normalize_Pathname): Fix handling of ".." in
        the root directory.

gcc/testsuite/

        * gnat.dg/normalize_pathname.adb: New testcase.
--- gcc/ada/libgnat/s-os_lib.adb
+++ gcc/ada/libgnat/s-os_lib.adb
@@ -2389,12 +2389,18 @@ package body System.OS_Lib is
          elsif Finish = Start + 1
            and then Path_Buffer (Start .. Finish) = ".."
          then
-            Start := Last;
-            loop
-               Start := Start - 1;
-               exit when Start = 1
-                 or else Path_Buffer (Start) = Directory_Separator;
-            end loop;
+            if Last > 1 then
+               Start := Last - 1;
+
+               while Start > 1
+                 and then Path_Buffer (Start) /= Directory_Separator
+               loop
+                  Start := Start - 1;
+               end loop;
+
+            else
+               Start := Last;
+            end if;
 
             if Start = 1 then
                if Finish = End_Path then

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/normalize_pathname.adb
@@ -0,0 +1,10 @@
+--  { dg-do run }
+
+with GNAT.OS_Lib;
+
+procedure Normalize_Pathname is
+   S : constant String := GNAT.OS_Lib.Normalize_Pathname
+     ("/../tmp", Directory => "", Resolve_Links => True);
+begin
+   null;
+end Normalize_Pathname;

Reply via email to