Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime/cgo: cstring crash with empty string #9749

Closed
chai2010 opened this issue Feb 2, 2015 · 1 comment
Closed

runtime/cgo: cstring crash with empty string #9749

chai2010 opened this issue Feb 2, 2015 · 1 comment

Comments

@chai2010
Copy link
Contributor

chai2010 commented Feb 2, 2015

package main

/*
#include <ctype.h>
#include <stdlib.h>

char* str_to_lower(char* str) {
    char* p = str;
    if(str == NULL || str[0] == '\0') {
        return str;
    }
    for(; *p != '\0'; p++) {
        *p = tolower(*p);
    }
    return str;
}
*/
import "C"
import "unsafe"

func str_to_lower(s string) string {
    cs1 := C.CString(s)
    defer C.free(unsafe.Pointer(cs1))

    cs2 := C.str_to_lower(cs1)
    defer C.free(unsafe.Pointer(cs2))

    return C.GoString(cs2)
}

func main() {
    println(str_to_lower(""))
}

go version
go version go1.4.1 windows/amd64

gcc --version
gcc (tdm64-1) 4.9.2

@minux
Copy link
Member

minux commented Feb 2, 2015

It shouldn't C.free the pointer returned from C.str_to_lower.

It's caused by double free error, and has nothing to do with Go.

@minux minux closed this as completed Feb 2, 2015
@mikioh mikioh changed the title cgo: cstring crash with empty string runtime/cgo: cstring crash with empty string Feb 2, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants