[patch] Avoid bogus pointer->int cast
Ralf Corsepius
ralf.corsepius@rtems.org
Fri Jul 20 23:26:00 GMT 2012
On 07/17/2012 07:45 PM, Eric Blake wrote:
> On 07/17/2012 11:26 AM, Ralf Corsepius wrote:
>> On 07/17/2012 07:21 PM, Ralf Corsepius wrote:
>>> Hi,
>>>
>>> The patch below addresses a (minor) bug in readdir.c. It replaces a
>>> (bogus) pointer->int cast with a pointer->intptr_t cast.
>
> Rather than "fixing" the bogus pointer check, why not just nuke that
> code altogether?
My goal was to make the existing code compilable by removing invalid
code, not rewrite it.
>> +#include <stdint.h>
>
> In other words, no need to drag this in...
>
>> #include <dirent.h>
>>
>> extern int getdents (int fd, void *dp, int count);
>> @@ -75,7 +76,7 @@ _DEFUN(readdir, (dirp),
>> continue;
>> }
>> dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
>
> and everything between here...
>
>> - if ((int)dp & 03) { /* bogus pointer check */
>> + if ((intptr_t)dp & 03) { /* bogus pointer check */
>> #ifdef HAVE_DD_LOCK
>> __lock_release_recursive(dirp->dd_lock);
>> #endif
> return NULL;
> }
>
> ...and here is just stupid. POSIX already states that behavior is
> undefined if you pass in a bogus pointer; and in a compliant program,
> readdir should only ever be called on a valid pointer, making this check
> look like nothing more than dead code. I see no need to bloat the code
> with an attempt to gracefully deal with a bogus pointer.
This is a different issue than I was addressing.
Ralf
More information about the Newlib
mailing list