[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: apr_off_t is of an ambiguous size.

From: Russell Yanofsky <rey4_at_columbia.edu>
Date: 2004-01-17 01:30:53 CET

Ben Reser wrote:
> On Fri, Jan 16, 2004 at 05:09:23PM -0500, Russell Yanofsky wrote:
> I see what you're getting at and I think this would be better then
> what
> I did or even what you did:
>
> if (sizeof (svn_offset_t) >= sizeof (apr_off_t) ||
> (sizeof (apr_off_t) == 4 &&
> *offset > -0x7FFFFFFF | *offset < 0x7FFFFFFF) ||
> (sizeof (apr_off_t) == 2 &&
> *offset > -0x7FFF && *offset < 0x7FFF) ||
> (sizeof (apr_off_t) == 1 &&
> *offset > -0x7F || *offset > 0x7FF)
> {
> /* execute apr_file_seek */
> }
> else
> {
> /* throw error */
> }

There appear to be a few bugs above. In the first line >= should be replaced
by <=. On the third line | should be &&. On the seventh line the second >
should be < and an extra parenthesis should be added on the end.

> It eliminates an entire block that mostly is there because I started
> to implement it the way you suggest next.

Yes. With the bugs fixed it should be equivalent to my first suggestion, but
shorter.

>> I'm also curious if another implementation might work too:
>>
>> const apr_off_t min = ((apr_off_t)-1) << (sizeof(apr_off_t)*8-1);
>> const apr_off_t max = ~min;
>>
>> if (*offset < min || *offset > max) ...
>>
>> It assumes a 2's complement representation. Anyone know if that's ok
>> in ansi c?
>
> Nope because it's dependent upon the bit order of the arch. If you do
> this on a big endian 32-bit system that has an 8 byte apr_off_t it
> will
> fail. And such architectures do exist. That's why I went with it the
> way I did. It's kinda ugly but it works more portably.

Huh? How does endianness come into play here? Do you see me casting to a
char array? declaring a union? calling a socket api? :)
With an 8 byte apr_off_t the expression should be evaluated as follows on
*any* architecture:

min = ((apr_off_t)-1) << (sizeof(apr_off_t)*8-1)
min = 0xFFFFFFFFFFFFFFFF << (sizeof(apr_off_t)*8-1)
min = 0xFFFFFFFFFFFFFFFF << 63
min = 0x8000000000000000
max = ~min
max = 0x7FFFFFFFFFFFFFFF

In 2's complement representation

min = 0x8000000000000000 = -9223372036854775808 = -2^63
max = 0x7FFFFFFFFFFFFFFF = 9223372036854775807 = 2^63-1

- Russ

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Sat Jan 17 01:28:25 2004

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.