[Top][All Lists]
[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++.)
- Re: memmem issues, (continued)
- Re: memmem issues, Jim Meyering, 2007/12/20
- Re: memmem issues, Simon Josefsson, 2007/12/20
- Re: memmem issues, Paul Eggert, 2007/12/21
- Re: memmem issues,
Paul Eggert <=
- Re: memmem issues, Bruno Haible, 2007/12/31
- Re: memmem issues, Paul Eggert, 2007/12/31
Re: memmem issues, Bruno Haible, 2007/12/26
Re: memmem issues, Bruno Haible, 2007/12/26
Re: memmem issues, Bruno Haible, 2007/12/21