From: Piotr Trojanek <troja...@adacore.com>

When opening files with "shared=yes", as described in GNAT RM 11.10,
Sharing Files, we now prevent sharing a single file in inconsistent
read-write modes.

gcc/ada/ChangeLog:

        * doc/gnat_rm/the_implementation_of_standard_i_o.rst
        (Shared Files): Add trailing period.
        * libgnat/s-ficobl.ads (AFCB): Reflect new behavior in comment.
        * libgnat/s-fileio.adb (Open): Detect inconsistent sharing,
        just like we do in System.File_IO.Reset.
        * gnat_rm.texi: Regenerate.
        * gnat_ugn.texi: Regenerate.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/doc/gnat_rm/the_implementation_of_standard_i_o.rst | 2 +-
 gcc/ada/gnat_rm.texi                                       | 4 ++--
 gcc/ada/gnat_ugn.texi                                      | 2 +-
 gcc/ada/libgnat/s-ficobl.ads                               | 4 ++--
 gcc/ada/libgnat/s-fileio.adb                               | 5 +++++
 5 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/ada/doc/gnat_rm/the_implementation_of_standard_i_o.rst 
b/gcc/ada/doc/gnat_rm/the_implementation_of_standard_i_o.rst
index f6d884d0823..fff9bbf7176 100644
--- a/gcc/ada/doc/gnat_rm/the_implementation_of_standard_i_o.rst
+++ b/gcc/ada/doc/gnat_rm/the_implementation_of_standard_i_o.rst
@@ -894,7 +894,7 @@ One common use of file sharing in Ada 83 is the use of 
instantiations of
 Sequential_IO on the same file with different types, to achieve
 heterogeneous input-output.  Although this approach will work in GNAT if
 ``shared=yes`` is specified, it is preferable in Ada to use Stream_IO
-for this purpose (using the stream attributes)
+for this purpose (using the stream attributes).
 
 .. _Filenames_encoding:
 
diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi
index ff55de54d7d..5c3f439caad 100644
--- a/gcc/ada/gnat_rm.texi
+++ b/gcc/ada/gnat_rm.texi
@@ -19,7 +19,7 @@
 
 @copying
 @quotation
-GNAT Reference Manual , Nov 04, 2024
+GNAT Reference Manual , Nov 08, 2024
 
 AdaCore
 
@@ -22891,7 +22891,7 @@ One common use of file sharing in Ada 83 is the use of 
instantiations of
 Sequential_IO on the same file with different types, to achieve
 heterogeneous input-output.  Although this approach will work in GNAT if
 @code{shared=yes} is specified, it is preferable in Ada to use Stream_IO
-for this purpose (using the stream attributes)
+for this purpose (using the stream attributes).
 
 @node Filenames encoding,File content encoding,Shared Files,The Implementation 
of Standard I/O
 @anchor{gnat_rm/the_implementation_of_standard_i_o 
filenames-encoding}@anchor{2e2}@anchor{gnat_rm/the_implementation_of_standard_i_o
 id22}@anchor{2e3}
diff --git a/gcc/ada/gnat_ugn.texi b/gcc/ada/gnat_ugn.texi
index 5eef6020812..e4f14d231d4 100644
--- a/gcc/ada/gnat_ugn.texi
+++ b/gcc/ada/gnat_ugn.texi
@@ -29841,8 +29841,8 @@ to permit their use in free software.
 
 @printindex ge
 
-@anchor{d2}@w{                              }
 @anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{   
                           }
+@anchor{d2}@w{                              }
 
 @c %**end of body
 @bye
diff --git a/gcc/ada/libgnat/s-ficobl.ads b/gcc/ada/libgnat/s-ficobl.ads
index 29560cb6f13..37dac540e31 100644
--- a/gcc/ada/libgnat/s-ficobl.ads
+++ b/gcc/ada/libgnat/s-ficobl.ads
@@ -100,8 +100,8 @@ package System.File_Control_Block with SPARK_Mode => Off is
       --  defaults at this level). The string is always null-terminated.
 
       Mode : File_Mode;
-      --  The file mode. No checks are made that the mode is consistent
-      --  with the form used to fopen the file.
+      --  The file mode. When sharing files, we check that the mode is
+      --  consistent with the already opened files.
 
       Is_Regular_File : Boolean;
       --  A flag indicating if the file is a regular file
diff --git a/gcc/ada/libgnat/s-fileio.adb b/gcc/ada/libgnat/s-fileio.adb
index 3dea3582a67..69841f06ce1 100644
--- a/gcc/ada/libgnat/s-fileio.adb
+++ b/gcc/ada/libgnat/s-fileio.adb
@@ -1042,6 +1042,11 @@ package body System.File_IO is
                      elsif Shared = Yes
                        and then P.Shared_Status = Yes
                      then
+                        if Mode /= P.Mode then
+                           raise Use_Error
+                             with "sharing file in different modes";
+                        end if;
+
                         Stream := P.Stream;
 
                         Record_AFCB;
-- 
2.43.0

Reply via email to