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.
Hi Christopher,
> 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".
Thanks. I checked in the following.
2002-11-25 Kazu Hirata <kazu@cs.umass.edu>
* libc/string/memset.c (memset): Make it safe even if
sizeof (int) = 2.
Index: memset.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/string/memset.c,v
retrieving revision 1.2
diff -u -r1.2 memset.c
--- memset.c 24 Aug 2000 16:25:36 -0000 1.2
+++ memset.c 25 Nov 2002 20:52:51 -0000
@@ -64,21 +64,23 @@
{
/* If we get this far, we know that n is large and m is word-aligned. */
+ /* To avoid sign extention, copy C to an unsigned variable. */
+ unsigned int d = c & 0xff;
+
aligned_addr = (unsigned long*)m;
- /* Store C into each char sized location in BUFFER so that
+ /* Store D into each char sized location in BUFFER so that
we can set large blocks quickly. */
- c &= 0xff;
if (LBLOCKSIZE == 4)
{
- buffer = (c << 8) | c;
+ buffer = (d << 8) | d;
buffer |= (buffer << 16);
}
else
{
buffer = 0;
for (i = 0; i < LBLOCKSIZE; i++)
- buffer = (buffer << 8) | c;
+ buffer = (buffer << 8) | d;
}
while (n >= LBLOCKSIZE*4)