[Python-Dev] 2.4 vs Windows vs bsddb

David Hopwood david.nospam.hopwood at blueyonder.co.uk
Wed Oct 11 00:49:48 CEST 2006


Tim Peters wrote:
> Given that, the assert() in question looks fine to me:
> 
>         if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) {
>             bytes_left = sizeof(errTxt) - bytes_left - 4 - 1;
>             assert(bytes_left >= 0);
> 
> We can't get into the block unless
> 
>     bytes_left < sizeof(errTxt) - 4
> 
> is true.  Subtracting bytes_left from both sides, then swapping LHS and RHS:
> 
>     sizeof(errTxt) - bytes_left - 4 > 0
> 
> which implies
> 
>     sizeof(errTxt) - bytes_left - 4 >= 1
> 
> Subtracting 1 from both sides:
> 
>     sizeof(errTxt) - bytes_left - 4 - 1 >= 0
> 
> And since the LHS of that is the new value of bytes_left, it must be true that
> 
>      bytes_left >= 0
> 
> Either that, or the original author (and me, just above) made an error
> in analyzing what must be true at this point.

You omitted to state an assumption that sizeof(errTxt) >= 4, since size_t
(and the constant 4) are unsigned. Also bytes_left must initially be nonnegative
so that the subexpression 'sizeof(errTxt) - bytes_left' cannot overflow.

-- 
David Hopwood <david.nospam.hopwood at blueyonder.co.uk>




More information about the Python-Dev mailing list