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

From: Keith Frost (keith.l.frost at gmail.com)
Date: Fri Jul 27 20:42:43 EDT 2007

Well, I'm still not sure what's happening, but here's what I've been
able to figure out so far.
The sqlite3 API includes a function
int sqlite3_get_table(
  sqlite3*,               /* An open database */
  const char *sql,       /* SQL to be executed */
  char ***resultp,       /* Result written to a char *[]  that this points to */
  int *nrow,             /* Number of result rows written here */
  int *ncolumn,          /* Number of result columns written here */
  char **errmsg          /* Error msg written here */
);

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,
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?

Thanks,

Keith F.


Posted on the users mailing list.