Guile STRING LENGTH typemap fixes

Fix leak in (char *STRING, size_t LENGTH) family of typemaps.
Add (size_t LENGTH, const char *STRING) family of typemaps.
This commit is contained in:
William S Fulton 2025-07-28 22:39:05 +01:00
parent 7c478b5e17
commit 29daa0fe34
4 changed files with 33 additions and 5 deletions

View File

@ -19,10 +19,6 @@ top_builddir = @top_builddir@
GUILE = @GUILE@ GUILE = @GUILE@
GUILE_RUNTIME= GUILE_RUNTIME=
FAILING_CPP_TESTS = \
char_binary_rev_len \
director_binary_string_rev_len \
C_TEST_CASES = long_long \ C_TEST_CASES = long_long \
list_vector \ list_vector \
multivalue \ multivalue \

View File

@ -0,0 +1,6 @@
;; The SWIG modules have "passive" Linkage, i.e., they don't generate
;; Guile modules (namespaces) but simply put all the bindings into the
;; current module. That's enough for such a simple test.
(dynamic-call "scm_init_char_binary_rev_len_module" (dynamic-link "./libchar_binary_rev_len"))
(load "testsuite.scm")
(load "../schemerunme/char_binary_rev_len.scm")

View File

@ -0,0 +1,9 @@
(define t (new-Test))
(if (not (= (Test-strlen t "hile") 4))
(error "bad multi-arg typemap"))
(if (not (= (Test-strlen t "hil\x00") 4))
(error "bad multi-arg typemap"))
(exit 0)

View File

@ -382,15 +382,32 @@ typedef unsigned long SCM;
* String & length * String & length
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
%typemap(in) (char *STRING, int LENGTH), (char *STRING, size_t LENGTH) { %typemap(in) (const char *STRING, size_t LENGTH)(int must_free = 0) {
size_t temp; size_t temp;
$1 = ($1_ltype) SWIG_Guile_scm2newstr($input, &temp); $1 = ($1_ltype) SWIG_Guile_scm2newstr($input, &temp);
$2 = ($2_ltype) temp; $2 = ($2_ltype) temp;
must_free = 1;
} }
%typemap(freearg) (const char *STRING, size_t LENGTH) "if (must_free$argnum) SWIG_free($1);"
%apply (const char *STRING, size_t LENGTH) { (const char *STRING, int LENGTH) } %apply (const char *STRING, size_t LENGTH) { (const char *STRING, int LENGTH) }
%apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) } %apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) }
%apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) } %apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) }
/* Length & string reverse order typemap */
%typemap(in) (size_t LENGTH, const char *STRING)(int must_free = 0) {
size_t temp;
$2 = ($2_ltype) SWIG_Guile_scm2newstr($input, &temp);
$1 = ($1_ltype) temp;
must_free = 1;
}
%typemap(freearg) (size_t LENGTH, const char *STRING) "if (must_free$argnum) SWIG_free($2);"
%apply (size_t LENGTH, const char *STRING) { (int LENGTH, const char *STRING) }
%apply (size_t LENGTH, const char *STRING) { (size_t LENGTH, char *STRING) }
%apply (size_t LENGTH, char *STRING) { (int LENGTH, char *STRING) }
/* ------------------------------------------------------------ /* ------------------------------------------------------------
* CLASS::* (member function pointer) typemaps * CLASS::* (member function pointer) typemaps
* taken from typemaps/swigtype.swg * taken from typemaps/swigtype.swg