Divide by zero?

Stefan Bellon sbellon at sbellon.de
Sun Jan 2 11:29:50 PST 2005

John Tytgat wrote:

> I also noticed this during my last signal related changes in UnixLib
> but when I saw that this was already the case for a long time, I
> didn't bother to change it.  But you're right, if SharedCLibrary does
> this, then I think we should change UnixLib too.  Basically the fix
> will be in __rt_sdiv (and __rt_udiv) routines. 

It would be nice if you could fix this.

Oh, BTW, if you are looking at those math things anyway, I have another
(perhaps related) mystery. I'm trying to get Richard Windley's port of
SQLite to work with UnixLib so that I can link it against Bogofilter
(which needs UnixLib). However, SQLite behaves differently when
compiled with and linked against SharedCLibrary (it works) than when
compiled with and linked against UnixLib (it doesn't work).

I have traced down one problem in this function:

int sqlite3pager_pagecount(Pager *pPager){
  i64 n;
  assert( pPager!=0 );
  if( pPager->dbSize>=0 ){
    return pPager->dbSize;
  if( sqlite3OsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
    pPager->errMask |= PAGER_ERR_DISK;
    return 0;
  n /= pPager->pageSize;
  if( !pPager->memDb && n==PENDING_BYTE/pPager->pageSize ){
  if( pPager->state!=PAGER_UNLOCK ){
    pPager->dbSize = n;
  return n;

When compiled with and linked against UnixLib, the division in line

  n /= pPager->pageSize;

yields 1024 when n was 0 previously. I was able to fix like this:

  if (n != 0)
    n /= pPager->pageSize;

which worked.

But still, SQLite behaves differently. I hope that this is due to other
similar places.

So, if you're looking at the maths code, could you please have a look
whether there's something wrong that could lead to this problem?

BTW: i64 is a typedef of "long long int" and pageSize is just int.

And no, I wasn't able to produce a minimal case where it still is true.
> Note that today a float/double division by zero will raise.  I'll see
> if I can do that together with the other fix needed to let UnixLib
> compile with Norcorft.

Great. :-)

Stefan Bellon

More information about the gcc mailing list