aarch64: fix off-by-one in reading cpuinfo

Message ID 20221003212419.3337714-1-philipp.tomsich@vrull.eu
State New
Headers
Series aarch64: fix off-by-one in reading cpuinfo |

Commit Message

Philipp Tomsich Oct. 3, 2022, 9:24 p.m. UTC
  Fixes: 341573406b39

Don't subtract one from the result of strnlen() when trying to point
to the first character after the current string.  This issue would
cause individual characters (where the 128 byte buffers are stitched
together) to be lost.

gcc/ChangeLog:

	* config/aarch64/driver-aarch64.cc (readline): Fix off-by-one.

Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>

---

 gcc/config/aarch64/driver-aarch64.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Richard Sandiford Oct. 4, 2022, 9:10 a.m. UTC | #1
Philipp Tomsich <philipp.tomsich@vrull.eu> writes:
> Fixes: 341573406b39
>
> Don't subtract one from the result of strnlen() when trying to point
> to the first character after the current string.  This issue would
> cause individual characters (where the 128 byte buffers are stitched
> together) to be lost.
>
> gcc/ChangeLog:
>
> 	* config/aarch64/driver-aarch64.cc (readline): Fix off-by-one.
>
> Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>

Thanks for the patch.  Would it be possible to create a testcase along
the lines of gcc.target/aarch64/cpunative/native_cpu_15.c?

Richard

> ---
>
>  gcc/config/aarch64/driver-aarch64.cc | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/config/aarch64/driver-aarch64.cc b/gcc/config/aarch64/driver-aarch64.cc
> index 52ff537908e..48250e68034 100644
> --- a/gcc/config/aarch64/driver-aarch64.cc
> +++ b/gcc/config/aarch64/driver-aarch64.cc
> @@ -203,9 +203,9 @@ readline (FILE *f)
>  	return std::string ();
>        /* If we're not at the end of the line then override the
>  	 \0 added by fgets.  */
> -      last = strnlen (buf, size) - 1;
> +      last = strnlen (buf, size);
>      }
> -  while (!feof (f) && buf[last] != '\n');
> +  while (!feof (f) && (last > 0 && buf[last - 1] != '\n'));
>  
>    std::string result (buf);
>    free (buf);
  

Patch

diff --git a/gcc/config/aarch64/driver-aarch64.cc b/gcc/config/aarch64/driver-aarch64.cc
index 52ff537908e..48250e68034 100644
--- a/gcc/config/aarch64/driver-aarch64.cc
+++ b/gcc/config/aarch64/driver-aarch64.cc
@@ -203,9 +203,9 @@  readline (FILE *f)
 	return std::string ();
       /* If we're not at the end of the line then override the
 	 \0 added by fgets.  */
-      last = strnlen (buf, size) - 1;
+      last = strnlen (buf, size);
     }
-  while (!feof (f) && buf[last] != '\n');
+  while (!feof (f) && (last > 0 && buf[last - 1] != '\n'));
 
   std::string result (buf);
   free (buf);