diff mbox

[Ada] Avoid reading past end of file when checking for BOM

Message ID 20130910152139.GA22055@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Sept. 10, 2013, 3:21 p.m. UTC
This patch makes sure that the routine Check_For_BOM cannot
read past the end of file. In practice it is probably the
case that this cannot cause a real error, but valgrind can
see that this is happening. So this change will avoid the
annoying false positive from valgrind. It's not worth setting
up a valgrind test for this very minor issue, and there is
no way to generate a real test that fails, so no test.

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

2013-09-10  Robert Dewar  <dewar@adacore.com>

	* sinput.adb (Check_For_BOM): Avoid reading past end of file.
diff mbox

Patch

Index: sinput.adb
===================================================================
--- sinput.adb	(revision 202451)
+++ sinput.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2013, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -258,10 +258,20 @@ 
       BOM : BOM_Kind;
       Len : Natural;
       Tst : String (1 .. 5);
+      C   : Character;
 
    begin
       for J in 1 .. 5 loop
-         Tst (J) := Source (Scan_Ptr + Source_Ptr (J) - 1);
+         C := Source (Scan_Ptr + Source_Ptr (J) - 1);
+
+         --  Definitely no BOM if EOF character marks either end of file, or
+         --  an illegal non-BOM character if not at the end of file.
+
+         if C = EOF then
+            return;
+         end if;
+
+         Tst (J) := C;
       end loop;
 
       Read_BOM (Tst, Len, BOM, False);