public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/vendors/redhat/heads/gcc-8-branch)] AArch64: Fix hwasan failure in readline.
Date: Thu, 17 Sep 2020 17:16:18 +0000 (GMT)	[thread overview]
Message-ID: <20200917171618.E2F713A4781A@sourceware.org> (raw)

https://gcc.gnu.org/g:97073a931a7b5659a51f9988ccb3f15a555f5f0a

commit 97073a931a7b5659a51f9988ccb3f15a555f5f0a
Author: Tamar Christina <tamar.christina@arm.com>
Date:   Mon Aug 3 12:03:17 2020 +0100

    AArch64: Fix hwasan failure in readline.
    
    My previous fix added an unchecked call to fgets in the new function readline.
    fgets can fail when there's an error reading the file in which case it returns
    NULL.  It also returns NULL when the next character is EOF.
    
    The EOF case is already covered by the existing code but the error case isn't.
    This fixes it by returning the empty string on error.
    
    Also I now use strnlen instead of strlen to make sure we never read outside the
    buffer.
    
    This was flagged by Matthew Malcomson during his hwasan work.
    
    gcc/ChangeLog:
    
            * config/aarch64/driver-aarch64.c (readline): Check return value fgets.
    
    (cherry picked from commit 341573406b392f4d57e052ce22f80e85a7c479e9)

Diff:
---
 gcc/config/aarch64/driver-aarch64.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/gcc/config/aarch64/driver-aarch64.c b/gcc/config/aarch64/driver-aarch64.c
index 6dc7aa6d3be..4c1e42edff8 100644
--- a/gcc/config/aarch64/driver-aarch64.c
+++ b/gcc/config/aarch64/driver-aarch64.c
@@ -194,10 +194,16 @@ readline (FILE *f)
       size += buf_size;
       buf = (char*) xrealloc (buf, size);
       gcc_assert (buf);
-      fgets (buf + last, buf_size, f);
+      /* If fgets fails it returns NULL, but if it reaches EOF
+	 with 0 characters read it also returns EOF.  However
+	 the condition on the loop would have broken out of the
+	 loop in that case,  and if we are in the first iteration
+	 then the empty string is the correct thing to return.  */
+      if (!fgets (buf + last, buf_size, f))
+	return std::string ();
       /* If we're not at the end of the line then override the
 	 \0 added by fgets.  */
-      last = strlen (buf) - 1;
+      last = strnlen (buf, size) - 1;
     }
   while (!feof (f) && buf[last] != '\n');


                 reply	other threads:[~2020-09-17 17:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200917171618.E2F713A4781A@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).