Re: [PATCH] isofs: fix undefined behavior in iso_date()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Jul 09, 2023 at 02:42:55PM +0800, Linke Li wrote:
> From: Linke Li <lilinke99@xxxxxxxxx>
> 
> Fix undefined behavior in the code by properly handling the left shift operaion.
> Instead of left-shifting a negative value, explicitly cast -1 to an unsigned int
> before the shift. This ensures well defined behavior and resolves any potential
> issues.

This certainly fixes the problem, but wouldn't it be easier to get the
compiler to do the work for us?

#include <stdio.h>

int f(unsigned char *p)
{
	return (signed char)p[0];
}

int main(void)
{
	unsigned char x = 0xa5;

	printf("%d\n", f(&x));

	return 0;
}

prints -91.



[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux