lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <f25545e96bc1458eb5bffc1505255e68@AcuMS.aculab.com>
Date:   Mon, 22 May 2023 12:07:23 +0000
From:   David Laight <David.Laight@...LAB.COM>
To:     "'15330273260@....cn'" <15330273260@....cn>,
        Jani Nikula <jani.nikula@...ux.intel.com>,
        Li Yi <liyi@...ngson.cn>
CC:     Thomas Zimmermann <tzimmermann@...e.de>,
        "linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
        "dri-devel@...ts.freedesktop.org" <dri-devel@...ts.freedesktop.org>,
        "loongson-kernel@...ts.loongnix.cn" 
        <loongson-kernel@...ts.loongnix.cn>
Subject: RE: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast

From: 15330273260@....cn <15330273260@....cn>
> Sent: 22 May 2023 12:56
...
> > I'll bet most people will be surprised to see what this prints:
> >
> > #include <stdio.h>
> > #include <stdint.h>
> >
> > int main(void)
> > {
> > 	uint16_t x = 0xffff;
> > 	uint16_t y = 0xffff;
> > 	uint64_t z = x * y;
> >
> > 	printf("0x%016lx\n", z);
> > 	printf("%ld\n", z);
> 
> Here, please replace the "%ld\n" with the "%lu\n", then you will see the
> difference.
> 
> you are casting the variable 'z' to signed value,  "%d" is for printing
> signed value, and "%u" is for printing unsigned value.

That makes very little difference on 2's compliment systems.
They both display the contents of the variable.

> Your simple code explained exactly why you are still in confusion,
> 
> that is u16 * u16  can yield a negative value if you use the int as the
> return type. Because it overflowed.

There is no 'return type', the type of 'u16 * u16' is signed int.
When 'signed int' is promoted/cast to u64 it is first sign extended
to 64 bits.

You can get what you want/expect by either forcing an unsigned multiply
or by explicitly casting the result of the multiply to u32.
So the product in 'z = (x + 0u) * y' is 'unsigned int' it gets
promoted to int64_t (ie a signed type) and then converted to
unsigned.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ