lib/xz/xz_dec_bcj.c | 2 +- lib/xz/xz_dec_lzma2.c | 11 +++++------ lib/xz/xz_dec_stream.c | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-)
From: Thorsten Blum <thorsten.blum@linux.dev>
Use the simpler min() macro since the values are unsigned and
compatible.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Reviewed-by: Lasse Collin <lasse.collin@tukaani.org>
Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
---
lib/xz/xz_dec_bcj.c | 2 +-
lib/xz/xz_dec_lzma2.c | 11 +++++------
lib/xz/xz_dec_stream.c | 4 ++--
3 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/lib/xz/xz_dec_bcj.c b/lib/xz/xz_dec_bcj.c
index cc49a300a5b2..88922323f96e 100644
--- a/lib/xz/xz_dec_bcj.c
+++ b/lib/xz/xz_dec_bcj.c
@@ -466,7 +466,7 @@ static void bcj_flush(struct xz_dec_bcj *s, struct xz_buf *b)
{
size_t copy_size;
- copy_size = min_t(size_t, s->temp.filtered, b->out_size - b->out_pos);
+ copy_size = min(s->temp.filtered, b->out_size - b->out_pos);
memcpy(b->out + b->out_pos, s->temp.buf, copy_size);
b->out_pos += copy_size;
diff --git a/lib/xz/xz_dec_lzma2.c b/lib/xz/xz_dec_lzma2.c
index 4b783ac94e71..9d80342b9c6b 100644
--- a/lib/xz/xz_dec_lzma2.c
+++ b/lib/xz/xz_dec_lzma2.c
@@ -354,7 +354,7 @@ static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
if (dist >= dict->full || dist >= dict->size)
return false;
- left = min_t(size_t, dict->limit - dict->pos, *len);
+ left = min(dict->limit - dict->pos, *len);
*len -= left;
back = dict->pos - dist - 1;
@@ -1098,9 +1098,8 @@ enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b)
* the output buffer yet, we may run this loop
* multiple times without changing s->lzma2.sequence.
*/
- dict_limit(&s->dict, min_t(size_t,
- b->out_size - b->out_pos,
- s->lzma2.uncompressed));
+ dict_limit(&s->dict, min(b->out_size - b->out_pos,
+ s->lzma2.uncompressed));
if (!lzma2_lzma(s, b))
return XZ_DATA_ERROR;
@@ -1260,8 +1259,8 @@ enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s_ptr,
s->dict.end = b->out_size - b->out_pos;
while (true) {
- dict_limit(&s->dict, min_t(size_t, b->out_size - b->out_pos,
- s->lzma2.uncompressed));
+ dict_limit(&s->dict, min(b->out_size - b->out_pos,
+ s->lzma2.uncompressed));
if (!lzma2_lzma(s, b))
return XZ_DATA_ERROR;
diff --git a/lib/xz/xz_dec_stream.c b/lib/xz/xz_dec_stream.c
index 59bfd54ffee7..0bed6daefac2 100644
--- a/lib/xz/xz_dec_stream.c
+++ b/lib/xz/xz_dec_stream.c
@@ -155,8 +155,8 @@ static const uint8_t check_sizes[16] = {
*/
static bool fill_temp(struct xz_dec *s, struct xz_buf *b)
{
- size_t copy_size = min_t(size_t,
- b->in_size - b->in_pos, s->temp.size - s->temp.pos);
+ size_t copy_size = min(b->in_size - b->in_pos,
+ s->temp.size - s->temp.pos);
memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
b->in_pos += copy_size;
--
2.54.0
On Tue, Jun 09, 2026 at 06:00:28PM +0300, Lasse Collin wrote:
> From: Thorsten Blum <thorsten.blum@linux.dev>
>
> Use the simpler min() macro since the values are unsigned and
> compatible.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> Reviewed-by: Lasse Collin <lasse.collin@tukaani.org>
> Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
...
> diff --git a/lib/xz/xz_dec_lzma2.c b/lib/xz/xz_dec_lzma2.c
> index 4b783ac94e71..9d80342b9c6b 100644
> --- a/lib/xz/xz_dec_lzma2.c
> +++ b/lib/xz/xz_dec_lzma2.c
> @@ -354,7 +354,7 @@ static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
> if (dist >= dict->full || dist >= dict->size)
> return false;
>
> - left = min_t(size_t, dict->limit - dict->pos, *len);
> + left = min(dict->limit - dict->pos, *len);
> *len -= left;
>
> back = dict->pos - dist - 1;
> @@ -1098,9 +1098,8 @@ enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b)
> * the output buffer yet, we may run this loop
> * multiple times without changing s->lzma2.sequence.
> */
> - dict_limit(&s->dict, min_t(size_t,
> - b->out_size - b->out_pos,
> - s->lzma2.uncompressed));
> + dict_limit(&s->dict, min(b->out_size - b->out_pos,
> + s->lzma2.uncompressed));
> if (!lzma2_lzma(s, b))
> return XZ_DATA_ERROR;
>
These two hunks from this change in -next as 1003161e12ac ("lib/xz:
replace min_t with min") cause warnings in the arch/powerpc/boot code,
as it uses an old, simple version of min() and max() that does not have
the improvements done in d03eba99f5bf ("minmax: allow
min()/max()/clamp() if the arguments have the same signedness."):
In file included from arch/powerpc/boot/ops.h:13,
from arch/powerpc/boot/decompress.c:12:
arch/powerpc/boot/../../../lib/xz/xz_dec_lzma2.c: In function 'dict_repeat':
arch/powerpc/boot/types.h:31:21: warning: comparison of distinct pointer types lacks a cast [-Wcompare-distinct-pointer-types]
31 | (void) (&_x == &_y); \
| ^~
arch/powerpc/boot/../../../lib/xz/xz_dec_lzma2.c:357:16: note: in expansion of macro 'min'
357 | left = min(dict->limit - dict->pos, *len);
| ^~~
arch/powerpc/boot/../../../lib/xz/xz_dec_lzma2.c: In function 'xz_dec_lzma2_run':
arch/powerpc/boot/types.h:31:21: warning: comparison of distinct pointer types lacks a cast [-Wcompare-distinct-pointer-types]
31 | (void) (&_x == &_y); \
| ^~
arch/powerpc/boot/../../../lib/xz/xz_dec_lzma2.c:1101:46: note: in expansion of macro 'min'
1101 | dict_limit(&s->dict, min(b->out_size - b->out_pos,
| ^~~
We could try to update arch/powerpc/boot/types.h with the current min()
and max() versions but that will require dragging in many other macros.
Could try to make the Linux headers work as is to reduce duplication but
that is likely quite involved (and maybe even undesirable). Backing out
of these two changes is likely a better solution, at least for now.
--
Cheers,
Nathan
On Wed, 10 Jun 2026 16:23:23 -0700 Nathan Chancellor <nathan@kernel.org> wrote:
> On Tue, Jun 09, 2026 at 06:00:28PM +0300, Lasse Collin wrote:
> > From: Thorsten Blum <thorsten.blum@linux.dev>
> >
> > Use the simpler min() macro since the values are unsigned and
> > compatible.
> >
> > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> > Reviewed-by: Lasse Collin <lasse.collin@tukaani.org>
> > Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
> ...
> > diff --git a/lib/xz/xz_dec_lzma2.c b/lib/xz/xz_dec_lzma2.c
> > index 4b783ac94e71..9d80342b9c6b 100644
> > --- a/lib/xz/xz_dec_lzma2.c
> > +++ b/lib/xz/xz_dec_lzma2.c
> > @@ -354,7 +354,7 @@ static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
> > if (dist >= dict->full || dist >= dict->size)
> > return false;
> >
> > - left = min_t(size_t, dict->limit - dict->pos, *len);
> > + left = min(dict->limit - dict->pos, *len);
> > *len -= left;
> >
> > back = dict->pos - dist - 1;
> > @@ -1098,9 +1098,8 @@ enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b)
> > * the output buffer yet, we may run this loop
> > * multiple times without changing s->lzma2.sequence.
> > */
> > - dict_limit(&s->dict, min_t(size_t,
> > - b->out_size - b->out_pos,
> > - s->lzma2.uncompressed));
> > + dict_limit(&s->dict, min(b->out_size - b->out_pos,
> > + s->lzma2.uncompressed));
> > if (!lzma2_lzma(s, b))
> > return XZ_DATA_ERROR;
> >
>
> These two hunks from this change in -next as 1003161e12ac ("lib/xz:
> replace min_t with min") cause warnings in the arch/powerpc/boot code,
> as it uses an old, simple version of min() and max() that does not have
> the improvements done in d03eba99f5bf ("minmax: allow
> min()/max()/clamp() if the arguments have the same signedness."):
Well that's annoying.
A pleasing solution would be to make the xz code be more consistent in its
type usage. struct lzma_dec has a liking for uintXX_t whereas struct
dictionary likes size_t. Perhaps a fundamental reexamination of what
these fields are representing is in order?
On 2026-06-10 Andrew Morton wrote:
> On Wed, 10 Jun 2026 16:23:23 -0700 Nathan Chancellor
> <nathan@kernel.org> wrote:
> > These two hunks from this change in -next as 1003161e12ac ("lib/xz:
> > replace min_t with min") cause warnings in the arch/powerpc/boot
> > code, as it uses an old, simple version of min() and max() that
> > does not have the improvements done in d03eba99f5bf ("minmax: allow
> > min()/max()/clamp() if the arguments have the same signedness."):
>
> Well that's annoying.
>
> A pleasing solution would be to make the xz code be more consistent
> in its type usage. struct lzma_dec has a liking for uintXX_t whereas
> struct dictionary likes size_t. Perhaps a fundamental reexamination
> of what these fields are representing is in order?
I tried to rethink it.
Callers use struct xz_buf to pass input and output buffers. That struct
uses size_t. I assume it should stay that way because the existing
callers assign size_t values to struct xz_buf members.
The input buffer members from struct xz_buf are assigned to members of
struct rc_dec, which is why size_t is needed in struct rc_dec.
In single-call mode (XZ_SINGLE), the decoder writes directly to the
caller-specified output buffer. In this case the output buffer members
from struct xz_buf are assigned to struct dictionary, and thus multiple
members in struct dictionary have to be size_t. In multi-call mode
(XZ_PREALLOC or XZ_DYNALLOC) the dictionary buffer is private to the
decoder and then uint32_t would suffice in struct dictionary.
Otherwise xz_dec_lzma2.c would be fine without size_t.
A few uint32_t uses could be changed to size_t to avoid the min()
issue, and slightly cleanup uint32_t vs. size_t usage otherwise too.
The first attached draft patch does this.
Even all uint32_t uses could be changed to size_t like in the second
patch, but it looks confusing in situations when a value isn't a size
or offset. It also increases the sizes of structs more.
I don't like the second patch, but I will submit the first one in a day
or two if I still think it's OK.
--
Lasse Collin
On Wed, 10 Jun 2026 16:48:00 -0700
Andrew Morton <akpm@linux-foundation.org> wrote:
> On Wed, 10 Jun 2026 16:23:23 -0700 Nathan Chancellor <nathan@kernel.org> wrote:
>
> > On Tue, Jun 09, 2026 at 06:00:28PM +0300, Lasse Collin wrote:
> > > From: Thorsten Blum <thorsten.blum@linux.dev>
> > >
> > > Use the simpler min() macro since the values are unsigned and
> > > compatible.
> > >
> > > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> > > Reviewed-by: Lasse Collin <lasse.collin@tukaani.org>
> > > Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
> > ...
> > > diff --git a/lib/xz/xz_dec_lzma2.c b/lib/xz/xz_dec_lzma2.c
> > > index 4b783ac94e71..9d80342b9c6b 100644
> > > --- a/lib/xz/xz_dec_lzma2.c
> > > +++ b/lib/xz/xz_dec_lzma2.c
> > > @@ -354,7 +354,7 @@ static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
> > > if (dist >= dict->full || dist >= dict->size)
> > > return false;
> > >
> > > - left = min_t(size_t, dict->limit - dict->pos, *len);
> > > + left = min(dict->limit - dict->pos, *len);
> > > *len -= left;
> > >
> > > back = dict->pos - dist - 1;
> > > @@ -1098,9 +1098,8 @@ enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b)
> > > * the output buffer yet, we may run this loop
> > > * multiple times without changing s->lzma2.sequence.
> > > */
> > > - dict_limit(&s->dict, min_t(size_t,
> > > - b->out_size - b->out_pos,
> > > - s->lzma2.uncompressed));
> > > + dict_limit(&s->dict, min(b->out_size - b->out_pos,
> > > + s->lzma2.uncompressed));
> > > if (!lzma2_lzma(s, b))
> > > return XZ_DATA_ERROR;
> > >
> >
> > These two hunks from this change in -next as 1003161e12ac ("lib/xz:
> > replace min_t with min") cause warnings in the arch/powerpc/boot code,
> > as it uses an old, simple version of min() and max() that does not have
> > the improvements done in d03eba99f5bf ("minmax: allow
> > min()/max()/clamp() if the arguments have the same signedness."):
>
> Well that's annoying.
>
> A pleasing solution would be to make the xz code be more consistent in its
> type usage. struct lzma_dec has a liking for uintXX_t whereas struct
> dictionary likes size_t. Perhaps a fundamental reexamination of what
> these fields are representing is in order?
>
>
Or change the ppc boot definitions to simple ones like:
#define min(a, b) ({ \
auto _a = a; \
auto _b = b; \
_a < _b ? _a : _b; \
})
That remove the type check (fairly pointless!).
(There isn't even a commit message that says someone got bitten
by a negative value becoming a very large one.)
-- David
© 2016 - 2026 Red Hat, Inc.