Re: [PATCH] drm/xe: use %z format string for ptrdiff_t

From: David Laight

Date: Tue Mar 17 2026 - 12:51:59 EST


On Tue, 17 Mar 2026 14:56:47 +0000
David Laight <david.laight.linux@xxxxxxxxx> wrote:

> On Tue, 17 Mar 2026 00:07:57 +0100
> "Arnd Bergmann" <arnd@xxxxxxxx> wrote:
>
> > On Mon, Mar 16, 2026, at 23:51, Matt Roper wrote:
> > > On Mon, Mar 16, 2026 at 11:43:46PM +0100, Arnd Bergmann wrote:
> > >> From: Arnd Bergmann <arnd@xxxxxxxx>
> > >>
> > >> ptrdiff_t, size_t and long are the same size on all supported architectures,
> > >> but gcc requires the use of the %zx modifier instead of %lx. On 32-bit
> > >
> > > Nathan Chancellor just sent a fix here:
> > >
> > > https://lore.kernel.org/all/20260316-drm-xe-fix-32-bit-wformat-ptrdiff-v1-1-0108b10b2b6b@xxxxxxxxxx/
> > >
> > > His solution uses %tx rather than %zx, which according to
> > > Documentation/core-api/printk-formats.rst sounds like it's probably the
> > > most accurate format string for this case?
> > >
> >
> > Right, Nathan's version is correct. I wasn't aware that size_t
> > and ptrdiff_t have different modifiers.
>
> Consider:
> void foo(size_t len)
> {
> char *p = malloc(len);
> char *end = p + len;
> ptrdiff_t diff = p - end;
> printf("len %zu, diff %td\n", len, diff);
> free(p);
> }
> Clearly foo(5) output "5 -5".
> But what about foo(5u << 29) on a 32bit system with a large unlimit and
> a big enough hole in the address space.
> More likely would be something running on an x86 in real mode.
> There malloc(65000) might be reasonable, so size_t would be unsigned int
> but ptrdiff_t would need to be a signed 32bit type.
>
> Of course, in linux, they are all 'long'.

I should have said 'the same size as long'.

>
> David
>
>
> >
> > Arnd
> >
>
>