[plt-scheme] sqlite.plt seg faults with 370

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Jul 27 22:06:31 EDT 2007

On Jul 27, Keith Frost wrote:
> [...]
> In sqlite.ss, the planet interface defines
> 
>   (define (select db sql)
>     (let ([result_ptr_ptr
>            (wrap-finalizer (malloc _string_array_ptr)
>                            (lambda (ptr)
>                              (when ptr
>                                (sqlite3_free_table
>                                 (ptr-ref ptr _string_array)))))]
>           [row-count_ptr (malloc _int_ptr)]
>           [column-count_ptr (malloc _int_ptr)])
>       (when (handle-status
>              db
>              (sqlite3_get_table (db-handle db)
>                                 sql
>                                 result_ptr_ptr
>                                 row-count_ptr
>                                 column-count_ptr
>                                 (db-_errMsg_ptr db)))
>         (cvector->list-of-vector
>          (add1 (ptr-ref row-count_ptr _int))
>          (ptr-ref column-count_ptr _int)
>          (make-cvector* (ptr-ref result_ptr_ptr _string_array)
>                         _string
>                         (* (+ (ptr-ref row-count_ptr _int) 1)
>                            (ptr-ref column-count_ptr _int)))))))
> 
> And it seems that some time between the calling of sqlite3_get_table,
> and the extraction of some of the results with
> cvector->list-of-vector, the block of memory pointed to by
> result_ptr_ptr (which is apparently malloc'd by the sqlite3 library,

Are you sure?  It looks like it's malloced in Scheme, and perhaps it's
set by sqlite?


> along with the space for the strings to which it points) can get
> clobbered.  This seems to be what's happening, and I guess it's
> plausible that 3m might do this, but what I don't know is how to fix
> it.  Can anybody here give me another clue?

I don't know about the sqlite interface, but one thing that you can
try is to use a raw malloc (add a 'raw flag to the malloc) -- this
will use the system's malloc, which is completely ignored by the GC.
This requires freeing the block explicitly, but the code already has a
finalizer so it's easy.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.