diff mbox

[Ada] Fix bug on Get_Line when incomplete last line in file

Message ID 20160502091109.GA61088@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet May 2, 2016, 9:11 a.m. UTC
It may occur in some occasions that Get_Line incorrectly sets its Last
parameter to one past the correct value. This can only occur when the
line being copied is the last line of the file, and does not contain
the newline character.

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

2016-05-02  Yannick Moy  <moy@adacore.com>

	* a-tigeli.adb (Get_Line): Fix bound for test to
	decide when to compensate for character 0 added by call to fgets.
diff mbox

Patch

Index: a-tigeli.adb
===================================================================
--- a-tigeli.adb	(revision 235706)
+++ a-tigeli.adb	(working copy)
@@ -120,10 +120,15 @@ 
             K : Natural := Natural (P - S);
 
          begin
-            --  Now Buf (K + 2) should be 0, or otherwise Buf (K) is the 0
-            --  put in by fgets, so compensate.
+            --  If K + 2 is greater than N, then Buf (K + 1) cannot be a LM
+            --  character from the source file, as the call to fgets copied at
+            --  most N - 1 characters. Otherwise, either LM is a character from
+            --  the source file and then Buf (K + 2) should be 0, or LM is a
+            --  character put in Buf by memset and then Buf (K) is the 0 put in
+            --  by fgets. In both cases where LM does not come from the source
+            --  file, compensate.
 
-            if K + 2 > Buf'Last or else Buf (K + 2) /= ASCII.NUL then
+            if K + 2 > N or else Buf (K + 2) /= ASCII.NUL then
 
                --  Incomplete last line, so remove the extra 0