From: Matthew Wilcox <matthew@wil.cx>
To: Borislav Petkov <bp@amd64.org>
Cc: Ingo Molnar <mingo@elte.hu>, Dan Carpenter <error27@gmail.com>,
"Herrmann3, Andreas" <Andreas.Herrmann3@amd.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
"maintainer:X86 ARCHITECTURE..." <x86@kernel.org>,
"open list:AMD MICROCODE UPD..." <amd64-microcode@amd64.org>,
open list <linux-kernel@vger.kernel.org>,
"kernel-janitors@vger.kernel.org"
<kernel-janitors@vger.kernel.org>
Subject: Re: [patch -next] x86, microcode, AMD: signedness bug in
Date: Sun, 20 Feb 2011 17:50:11 +0000 [thread overview]
Message-ID: <20110220175011.GA13726@parisc-linux.org> (raw)
In-Reply-To: <20110220141452.GA12127@aftab>
On Sun, Feb 20, 2011 at 03:14:52PM +0100, Borislav Petkov wrote:
> int f() {
> return 0xa5a5a5a5;
> }
>
> int main()
> {
>
> char ret = f();
>
> printf("ret = 0x%016x\n", ret);
>
> return 0;
> }
> --
>
> doesn't cause a warning and prints a sign extended 0x00000000ffffffa5
> which is cast to the return type of the function. If ret is an unsigned
> char, then we return a 0x00000000000000a5.
>
> I found something about it in the C99 standard??, section "6.5.16.1 Simple
> assignment":
>
> 4. EXAMPLE 1 In the program fragment
>
> int f(void);
> char c;
> /* ... */
> if ((c = f()) = -1)
> /* ... */
>
> the int value returned by the function may be truncated when stored in
> the char, and then converted back to int width prior to the comparison.
> In an implementation in which ??????plain?????? char has the same range
> of values as unsigned char (and char is narrower than int), the result
> of the conversion cannot be negative, so the operands of the comparison
> can never compare equal. Therefore, for full portability, the variable c
> should be declared as int."
>
> so the whole "... may be truncated.. " could mean a lot of things. From
> my example above, gcc does truncate the int return type to a byte-sized
> char only when they differ in signedness.
No, that's not what's going on. GCC _is_ truncating to a byte, 0xa5,
whether it's signed or not. Then at the time of the call to printf,
the 0xa5 is cast to int. If the char is signed, 0xa5 is sign-extended;
if unsigned, it's zero-extended.
--
Matthew Wilcox Intel Open Source Technology Centre
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours. We can't possibly take such
a retrograde step."
WARNING: multiple messages have this Message-ID (diff)
From: Matthew Wilcox <matthew@wil.cx>
To: Borislav Petkov <bp@amd64.org>
Cc: Ingo Molnar <mingo@elte.hu>, Dan Carpenter <error27@gmail.com>,
"Herrmann3, Andreas" <Andreas.Herrmann3@amd.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
"maintainer:X86 ARCHITECTURE..." <x86@kernel.org>,
"open list:AMD MICROCODE UPD..." <amd64-microcode@amd64.org>,
open list <linux-kernel@vger.kernel.org>,
"kernel-janitors@vger.kernel.org"
<kernel-janitors@vger.kernel.org>
Subject: Re: [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode()
Date: Sun, 20 Feb 2011 10:50:11 -0700 [thread overview]
Message-ID: <20110220175011.GA13726@parisc-linux.org> (raw)
In-Reply-To: <20110220141452.GA12127@aftab>
On Sun, Feb 20, 2011 at 03:14:52PM +0100, Borislav Petkov wrote:
> int f() {
> return 0xa5a5a5a5;
> }
>
> int main()
> {
>
> char ret = f();
>
> printf("ret = 0x%016x\n", ret);
>
> return 0;
> }
> --
>
> doesn't cause a warning and prints a sign extended 0x00000000ffffffa5
> which is cast to the return type of the function. If ret is an unsigned
> char, then we return a 0x00000000000000a5.
>
> I found something about it in the C99 standard??, section "6.5.16.1 Simple
> assignment":
>
> 4. EXAMPLE 1 In the program fragment
>
> int f(void);
> char c;
> /* ... */
> if ((c = f()) == -1)
> /* ... */
>
> the int value returned by the function may be truncated when stored in
> the char, and then converted back to int width prior to the comparison.
> In an implementation in which ??????plain?????? char has the same range
> of values as unsigned char (and char is narrower than int), the result
> of the conversion cannot be negative, so the operands of the comparison
> can never compare equal. Therefore, for full portability, the variable c
> should be declared as int."
>
> so the whole "... may be truncated.. " could mean a lot of things. From
> my example above, gcc does truncate the int return type to a byte-sized
> char only when they differ in signedness.
No, that's not what's going on. GCC _is_ truncating to a byte, 0xa5,
whether it's signed or not. Then at the time of the call to printf,
the 0xa5 is cast to int. If the char is signed, 0xa5 is sign-extended;
if unsigned, it's zero-extended.
--
Matthew Wilcox Intel Open Source Technology Centre
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours. We can't possibly take such
a retrograde step."
next prev parent reply other threads:[~2011-02-20 17:50 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-18 9:17 [patch -next] x86, microcode, AMD: signedness bug in Dan Carpenter
2011-02-18 9:17 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Dan Carpenter
2011-02-18 9:39 ` [patch -next] x86, microcode, AMD: signedness bug in Borislav Petkov
2011-02-18 9:39 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Borislav Petkov
2011-02-20 13:02 ` [patch -next] x86, microcode, AMD: signedness bug in Ingo Molnar
2011-02-20 13:02 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Ingo Molnar
2011-02-20 13:09 ` [patch -next] x86, microcode, AMD: signedness bug in Dan Carpenter
2011-02-20 13:09 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Dan Carpenter
2011-02-20 14:14 ` [patch -next] x86, microcode, AMD: signedness bug in Borislav Petkov
2011-02-20 14:14 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Borislav Petkov
2011-02-20 17:50 ` Matthew Wilcox [this message]
2011-02-20 17:50 ` Matthew Wilcox
2011-02-20 18:08 ` [patch -next] x86, microcode, AMD: signedness bug in Borislav Petkov
2011-02-20 18:08 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Borislav Petkov
2011-02-20 18:42 ` [patch -next] x86, microcode, AMD: signedness bug in Matthew Wilcox
2011-02-20 18:42 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Matthew Wilcox
2011-02-20 19:33 ` [patch -next] x86, microcode, AMD: signedness bug in Borislav Petkov
2011-02-20 19:33 ` [patch -next] x86, microcode, AMD: signedness bug in generic_load_microcode() Borislav Petkov
2011-02-20 13:07 ` [tip:x86/microcode] x86, microcode, AMD: Fix " tip-bot for 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=20110220175011.GA13726@parisc-linux.org \
--to=matthew@wil.cx \
--cc=Andreas.Herrmann3@amd.com \
--cc=amd64-microcode@amd64.org \
--cc=bp@amd64.org \
--cc=error27@gmail.com \
--cc=hpa@zytor.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.