This is the mail archive of the
newlib@sources.redhat.com
mailing list for the newlib project.
[patch] memset.c: Make memset safe even if sizeof (int) = 2.
- From: Kazu Hirata <kazu at cs dot umass dot edu>
- To: newlib at sources dot redhat dot com
- Date: Mon, 25 Nov 2002 15:24:58 -0500 (EST)
- Subject: [patch] memset.c: Make memset safe even if sizeof (int) = 2.
Hi,
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.
I don't know the exact specification of the C language as to whether a
value should be sign-extended when the resulting type is unsigned, but
the patch makes this issue irrelevant by first making a copy of c into
d, which is unsigned.
OK to apply?
Thanks,
p.s.
Actually, the H8 port does not use this memset because newlib has the
assembly language version for H8, but the patch should be useful for
other ports.
Kazu Hirata
2002-11-25 Kazu Hirata <kazu@cs.umass.edu>
* libc/string/memset.c (memset): Make it safe even if
sizeof (2) = 2.
Index: memset.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/string/memset.c,v
retrieving revision 1.2
diff -c -r1.2 memset.c
*** memset.c 24 Aug 2000 16:25:36 -0000 1.2
--- memset.c 25 Nov 2002 20:17:27 -0000
***************
*** 71,77 ****
c &= 0xff;
if (LBLOCKSIZE == 4)
{
! buffer = (c << 8) | c;
buffer |= (buffer << 16);
}
else
--- 71,78 ----
c &= 0xff;
if (LBLOCKSIZE == 4)
{
! unsigned int d = c;
! buffer = (d << 8) | d;
buffer |= (buffer << 16);
}
else