aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorTamar Christina <tamar.christina@arm.com>2020-08-03 12:03:17 +0100
committerTamar Christina <tamar.christina@arm.com>2020-08-03 12:03:17 +0100
commit341573406b392f4d57e052ce22f80e85a7c479e9 (patch)
tree8725f5eb73cfacc8688e04059387f26f88d729ad /gcc
parentf2ec836aa1d6e2ed4fe286ffa661050888f652d1 (diff)
downloadgcc-341573406b392f4d57e052ce22f80e85a7c479e9.zip
gcc-341573406b392f4d57e052ce22f80e85a7c479e9.tar.gz
gcc-341573406b392f4d57e052ce22f80e85a7c479e9.tar.bz2
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/config/aarch64/driver-aarch64.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/gcc/config/aarch64/driver-aarch64.c b/gcc/config/aarch64/driver-aarch64.c
index 0c70629..d68a725 100644
--- a/gcc/config/aarch64/driver-aarch64.c
+++ b/gcc/config/aarch64/driver-aarch64.c
@@ -191,10 +191,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');