https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115096

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So this is not a miscompiling as the pointer types "char*" and "unsigned char*"
are two distant pointer types and are not considered to be aliasing and you are
not accessing the pointer via a character type (GCC has an exception for void*
types though which you could do the store in and get the correct result).

There is no warning here because the casting between pointer types is not the
issue but rather the access.

Since test is passed a pointer to a "char*" type and only in test you do the
cast to a pointer to a "unsigned char*" type. a warning in this case would 100%
require full path analysis of the code.

Note you could rewrite the code to be:
```
static int helper(void **cursor)
{
    // first character ('a') is OK
    if (*(unsigned char*)*cursor > 'z')
    {
        return -11;
    }
    *cursor = ((unsigned char*)*cursor) + 1;
    // second character ('b') is OK
    if (*(unsigned char*)*cursor > 'z')
    {
        return -12;
    }
    // increment to third character ('.')...
    *cursor = ((unsigned char*)*cursor) + 1;
    return 0;
}
```

And it would work correctly as I mentioned GCC has an exception on the `void*`
type which is allowed to alias all other pointer types. (Note I am not 100%
sure if C standard says that is still undefined code or not but GCC makes it as
such).

Reply via email to