mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: Linke Li <lilinke99@foxmail.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jan Kara <jack@suse.cz>, Linke Li <lilinke99@gmail.com>
Subject: Re: [PATCH] isofs: fix undefined behavior in iso_date()
Date: Sun, 9 Jul 2023 12:41:25 +0100	[thread overview]
Message-ID: <ZKqc5Uj14C7ST21K@casper.infradead.org> (raw)
In-Reply-To: <tencent_4D921A8D1F69E70C85C28875DC829E28EC09@qq.com>

On Sun, Jul 09, 2023 at 02:42:55PM +0800, Linke Li wrote:
> From: Linke Li <lilinke99@gmail.com>
> 
> 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.

  reply	other threads:[~2023-07-09 11:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-09  6:42 Linke Li
2023-07-09 11:41 ` Matthew Wilcox [this message]
2023-07-13  7:22   ` linke li
     [not found] ` <63c6f039-4b33-cdfc-1e49-fc9fc35d513e@web.de>
2023-07-10  6:10   ` linke li
2023-07-10  9:56 ` Dan Carpenter
2023-07-13 14:11   ` David Laight
2023-07-13 14:26     ` Dan Carpenter

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=ZKqc5Uj14C7ST21K@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=jack@suse.cz \
    --cc=lilinke99@foxmail.com \
    --cc=lilinke99@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®