]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Detect sharing of external file in inconsistent read-write modes
authorPiotr Trojanek <trojanek@adacore.com>
Thu, 24 Oct 2024 08:47:09 +0000 (10:47 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 12 Nov 2024 13:00:49 +0000 (14:00 +0100)
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.

gcc/ada/doc/gnat_rm/the_implementation_of_standard_i_o.rst
gcc/ada/gnat_rm.texi
gcc/ada/gnat_ugn.texi
gcc/ada/libgnat/s-ficobl.ads
gcc/ada/libgnat/s-fileio.adb

index f6d884d0823e4d47856f9676fd15d7135f1e5fa4..fff9bbf717673243bf04d7cd2ce09b5d91576c0f 100644 (file)
@@ -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:
 
index ff55de54d7d935b6d8e5d719585e4eef0c7895f3..5c3f439caadd4f57e446300648ed42b54087bc03 100644 (file)
@@ -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}
index 5eef6020812195c0f4dd1e5ac805857541d40195..e4f14d231d4f11b017f7c3caf0bdc62cfcefe400 100644 (file)
@@ -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
index 29560cb6f13a53cad2af884bf90182e2c0590e91..37dac540e31983c236c89042127f3192a6b148b7 100644 (file)
@@ -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
index 3dea3582a67e2d252cce5ec3756d57834d66e0de..69841f06ce1201dd342f634c17618ce70f530359 100644 (file)
@@ -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;