public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
[parent not found: <20240206113813.26DCA3858D37@sourceware.org>]
* [PATCH] middle-end/113576 - avoid out-of-bound vector element access
@ 2024-02-06 11:37 Richard Biener
  0 siblings, 0 replies; 8+ messages in thread
From: Richard Biener @ 2024-02-06 11:37 UTC (permalink / raw)
  To: gcc-patches; +Cc: richard.sandiford

The following avoids accessing out-of-bound vector elements when
native encoding a boolean vector with sub-BITS_PER_UNIT precision
elements.  The error was basing the number of elements to extract
on the rounded up total byte size involved and the patch bases
everything on the total number of elements to extract instead.

As a side-effect this now consistently results in zeros in the
padding of the last encoded byte which also avoids the failure
mode seen in PR113576.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

OK?

Thanks,
Richard.

	PR middle-end/113576
	* fold-const.cc (native_encode_vector_part): Avoid accessing
	out-of-bound elements.
---
 gcc/fold-const.cc | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 80e211e18c0..8638757312b 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -8057,13 +8057,13 @@ native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
 	off = 0;
 
       /* Zero the buffer and then set bits later where necessary.  */
-      int extract_bytes = MIN (len, total_bytes - off);
+      unsigned elts_per_byte = BITS_PER_UNIT / elt_bits;
+      unsigned first_elt = off * elts_per_byte;
+      unsigned extract_elts = MIN (len * elts_per_byte, count - first_elt);
+      unsigned extract_bytes = CEIL (elt_bits * extract_elts, BITS_PER_UNIT);
       if (ptr)
 	memset (ptr, 0, extract_bytes);
 
-      unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
-      unsigned int first_elt = off * elts_per_byte;
-      unsigned int extract_elts = extract_bytes * elts_per_byte;
       for (unsigned int i = 0; i < extract_elts; ++i)
 	{
 	  tree elt = VECTOR_CST_ELT (expr, first_elt + i);
-- 
2.35.3

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-03-05 14:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240206113819.2335F3858402@sourceware.org>
2024-02-14 11:31 ` [PATCH] middle-end/113576 - avoid out-of-bound vector element access Richard Sandiford
2024-02-14 12:34   ` Richard Biener
2024-02-14 15:42     ` Richard Sandiford
2024-02-15  8:12       ` Richard Biener
2024-02-15  9:46         ` Richard Sandiford
2024-02-15 10:12           ` Richard Biener
     [not found] <20240206113813.26DCA3858D37@sourceware.org>
2024-03-05 14:39 ` Jeff Law
2024-02-06 11:37 Richard Biener

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).