This is the mail archive of the
newlib@sources.redhat.com
mailing list for the newlib project.
Re: [patch] memset.c: Make memset safe even if sizeof (int) = 2.
- From: Christopher Faylor <cgf at redhat dot com>
- To: Kazu Hirata <kazu at cs dot umass dot edu>
- Cc: newlib at sources dot redhat dot com
- Date: Mon, 25 Nov 2002 15:34:51 -0500
- Subject: Re: [patch] memset.c: Make memset safe even if sizeof (int) = 2.
- References: <20021125.152458.74754635.kazu@cs.umass.edu>
- Reply-to: newlib at sources dot redhat dot com
[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".
cgf