Skip to content

Commit d6211f2

Browse files
committed
Fix coverage crash when generating a HTML report with missing sources
1 parent 92e7616 commit d6211f2

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package body Pkg is
2+
procedure Foo is
3+
begin
4+
null;
5+
end Foo;
6+
end Pkg;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package Pkg is
2+
procedure Foo;
3+
end Pkg;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
with Pkg;
2+
3+
procedure Test is
4+
begin
5+
Pkg.Foo;
6+
end Test;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Regression test: check that gnatcov does not crash when generating a XML / HTML
3+
report with missing sources. It used to crash when trying to compute scope
4+
metrics.
5+
"""
6+
7+
from e3.fs import mv, sync_tree
8+
9+
import os
10+
11+
from SCOV.minicheck import build_and_run
12+
from SUITE.cutils import Wdir
13+
from SUITE.gprutils import GPRswitches
14+
from SUITE.tutils import gprfor, thistest, xcov
15+
16+
tmp = Wdir("tmp_")
17+
18+
sync_tree(os.path.join("..", "src"), "src")
19+
20+
xcov_args = build_and_run(
21+
gprsw=GPRswitches(
22+
root_project=gprfor(srcdirs=["src"], mains=["test.adb"])
23+
),
24+
covlevel="stmt",
25+
mains=["test"],
26+
extra_coverage_args=[],
27+
trace_mode="src",
28+
)
29+
30+
# Then hide the sources
31+
os.mkdir("src_hidden")
32+
mv(os.path.join("src", "pkg.adb"), "src_hidden")
33+
34+
# Try to generate a HTML, and an XML report
35+
xcov(xcov_args + ["-axml"], tolerate_messages="warning: can't open")
36+
xcov(xcov_args + ["-ahtml"], tolerate_messages="warning: can't open")
37+
38+
thistest.result()

tools/gnatcov/annotations.adb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,24 @@ package body Annotations is
996996
(FI : File_Info_Access;
997997
From, To : Natural) return Li_Stat_Array
998998
is
999-
Result : Li_Stat_Array := (others => 0);
999+
Result : Li_Stat_Array := (others => 0);
1000+
Actual_To : Natural := To;
1001+
Last_Loaded_Line : Natural renames Last_Line (FI);
10001002
begin
1001-
for L in From .. To loop
1003+
-- Do not try to retrieve a line that was not loaded (which has an index
1004+
-- superior to Last_Line (FI)). This can happen when the sources are not
1005+
-- available. In that case, only loop until the last loaded line is
1006+
-- reached (it actually corresponds to the last SCO line), and consider
1007+
-- the subsequent lines as no code.
1008+
1009+
if To > Last_Loaded_Line then
1010+
Result (No_Code) := To - Last_Loaded_Line;
1011+
Actual_To := Last_Loaded_Line;
1012+
end if;
1013+
1014+
for L in From .. Actual_To loop
10021015
declare
1003-
LI : constant Line_Info_Access := Get_Line (FI, L);
1016+
LI : constant Line_Info_Access := Get_Line (FI, L);
10041017
S : constant Any_Line_State := Aggregated_State (LI.all);
10051018
begin
10061019
-- Update counts. Note that No_Code lines are always counted as

0 commit comments

Comments
 (0)