From 53b286e0ce7909864b20ff23dcfb5f8676787f2a Mon Sep 17 00:00:00 2001 From: jjgarcia Date: Tue, 28 Jun 2005 13:48:31 +0000 Subject: [PATCH] When creating a C-STRING, leave only as many characters as dictated by the fill pointer --- src/c/ffi.d | 18 ++++++++++++++++++ src/cmp/cmpffi.lsp | 4 +--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/c/ffi.d b/src/c/ffi.d index 5d16331f1..67f532982 100644 --- a/src/c/ffi.d +++ b/src/c/ffi.d @@ -45,8 +45,26 @@ ecl_foreign_data_pointer_safe(cl_object f) char * ecl_string_pointer_safe(cl_object f) { + cl_index l; + if (type_of(f) != t_string) FEwrong_type_argument(@'string', f); +#ifdef USE_BOEHM + /* This function is only used in CMPFFI.LSP as to convert lisp objects + * to a null terminated string. This code is safe with the + * Boehm-Weiser garbage collector because the pointer is stored in the + * stack as part of the argument list and cannot be lost. We still have to + * figure out what to do with the older garbage collector. + */ + if (f->string.hasfillp && ((l = f->string.fillp) < f->string.dim)) { + unsigned char *s = cl_alloc_atomic(l + 1); + memcpy(s, f->string.self, l); + s[l] = 0; + return s; + } +#else +# error "ecl_string_pointer_safe does not work with the old garbage collector" +#endif return f->string.self; } diff --git a/src/cmp/cmpffi.lsp b/src/cmp/cmpffi.lsp index b528aee55..c77d18dfe 100644 --- a/src/cmp/cmpffi.lsp +++ b/src/cmp/cmpffi.lsp @@ -225,9 +225,7 @@ ((:cstring) (case loc-rep-type ((:object) - (if (safe-compile) - (wt "ecl_string_pointer_safe(" loc ")") - (wt "(" loc ")->string.self"))) + (wt "ecl_string_pointer_safe(" loc ")")) ((:pointer-void) (wt "(char *)(" loc ")")) (otherwise