[patch] memset.c: Make memset safe even if sizeof (int) = 2.

Christopher Faylor cgf@redhat.com
Mon Nov 25 12:34:00 GMT 2002

[taking over for Jeff Johnston for a week or so]
On Mon, Nov 25, 2002 at 03:24:58PM -0500, Kazu Hirata wrote:
>Attached is a patch to make memset safe even if sizeof (int) = 2.
>A part of memset says
>  buffer = (c << 8) | c;
>  buffer |= (buffer << 16);
>Playing with H8 port, where sizeof (int) = 2, shows that if c = 0x80
>is given, then the value of buffer would be 0xffff8080 instead of
>0x80808080 because "(c << 8) | c", which is 0x8080, is sign-extended
>to a 32-bit signed integer.

This looks like a good catch, but the usual idiom for this is to do
something like:

           unsigned int d = c & 0xff;

to insure that there is no sign extension.

If you agree with this, then feel free to check in a variation on your
below patch.  Btw, could you fix the ChangeLog?  It seems to have a
typo "sizeof (2) = 2".


More information about the Newlib mailing list