From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1923) id 941BE3885537; Wed, 26 Oct 2022 20:55:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 941BE3885537 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666817703; bh=9PxpRpLVFcjhxOC9ksyYKGqc6gBhF76g2s5LCt6fZiw=; h=From:To:Subject:Date:From; b=nWwE9IXZrSf0k1yO/NDUZp4omnhTNMJf/pEdvHqLyAmvk3t6fBnRrb4/EAlRf07Wz lhb8h4s83TnrHlteY2sgVUJytVTU6cxsrOxvZGqx4K+8qRe+3GnDO4IXjYFWKgMmIk AX3eO2u31qUIAOOaSO2PY9zHnYKCRoqnUXqU1w18= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Philipp Tomsich To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10338] aarch64: fix off-by-one in reading cpuinfo X-Act-Checkin: gcc X-Git-Author: Philipp Tomsich X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: f747b2ca817184add50953a52efcb77146904010 X-Git-Newrev: 531731c449d175c19a5c5a6e264a1bfe36f6c57e Message-Id: <20221026205503.941BE3885537@sourceware.org> Date: Wed, 26 Oct 2022 20:55:03 +0000 (GMT) List-Id: https://gcc.gnu.org/g:531731c449d175c19a5c5a6e264a1bfe36f6c57e commit r11-10338-g531731c449d175c19a5c5a6e264a1bfe36f6c57e Author: Philipp Tomsich Date: Mon Oct 3 21:59:50 2022 +0200 aarch64: fix off-by-one in reading cpuinfo 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.c (readline): Fix off-by-one. gcc/testsuite/ChangeLog: * gcc.target/aarch64/cpunative/info_18: New test. * gcc.target/aarch64/cpunative/native_cpu_18.c: New test. (cherry picked from commit b1cfbccc41de6aec950c0f662e7e85ab34bfff8a) Diff: --- gcc/config/aarch64/driver-aarch64.c | 4 ++-- gcc/testsuite/gcc.target/aarch64/cpunative/info_18 | 8 ++++++++ .../gcc.target/aarch64/cpunative/native_cpu_18.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/gcc/config/aarch64/driver-aarch64.c b/gcc/config/aarch64/driver-aarch64.c index e2935a11564..5505e117515 100644 --- a/gcc/config/aarch64/driver-aarch64.c +++ b/gcc/config/aarch64/driver-aarch64.c @@ -200,9 +200,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); diff --git a/gcc/testsuite/gcc.target/aarch64/cpunative/info_18 b/gcc/testsuite/gcc.target/aarch64/cpunative/info_18 new file mode 100644 index 00000000000..25061a4abe8 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/cpunative/info_18 @@ -0,0 +1,8 @@ +processor : 0 +BogoMIPS : 2000.00 +Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm ssbs sb dcpodp flagm2 frint i8mm bf16 rng ecv +CPU implementer : 0xc0 +CPU architecture: 8 +CPU variant : 0x0 +CPU part : 0xac3 +CPU revision : 0 diff --git a/gcc/testsuite/gcc.target/aarch64/cpunative/native_cpu_18.c b/gcc/testsuite/gcc.target/aarch64/cpunative/native_cpu_18.c new file mode 100644 index 00000000000..b5f0a3005f5 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/cpunative/native_cpu_18.c @@ -0,0 +1,15 @@ +/* { dg-do compile { target { { aarch64*-*-linux*} && native } } } */ +/* { dg-set-compiler-env-var GCC_CPUINFO "$srcdir/gcc.target/aarch64/cpunative/info_18" } */ +/* { dg-additional-options "-mcpu=native" } */ + +int main() +{ + return 0; +} + +/* { dg-final { scan-assembler {\.arch armv8.6-a\+crc\+fp16\+aes\+sha3\+rng} } } */ + +/* Test one where the boundary of buffer size would overwrite the last + character read when stitching the fgets-calls together. With the + test data provided, this would truncate the 'sha512' into 'ha512' + (dropping the 'sha3' feature). */