summaryrefslogtreecommitdiff
path: root/Lib/go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2016-04-17 17:52:09 -0700
committerIan Lance Taylor <iant@golang.org>2016-04-17 17:52:09 -0700
commit223c2a483563e5c9dd93067831a6a2af6252bcec (patch)
treecb2dd1a0d12a09541b2f0ffad7da31782e97f44c /Lib/go
parentf1d1d7b490dcf65601db34c9082ac47cadf98e59 (diff)
downloadswig-223c2a483563e5c9dd93067831a6a2af6252bcec.tar.gz
[Go] Fixes for Go 1.6: avoid returning Go pointers from
directors that return string values; add a trailing 0 byte when treating Go string as C char*.
Diffstat (limited to 'Lib/go')
-rw-r--r--Lib/go/go.swg30
-rw-r--r--Lib/go/goruntime.swg4
-rw-r--r--Lib/go/std_string.i26
3 files changed, 56 insertions, 4 deletions
diff --git a/Lib/go/go.swg b/Lib/go/go.swg
index 24f1b73f7..40e274119 100644
--- a/Lib/go/go.swg
+++ b/Lib/go/go.swg
@@ -435,10 +435,22 @@
%typemap(in)
char *, char[ANY], char[]
-%{ $1 = ($1_ltype)$input.p; %}
+%{
+ $1 = ($1_ltype)malloc($input.n + 1);
+ memcpy($1, $input.p, $input.n);
+ $1[$input.n] = '\0';
+%}
%typemap(in) char *&
-%{ $1 = ($1_ltype)$input.p; %}
+%{
+ $1 = ($1_ltype)malloc($input.n + 1);
+ memcpy($1, $input.p, $input.n);
+ $1[$input.n] = '\0';
+%}
+
+%typemap(freearg)
+ char *, char *&, char[ANY], char[]
+%{ free($1); %}
%typemap(out,fragment="AllocateString")
char *, char *&, char[ANY], char[]
@@ -460,7 +472,19 @@
$result = swigCopyString($input)
%}
-%typemap(directorout)
+%typemap(godirectorout)
+ char *, char *&, char[ANY], char[]
+%{
+ {
+ p := Swig_malloc(len($input) + 1)
+ s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input) + 1]
+ copy(s, $input)
+ s[len($input)] = 0
+ $result = *(*string)(unsafe.Pointer(&s))
+ }
+%}
+
+%typemap(directorout, warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG)
char *, char *&, char[ANY], char[]
%{ $result = ($1_ltype)$input.p; %}
diff --git a/Lib/go/goruntime.swg b/Lib/go/goruntime.swg
index e7a33adc4..dc6193d04 100644
--- a/Lib/go/goruntime.swg
+++ b/Lib/go/goruntime.swg
@@ -8,6 +8,10 @@
static void Swig_free(void* p) {
free(p);
}
+
+static void* Swig_malloc(int c) {
+ return malloc(c);
+}
%}
%insert(runtime) %{
diff --git a/Lib/go/std_string.i b/Lib/go/std_string.i
index 068c688cb..099ae84d4 100644
--- a/Lib/go/std_string.i
+++ b/Lib/go/std_string.i
@@ -24,8 +24,21 @@ class string;
%typemap(in) string
%{ $1.assign($input.p, $input.n); %}
+%typemap(godirectorout) string
+%{
+ {
+ p := Swig_malloc(len($input))
+ s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)]
+ copy(s, $input)
+ $result = *(*string)(unsafe.Pointer(&s))
+ }
+%}
+
%typemap(directorout) string
-%{ $result.assign($input.p, $input.n); %}
+%{
+ $result.assign($input.p, $input.n);
+ free($input.p);
+%}
%typemap(out,fragment="AllocateString") string
%{ $result = Swig_AllocateString($1.data(), $1.length()); %}
@@ -45,10 +58,21 @@ class string;
$1 = &$1_str;
%}
+%typemap(godirectorout) const string &
+%{
+ {
+ p := Swig_malloc(len($input))
+ s := (*[1<<30]byte)(unsafe.Pointer(p))[:len($input)]
+ copy(s, $input)
+ $result = *(*string)(unsafe.Pointer(&s))
+ }
+%}
+
%typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string &
%{
static $*1_ltype $1_str;
$1_str.assign($input.p, $input.n);
+ free($input.p);
$result = &$1_str;
%}