Fix for seg fault of STk under Linux

From: Harvey J. Stein <hjstein_at_math.huji.ac.il>
Date: Fri, 11 Nov 1994 18:51:36 +0200

I think I found a fix for the seg fault problem under STk. I'm not
going to provide a patch, since my fix is extremely quick and dirty &
needs to be properly integrated into the code. For example, it will
only work verbatim with the gnu bignum implementation. It won't work
with the free bignum code. I don't have enough understanding of the
code to integrate the fix properly. Also, given my lack of
understanding of the code, there might be other problems now.
However, the good news is that turtle.stk now runs straight through to
the spiral demo part (the last demo in turtle.stk) without a problem
:). The bad news is that I managed to get STk to hang two times in a
row by clicking wildly on the buttons in the spiral demo before I made
an additional change.

None the less, I'll describe the problem & the fix, so that people can
fix the code by hand & hopefully get on with their work.

Firstly, in my Src directory, there was a copy of (or a link to, I
didn't check which) gmp.h. This was a copy of gmp.h from the
free mp implementation, and not the gnu mp implementation. However, I
was compiling with the gnu mp implementation. This in itself probably
wouldn't cause bugs since my guess is that the STk code in Src doesn't
actively manipulate the bignum structures, & I think that both
versions of gmp.h use the same size header structure (so that when STk
allocates a chunk of memory for the bignum, it was at least getting
the right amount of memory).

In any case, I first removed gmp.h from the Src directory & instead
made it a symbolic link to ../Mp/gmp-1.3.2/gmp.h.

Secondly, the bug really seems to be in the clone function (defined in
Src/number.c). It allocates the header for a bignum & then calls
mpz_set to copy a bignum into this new location. The function mpz_set
will allocate additional memory for the copying if necessary. The
problem is that the header of the bignum isn't being cleared out
before the call to mpz_set. So, mpz_set is getting wrong info on
what's been allocated to the new bignum.

The fix I made is to zero out the bignum structure before the call to
mpz_set. I'm not sure this is the proper way to allocate & create a
bignum, but it seems to work.

So, just replace the clone function in Src/number.c with:


static SCM clone(SCM number)
{
  /* clone a number */
  switch (TYPE(number)) {
    case tc_integer: return makesmallint(INTEGER(number));
    case tc_bignum: {
                             SCM z;
                       
                       NEWCELL(z, tc_bignum);
                       BIGNUM(z) = must_malloc(sizeof(MP_INT));

                       /* Clear out the bignum header before copying into it */
                       BIGNUM(z)->size = 0;
                       BIGNUM(z)->alloc = 0;
                       BIGNUM(z)->d = 0;

                       mpz_set(BIGNUM(z), BIGNUM(number));
                       return z;
                     }
    case tc_flonum: return makenumber(FLONM(number));
    default: return UNDEFINED; /* Error will be signaled later */
  }
}


Good luck,

Dr. Harvey J. Stein
Berger Financial Research
hjstein_at_math.huji.ac.il
Received on Fri Nov 11 1994 - 17:53:43 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST