On 2/20/06, Kon Lovett <address@hidden> wrote:
Could be something I did, message-digest is mine, and it was recently
updated. However, it doesn't explain the above. (I will look into
this more tomorrow.)
You're off the hook , because a minimal version of sha2 with no dependency on message-digest still had the problem.
Here's the smallest version I could come up with that segfaults when compiled with libpq, as I explained before:
#>
#include "sha2.h"
static unsigned char rps_buf[1024];
<#
(define sha256:init (foreign-lambda void "SHA256_Init" c-pointer))
(define sha256:update (foreign-lambda void "SHA256_Update" c-pointer pointer int))
(define sha256:final (foreign-lambda void "SHA256_Final" c-pointer pointer))
(define-foreign-variable mybuf c-pointer "rps_buf")
(define (mysha256:binary-digest obj)
(sha256:init mybuf)
(sha256:update mybuf obj (string-length obj))
(let ((res (make-string 32)))
(sha256:final mybuf res)
res))
And here's one that doesn't:
#>
#include "sha2.h"
static unsigned char rps_buf[1024];
<#
(define sha256:init (foreign-lambda void "SHA256_Init" c-pointer))
(define sha256:update (foreign-lambda void "SHA256_Update" c-pointer pointer int))
(define sha256:finalify
(foreign-lambda* c-string ((c-pointer buf))
"unsigned char *result = malloc(sizeof(unsigned char)*32);"
"SHA256_Final(buf, result);"
"return(result);"))
(define-foreign-variable mybuf c-pointer "rps_buf")
(define (mysha256:binary-digest obj)
(sha256:init mybuf)
(sha256:update mybuf obj (string-length obj))
(sha256:finalify mybuf))
To make either of these work, you have to separately compile sha2-base.c and link the resulting object file with the results of compiling these (which was the only way I could figure out to get -ggdb to work on sha2-base.c
).
I don't know if the second listing really solved the problem or not. I was thinking that there could be a problem with passing a string as the second argument to sha256:final, which both calls for a pointer and mutates that argument, but I don't really understand the FFI well enough to say for sure.
Reed Sheridan