ada: Add Codepeer Exemption + simplify TO_C code.
authorLiaiss Merzougue <merzougue@adacore.com>
Fri, 2 Apr 2021 09:29:03 +0000 (09:29 +0000)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 6 Dec 2022 13:58:47 +0000 (14:58 +0100)
This patch simplify the TO_C code to have a single branch for
raising exception. Furthermore, adding pragma annotate for codepeer
to ignore uninitialized value since this is caused because we have
input check before the initialization.

gcc/ada/

* libgnat/i-c.adb (To_C): Simplify code for having a single
exception raise. Add pragma annotate about uninitialized value
which happen only on exception raising.

gcc/ada/libgnat/i-c.adb

index 4b50d18..2866021 100644 (file)
@@ -186,7 +186,7 @@ is
      (Item     : char_array;
       Trim_Nul : Boolean := True) return String
    is
-      Count : Natural;
+      Count : Natural := 0;
       From  : size_t;
 
    begin
@@ -1177,7 +1177,7 @@ is
       To : size_t;
 
    begin
-      if Target'Length < Item'Length then
+      if Target'Length < Item'Length + (if Append_Nul then 1 else 0) then
          raise Constraint_Error;
 
       else
@@ -1210,17 +1210,14 @@ is
                      Target'First + (Item'Length - 1))'Initialized);
 
          if Append_Nul then
-            if To > Target'Last then
-               raise Constraint_Error;
-            else
-               Target (To) := char32_nul;
-               Count := Item'Length + 1;
-            end if;
-
+            Target (To) := char32_nul;
+            Count := Item'Length + 1;
          else
             Count := Item'Length;
          end if;
       end if;
    end To_C;
+   pragma Annotate (CodePeer, False_Positive, "validity check",
+     "Count is only uninitialized on abnormal return.");
 
 end Interfaces.C;