When executing a Line Number program statement, the state machine bails
out a few bytes before the current offset reaches the end of section to
account for possible padding bytes at the end.

The current test is checking if current_offset + 4 >= section_length,
which is too early for e.g. a program terminating with

  Advance PC by constant 17 to 0x776
  Extended opcode 1: End of Sequence

at offset 101 for a section with length 105.

This change tightens the computation, allowing proper interpretation
in the example case above, and explains the rationale.

Tested on x86_64-pc-linux-gnu, committed on trunk

2017-12-05  Olivier Hainque  <hain...@adacore.com>

        * s-dwalin.adb (Read_And_Execute_Isn): Adjust test checking for the end
        of section. Add comments explaining the rationale of the computation.

Index: libgnat/s-dwalin.adb
===================================================================
--- libgnat/s-dwalin.adb        (revision 255411)
+++ libgnat/s-dwalin.adb        (working copy)
@@ -578,14 +578,21 @@
          Initialize_State_Machine (C);
       end if;
 
-      --  Read the next prologue
+      --  If we have reached the next prologue, read it. Beware of possibly
+      --  empty blocks.
 
+      --  When testing for the end of section, beware of possible zero padding
+      --  at the end. Bail out as soon as there's not even room for at least a
+      --  DW_LNE_end_sequence, 3 bytes from Off to Off+2. This resolves to
+      --  Off+2 > Last_Offset_Within_Section, that is Off+2 > Section_Length-1,
+      --  or Off+3 > Section_Length.
+
       Tell (C.Lines, Off);
       while Off = C.Next_Prologue loop
          Initialize_State_Machine (C);
          Parse_Prologue (C);
          Tell (C.Lines, Off);
-         exit when Off + 4 >= Length (C.Lines);
+         exit when Off + 3 > Length (C.Lines);
       end loop;
 
       --  Test whether we're done
@@ -595,7 +602,7 @@
       --  We are finished when we either reach the end of the section, or we
       --  have reached zero padding at the end of the section.
 
-      if Prologue.Unit_Length = 0 or else Off + 4 >= Length (C.Lines) then
+      if Prologue.Unit_Length = 0 or else Off + 3 > Length (C.Lines) then
          Done := True;
          return;
       end if;

Reply via email to