bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memmem issues


From: Paul Eggert
Subject: Re: memmem issues
Date: Sat, 29 Dec 2007 01:30:34 -0800
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

Bruno Haible <address@hidden> writes:

> No; ISO C 99 section 7.21.4 says that when byte strings are compared the
> elements are considered as 'unsigned char' values. Why risk bugs when the
> approach is very simple: after fetching any 'char' from any of the strings,
> cast it to 'unsigned char'.

Unfortunately that simple approach isn't portable according to the C99
rules.  The 'char' type can have padding bits, which means that code
like this:

     char const *needle = (char const *) needle_start;
     char const *haystack = (char const *) haystack_start;
     if ((unsigned char) *needle == (unsigned char) *haystack) ...

might ignore some of the bits in the storage addressed by A and B.
To get a proper memcmp-style comparison one must do something like this:

     unsigned char const *needle = (unsigned char const *) needle_start;
     unsigned char const *haystack = (unsigned char const *) haystack_start;
     if (*needle == *haystack) ...

(In the latter version, the casts are needed only because this code is
intended to be portable to C++.)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]