[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: Ben Reser <ben_at_reser.org>
Date: 2004-01-16 23:28:19 CET

On Fri, Jan 16, 2004 at 05:09:23PM -0500, Russell Yanofsky wrote:
> If you really wanted to be paranoid you could write it like
>
> if ((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))
> {
> return svn_error_create (SVN_ERR_IO_OFFSET_SIZE, NULL,
> "Cannot seek file");
> }

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 */
  }

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

> That way there'd be an error for 3, 5, 6, and 7 byte off_t's.
>
> 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.

-- 
Ben Reser <ben@reser.org>
http://ben.reser.org
"Conscience is the inner voice which warns us somebody may be looking."
- H.L. Mencken
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Fri Jan 16 23:29:03 2004

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