go vet error on generated code by Swig

86 views
Skip to first unread message

Vishnu

unread,
Nov 19, 2019, 8:43:48 AM11/19/19
to golang-nuts
Hi, I am using swig to generate a go wrapper for one of my projects. I am seeing the following error in generated code. Is there any workaround for this?

type swig_gostring struct { p uintptr; n int }
func swigCopyString(s string) string {
p := *(*swig_gostring)(unsafe.Pointer(&s))
up := unsafe.Pointer(p.p)
r := string((*[0x7fffffff]byte)(up)[:p.n]) // This line gives a go vet error: possible misuse of unsafe.Pointer
Swig_free(p.p)
return r
}

Vishnu

Ian Lance Taylor

unread,
Nov 19, 2019, 9:40:55 AM11/19/19
to Vishnu, golang-nuts
Honestly I would say that the workaround is to not run go vet on
generated code. The code could be cleaner but it is safe.

By the way, a minor point, but that is not what the code looks like in
current SWIG, where it looks like this:

type swig_gostring struct { p uintptr; n int }
func swigCopyString(s string) string {
p := *(*swig_gostring)(unsafe.Pointer(&s))
r := string((*[0x7fffffff]byte)(unsafe.Pointer(p.p))[:p.n])
Swig_free(p.p)
return r
}

Probably still gets the go vet warning, though.

Ian

Vishnu

unread,
Nov 19, 2019, 10:17:28 AM11/19/19
to golang-nuts
Sorry. Yes, I was trying to modify the code to see if the error goes away. 

type swig_gostring struct { p uintptr; n int }
func swigCopyString(s string) string {
p := *(*swig_gostring)(unsafe.Pointer(&s))
  r := string((*[0x7fffffff]byte)(unsafe.Pointer(p.p))[:p.n]) // This line gives the go vet error: possible misuse of unsafe.Pointer 
Swig_free(p.p)
return r
}

How do I exclude the file from going through go vet. I tried putting the below header in the file but didn't help
// Code generated by swig. DO NOT EDIT.

Vishnu

Ian Lance Taylor

unread,
Nov 19, 2019, 11:23:02 AM11/19/19
to Vishnu, golang-nuts
On Tue, Nov 19, 2019 at 7:17 AM Vishnu <vishn...@gmail.com> wrote:
>
> Sorry. Yes, I was trying to modify the code to see if the error goes away.
>
> type swig_gostring struct { p uintptr; n int }
> func swigCopyString(s string) string {
> p := *(*swig_gostring)(unsafe.Pointer(&s))
> r := string((*[0x7fffffff]byte)(unsafe.Pointer(p.p))[:p.n]) // This line gives the go vet error: possible misuse of unsafe.Pointer
> Swig_free(p.p)
> return r
> }
>
>
> How do I exclude the file from going through go vet. I tried putting the below header in the file but didn't help
>
> // Code generated by swig. DO NOT EDIT.

Normally people use SWIG with Go by adding a .swig file in their Go
directory and letting the go tool invoke SWIG directly. If you do
that, then as far as I know go vet will not look at the generated
code. Are you invoking SWIG yourself and committing the resulting
files?

Ian

Vishnu

unread,
Nov 19, 2019, 2:38:06 PM11/19/19
to golang-nuts
Thanks Ian. Yeah. I am generating those files and committing them. I will try the option you suggested
Reply all
Reply to author
Forward
0 new messages