Re: [edk2] ANSI C bug in MdePkg ProcessorBind.h files for INT8.

Subject: Re: [edk2] ANSI C bug in MdePkg ProcessorBind.h files for INT8.

From: John Kearney <john.kearney@web.de>

To: edk2-devel@lists.sourceforge.net

Date: 2012-03-22 07:25:02

I quite literally talked about this earlier today on a embedded bulletin 
board.

Always be as explicit as possible in your code, especially C code.

so I always have
signed char vs unsigned char
signed int vs unsigned int
signed long vs unsigned long
signed long long vs unsigned long  long

in fact I also used signed in front of the internal variables, though 
maybe thats just my paranoia acting up again.

although technically they are redundant for int and up, it doesn't hurt 
and if nothing else it is a reminder to all of us and to noob that the 
non "u" ones are signed, and I swear I've seen option to switch int to 
unsigned default.

and the spec isn't all that explicit on this topic, more implies that 
clearly states what you should expect.


Am 21.03.2012 22:58, schrieb Kinney, Michael D:
> Cameron,
>
> Great information.  What do you think the best fix is here?  Andrew's proposal to use 'signed char' OR update tools_def.txt to specify -fsigned-char for GCC compilers?  It looks like a few of the compilers in tools_def.txt are already using -fsigned-char or its equivalent:
>
> DEFINE GCC_ARMGCC_CC_FLAGS         = DEF(GCC_ALL_CC_FLAGS) -mword-relocations -mlittle-endian -mabi=aapcs -mapcs -fno-short-enums -save-temps -fsigned-char  -ffunction-sections -fdata-sections -fomit-frame-pointer
>
> DEFINE RVCT_ALL_CC_FLAGS   = --c90 -c --no_autoinline --asm --gnu --apcs /interwork --signed_chars --no_unaligned_access --split_sections --enum_is_int --preinclude AutoGen.h --diag_warning 167 --diag_style=ide
>
> Thanks,
>
> Mike
>
> -----Original Message-----
> From: Cameron Esfahani [mailto:dirty@apple.com]
> Sent: Wednesday, March 21, 2012 2:09 PM
> To: edk2-devel@lists.sourceforge.net
> Subject: Re: [edk2] ANSI C bug in MdePkg ProcessorBind.h files for INT8.
>
> With gcc and related compilers, you can also pass C dialect option to indicate whether char should be signed or unsigned:
>
> -funsigned-char
> Let the type char be unsigned, like unsigned char.
> Each kind of machine has a default for what char should be. It is either like unsigned char by default or like signed char by default.
>
> Ideally, a portable program should always use signed char or unsigned char when it depends on the signedness of an object. But many programs have been written to use plain char and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default.
>
> The type char is always a distinct type from each of signed char or unsigned char, even though its behavior is always just like one of those two.
>
> -fsigned-char
> Let the type char be signed, like signed char.
> Note that this is equivalent to -fno-unsigned-char, which is the negative form of -funsigned-char. Likewise, the option -fno-signed-char is equivalent to -funsigned-char.
>
> Cameron Esfahani
>
> "There are times in the life of a nation when the only place a decent man can find himself is in prison."
>
>
>
> On Mar 21, 2012, at 2:00 PM, Andrew Fish wrote:
>
>> I was running into a strange situate where code was incorrectly getting optimized away. I tracked it down to a loop index variable that was defined as  INT8 and the loop index was -25 to 25. It looked like my compiler thinks a char is unsigned. When I looked this up in ANSI C the definition of a a char is that it can be either signed char, or unsigned.
>>
>> To fix this code bug I had to update the ProcessorBind.h files to:
>>   ///
>>   /// 1-byte signed value
>>   ///
>>   typedef signed char          INT8;
>>
>> for the non Microsoft specific path.
>>
>> ANSI C:
>> 15 The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.35)
>>
>> Andrew Fish
>>
>>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> This SF email is sponsosred by:
>> Try Windows Azure free for 90 days Click Here
>> http://p.sf.net/sfu/sfd2d-msazure
>> _______________________________________________
>> edk2-devel mailing list
>> edk2-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/edk2-devel
>
> ------------------------------------------------------------------------------
> This SF email is sponsosred by:
> Try Windows Azure free for 90 days Click Here
> http://p.sf.net/sfu/sfd2d-msazure
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/edk2-devel
>
> ------------------------------------------------------------------------------
> This SF email is sponsosred by:
> Try Windows Azure free for 90 days Click Here
> http://p.sf.net/sfu/sfd2d-msazure
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/edk2-devel


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel