From: Lasse Collin <lasse.collin@tukaani.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Lasse Collin <lasse.collin@tukaani.org>,
Jia Tan <jiat0218@gmail.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH 07/11] xz: Optimize for-loop conditions in the BCJ decoders
Date: Wed, 20 Mar 2024 20:38:40 +0200 [thread overview]
Message-ID: <20240320183846.19475-8-lasse.collin@tukaani.org> (raw)
In-Reply-To: <20240320183846.19475-1-lasse.collin@tukaani.org>
Compilers cannot optimize the addition "i + 4" away since theoretically
it could overflow.
Reviewed-by: Jia Tan <jiat0218@gmail.com>
Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
---
lib/xz/xz_dec_bcj.c | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/lib/xz/xz_dec_bcj.c b/lib/xz/xz_dec_bcj.c
index ab9237ed6db8..e0b4bf4999c0 100644
--- a/lib/xz/xz_dec_bcj.c
+++ b/lib/xz/xz_dec_bcj.c
@@ -161,7 +161,9 @@ static size_t bcj_powerpc(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
size_t i;
uint32_t instr;
- for (i = 0; i + 4 <= size; i += 4) {
+ size &= ~(size_t)3;
+
+ for (i = 0; i < size; i += 4) {
instr = get_unaligned_be32(buf + i);
if ((instr & 0xFC000003) == 0x48000001) {
instr &= 0x03FFFFFC;
@@ -218,7 +220,9 @@ static size_t bcj_ia64(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
/* Instruction normalized with bit_res for easier manipulation */
uint64_t norm;
- for (i = 0; i + 16 <= size; i += 16) {
+ size &= ~(size_t)15;
+
+ for (i = 0; i < size; i += 16) {
mask = branch_table[buf[i] & 0x1F];
for (slot = 0, bit_pos = 5; slot < 3; ++slot, bit_pos += 41) {
if (((mask >> slot) & 1) == 0)
@@ -266,7 +270,9 @@ static size_t bcj_arm(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
size_t i;
uint32_t addr;
- for (i = 0; i + 4 <= size; i += 4) {
+ size &= ~(size_t)3;
+
+ for (i = 0; i < size; i += 4) {
if (buf[i + 3] == 0xEB) {
addr = (uint32_t)buf[i] | ((uint32_t)buf[i + 1] << 8)
| ((uint32_t)buf[i + 2] << 16);
@@ -289,7 +295,12 @@ static size_t bcj_armthumb(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
size_t i;
uint32_t addr;
- for (i = 0; i + 4 <= size; i += 2) {
+ if (size < 4)
+ return 0;
+
+ size -= 4;
+
+ for (i = 0; i <= size; i += 2) {
if ((buf[i + 1] & 0xF8) == 0xF0
&& (buf[i + 3] & 0xF8) == 0xF8) {
addr = (((uint32_t)buf[i + 1] & 0x07) << 19)
@@ -317,7 +328,9 @@ static size_t bcj_sparc(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
size_t i;
uint32_t instr;
- for (i = 0; i + 4 <= size; i += 4) {
+ size &= ~(size_t)3;
+
+ for (i = 0; i < size; i += 4) {
instr = get_unaligned_be32(buf + i);
if ((instr >> 22) == 0x100 || (instr >> 22) == 0x1FF) {
instr <<= 2;
--
2.44.0
next prev parent reply other threads:[~2024-03-20 18:47 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-20 18:38 [PATCH 00/11] xz: Updates to license, filters, and compression options Lasse Collin
2024-03-20 18:38 ` [PATCH 01/11] MAINTAINERS: Add XZ Embedded maintainers Lasse Collin
2024-03-20 18:38 ` [PATCH 02/11] LICENSES: Add 0BSD license text Lasse Collin
2024-03-20 18:38 ` [PATCH 03/11] xz: Switch from public domain to BSD Zero Clause License (0BSD) Lasse Collin
2024-03-20 18:38 ` [PATCH 04/11] xz: Documentation/staging/xz.rst: Revise thoroughly Lasse Collin
2024-03-20 18:38 ` [PATCH 05/11] xz: Fix comments and coding style Lasse Collin
2024-03-20 18:38 ` [PATCH 06/11] xz: Cleanup CRC32 edits from 2018 Lasse Collin
2024-03-20 18:38 ` Lasse Collin [this message]
2024-03-20 18:38 ` [PATCH 08/11] xz: Add ARM64 BCJ filter Lasse Collin
2024-03-20 18:38 ` [PATCH 09/11] xz: Add RISC-V " Lasse Collin
2024-03-20 18:38 ` [PATCH 10/11] xz: Use 128 MiB dictionary and force single-threaded mode Lasse Collin
2024-03-20 18:38 ` [PATCH 11/11] xz: Adjust arch-specific options for better kernel compression Lasse Collin
2024-03-31 0:42 ` angel.lkml
2024-04-03 19:59 ` Lasse Collin
2024-04-04 14:01 ` Lasse Collin
2024-03-29 19:24 ` [PATCH 00/11] xz: Updates to license, filters, and compression options Jonathan Bennett
2024-03-29 19:32 ` Kees Cook
2024-03-29 20:51 ` [tech-board] " Jonathan Corbet
2024-03-30 0:37 ` Kees Cook
2024-03-30 2:56 ` [tech-board] " Andrew Morton
2024-03-30 12:48 ` Lasse Collin
2024-03-30 13:54 ` Kees Cook
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=20240320183846.19475-8-lasse.collin@tukaani.org \
--to=lasse.collin@tukaani.org \
--cc=akpm@linux-foundation.org \
--cc=jiat0218@gmail.com \
--cc=linux-kernel@vger.kernel.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 NNTP newsgroup(s).