public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] ada/30827: Zero-terminate Ada-generated version string
@ 2008-08-17 16:37 Samuel Tardieu
  2008-08-18  1:10 ` Robert Dewar
  0 siblings, 1 reply; 2+ messages in thread
From: Samuel Tardieu @ 2008-08-17 16:37 UTC (permalink / raw)
  To: gcc-patches

Official GCC version do not have a date specifier between parentheses.
Therefore, GNAT.Compiler_Version is unable to find the end of the
version information.

This patch zero-terminates the version string when the binder
generates Ada code (this is already the case when the binder
generates C code), and makes GNAT.Compiler_Version also look for
NUL in addition to a closing parenthesis.

Tested on i686-pc-linux-gnu. Ok for trunk?

    gcc/ada/
	PR ada/30827
	* bindgen.adb (Gen_Output_File_Ada): Zero-terminate the
	version string.
	Move comment in the right place.
	* g-comver.adb (Version): Look for a zero-termination in
	addition to a closing parenthesis.
---
 gcc/ada/bindgen.adb  |   14 ++++++++------
 gcc/ada/g-comver.adb |    7 +++++--
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/gcc/ada/bindgen.adb b/gcc/ada/bindgen.adb
index 070651c..8927e2e 100644
--- a/gcc/ada/bindgen.adb
+++ b/gcc/ada/bindgen.adb
@@ -2267,17 +2267,19 @@ package body Bindgen is
                WBI ("   gnat_exit_status : Integer;");
                WBI ("   pragma Import (C, gnat_exit_status);");
             end if;
-
-            --  Generate the GNAT_Version and Ada_Main_Program_Name info only
-            --  for the main program. Otherwise, it can lead under some
-            --  circumstances to a symbol duplication during the link (for
-            --  instance when a C program uses 2 Ada libraries)
          end if;
 
+         --  Generate the GNAT_Version and Ada_Main_Program_Name info only
+         --  for the main program. Otherwise, it can lead under some
+         --  circumstances to a symbol duplication during the link (for
+         --  instance when a C program uses 2 Ada libraries). Also, zero
+         --  terminate the string so that its end can be found reliably.
+
          WBI ("");
          WBI ("   GNAT_Version : constant String :=");
          WBI ("                    ""GNAT Version: " &
-                                Gnat_Version_String & """;");
+                                Gnat_Version_String &
+                                """ & Character'Val(0);");
          WBI ("   pragma Export (C, GNAT_Version, ""__gnat_version"");");
 
          WBI ("");
diff --git a/gcc/ada/g-comver.adb b/gcc/ada/g-comver.adb
index 2a0d120..618ce93 100644
--- a/gcc/ada/g-comver.adb
+++ b/gcc/ada/g-comver.adb
@@ -53,15 +53,18 @@ package body GNAT.Compiler_Version is
 
    function Version return String is
    begin
-      --  Search for terminating right paren
+      --  Search for terminating right paren or the end of the string
 
       for J in Ver_Prefix'Length + 1 .. GNAT_Version'Last loop
          if GNAT_Version (J) = ')' then
             return GNAT_Version (Ver_Prefix'Length + 1 .. J);
          end if;
+         if GNAT_Version (J) = Character'Val (0) then
+            return GNAT_Version (Ver_Prefix'Length + 1 .. J - 1);
+         end if;
       end loop;
 
-      --  This should not happen (no right paren found)
+      --  This should not happen (no right paren or zero found)
 
       return GNAT_Version;
    end Version;
-- 
1.6.0.rc3.258.gecf7

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ada/30827: Zero-terminate Ada-generated version string
  2008-08-17 16:37 [PATCH] ada/30827: Zero-terminate Ada-generated version string Samuel Tardieu
@ 2008-08-18  1:10 ` Robert Dewar
  0 siblings, 0 replies; 2+ messages in thread
From: Robert Dewar @ 2008-08-18  1:10 UTC (permalink / raw)
  To: Samuel Tardieu; +Cc: gcc-patches

Sam, here is my slightly modified version of your
patch, certainly fine to install:

> --- bindgen.adb (revision 134502)
> +++ bindgen.adb (working copy)
> @@ -2299,17 +2299,19 @@
>                 WBI ("   gnat_exit_status : Integer;");
>                 WBI ("   pragma Import (C, gnat_exit_status);");
>              end if;
> -
> -            --  Generate the GNAT_Version and Ada_Main_Program_Name info only
> -            --  for the main program. Otherwise, it can lead under some
> -            --  circumstances to a symbol duplication during the link (for
> -            --  instance when a C program uses 2 Ada libraries)
>           end if;
> 
> +         --  Generate the GNAT_Version and Ada_Main_Program_Name info only for
> +         --  the main program. Otherwise, it can lead under some circumstances
> +         --  to a symbol duplication during the link (for instance when a C
> +         --  program uses two Ada libraries). Also zero terminate the string
> +         --  so that its end can be found reliably at run time.
> +
>           WBI ("");
>           WBI ("   GNAT_Version : constant String :=");
>           WBI ("                    ""GNAT Version: " &
> -                                Gnat_Version_String & """;");
> +                                   Gnat_Version_String &
> +                                   """ & ASCII.NUL;");
>           WBI ("   pragma Export (C, GNAT_Version, ""__gnat_version"");");
> 
>           WBI ("");

> --- g-comver.adb        (revision 134444)
> +++ g-comver.adb        (working copy)
> @@ -53,15 +53,19 @@
> 
>     function Version return String is
>     begin
> -      --  Search for terminating right paren
> +      --  Search for terminating right paren or NUL ending the string
> 
>        for J in Ver_Prefix'Length + 1 .. GNAT_Version'Last loop
>           if GNAT_Version (J) = ')' then
>              return GNAT_Version (Ver_Prefix'Length + 1 .. J);
>           end if;
> +
> +         if GNAT_Version (J) = Character'Val (0) then
> +            return GNAT_Version (Ver_Prefix'Length + 1 .. J - 1);
> +         end if;
>        end loop;
> 
> -      --  This should not happen (no right paren found)
> +      --  This should not happen (no right paren or NUL found)
> 
>        return GNAT_Version;
>     end Version;

And here is the best I can come up with for a test program:

> with GNAT.Compiler_Version;
> procedure TestVer is
>    package Vsn is new GNAT.Compiler_Version;
>    use Vsn;
>    X : String := Version;
> begin
>    if X'Length = 46 then
>       -- 46 = Ver_Len_Max + Ver_Prefix
>       -- actual version should be shorter than this
>       raise Program_Error;
>    end if;
> end;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-08-17 22:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-17 16:37 [PATCH] ada/30827: Zero-terminate Ada-generated version string Samuel Tardieu
2008-08-18  1:10 ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).