alexbrainman / odbc

odbc driver written in go
BSD 3-Clause "New" or "Revised" License
352 stars 140 forks source link

Using package when built with --race "fatal error: checkptr: unsafe pointer conversion" #144

Closed MarkSonghurst closed 4 years ago

MarkSonghurst commented 4 years ago

Hi When I build my Linux program using the master branch with go build --race I'm constantly getting an fatal error. I've tried older branches of the package back to 2018 but they exhibit the same error. I've not had time to try an older version of Go, I'm using go version go1.14.2 linux/amd64

The program appears to run fine when built without race detection. I doubt the error is race related, but maybe they've improved pointer checking in newer Go versions..?

fatal error: checkptr: unsafe pointer conversion

goroutine 1 [running]:
runtime.throw(0x1bd6cd2, 0x23)
    /usr/local/go/src/runtime/panic.go:1116 +0x72 fp=0xc0004cad88 sp=0xc0004cad58 pc=0x447a62
runtime.checkptrAlignment(0xc00043a000, 0x189be60, 0x1)
    /usr/local/go/src/runtime/checkptr.go:20 +0xc9 fp=0xc0004cadb8 sp=0xc0004cad88 pc=0x419319
bitbucket.org/XXXX/taishuh-ced/vendor/github.com/alexbrainman/odbc.(*BaseColumn).Value(0xc00000e020, 0xc00043a000, 0x3a, 0x202, 0x37a76b8, 0xc000000000, 0x415ed6, 0xc00040b180)
    /home/mark/go/src/bitbucket.org/XXXX/taishuh-ced/vendor/github.com/alexbrainman/odbc/column.go:144 +0x700 fp=0xc0004caf30 sp=0xc0004cadb8 pc=0xe0e090
bitbucket.org/XXXX/taishuh-ced/vendor/github.com/alexbrainman/odbc.(*BindableColumn).Value(0xc000280000, 0x7f95481510a0, 0x0, 0xe15351, 0x7f9582a14108, 0x0, 0xc0000a4c90)
    /home/mark/go/src/bitbucket.org/XXXX/taishuh-ced/vendor/github.com/alexbrainman/odbc/column.go:259 +0x14c fp=0xc0004cb010 sp=0xc0004caf30 pc=0xe0f39c
bitbucket.org/XXXX/taishuh-ced/vendor/github.com/alexbrainman/odbc.(*Rows).Next(0xc000010008, 0xc0000a4c90, 0x3, 0x3, 0x4581f5, 0x17623d0)
    /home/mark/go/src/bitbucket.org/XXXX/taishuh-ced/vendor/github.com/alexbrainman/odbc/rows.go:35 +0x1a6 fp=0xc0004cb0a8 sp=0xc0004cb010 pc=0xe15546
database/sql.(*Rows).nextLocked(0xc000282180, 0xc000000000)
    /usr/local/go/src/database/sql/sql.go:2767 +0x19d fp=0xc0004cb170 sp=0xc0004cb0a8 pc=0xdde36d
database/sql.(*Rows).Next.func1()
    /usr/local/go/src/database/sql/sql.go:2745 +0x58 fp=0xc0004cb1b0 sp=0xc0004cb170 pc=0xde3498
database/sql.withLock(0x1ffb660, 0xc0002821b0, 0xc0004cb248)
    /usr/local/go/src/database/sql/sql.go:3184 +0x7f fp=0xc0004cb218 sp=0xc0004cb1b0 pc=0xde152f
database/sql.(*Rows).Next(0xc000282180, 0x200f7a0)
    /usr/local/go/src/database/sql/sql.go:2744 +0x95 fp=0xc0004cb278 sp=0xc0004cb218 pc=0xdde185
bitbucket.org/XXXX/taishuh-ced/operator.InitAPIKey(0x0, 0x0)
    /home/mark/go/src/bitbucket.org/XXXX/taishuh-ced/operator/apikey.go:53 +0x253 fp=0xc0004cb530 sp=0xc0004cb278 pc=0x100fa53
bitbucket.org/XXXX/taishuh-ced/daemon.Run(0x1baa123, 0x3)
    /home/mark/go/src/bitbucket.org/XXXX/taishuh-ced/daemon/daemon.go:252 +0x1d6b fp=0xc0004cbf68 sp=0xc0004cb530 pc=0x17542eb
main.main()
    /home/mark/go/src/bitbucket.org/XXXX/taishuh-ced/taishuh-ced.go:12 +0x44 fp=0xc0004cbf88 sp=0xc0004cbf68 pc=0x175a5d4
runtime.main()
    /usr/local/go/src/runtime/proc.go:203 +0x212 fp=0xc0004cbfe0 sp=0xc0004cbf88 pc=0x44a0e2
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc0004cbfe8 sp=0xc0004cbfe0 pc=0x47c4d1

According to the trace The crash is occurring within the call to rows.Next() my code line 53. I've added a comment in my code snippet below:

    rows, err := in.DB.Query(`EXEC dbo.UP_SC_GetScResourceLookup`)
    switch {
    case err != nil:
        return err

    default:
        defer rows.Close()
        var rowsError error
        for rows.Next() {               // <------ THIS IS LINE 53
            ak := APIKey{}
            err := rows.Scan(&ak.Resource, &ak.APIKey, &ak.Owner)
            switch {
            case err != nil:
                if rowsError == nil {
                    rowsError = err
                }

            default:
                apiKeysCount++
                ak.APIKey = strings.TrimSpace(ak.APIKey)
                apiKeyCache.Store(ak.Resource, ak)
            }
        }
        if err = rows.Err(); err != nil {
            if rowsError == nil {
                rowsError = err
            }
        }
        if rowsError != nil {
            return err
        }
    }

Looking at the odbc package code, the unsafe pointer conversion being reported by Go is on column.go:144

https://github.com/alexbrainman/odbc/blob/fd264d0ff3809a946737df4b1a0cef2a0baf6c57/column.go#L144

Sorry, this is too low level for me to offer any suggestions in how to fix this.

alexbrainman commented 4 years ago

Thank you for reporting this failure.

I doubt the error is race related, but maybe they've improved pointer checking in newer Go versions..?

That is correct. Go compiler now is more strict with unsafe package usage.

I will investigate this issue when I have some free time.

Alex

alexbrainman commented 4 years ago

@MarkSonghurst I just pushed

https://github.com/alexbrainman/odbc/tree/fix144

branch for you to test it. Cam you, please, verify and let me know, And I will merge into master.

Thank you.

Alex

MarkSonghurst commented 4 years ago

@alexbrainman Thank you for the fast turnaround. I can confirm the issue is now resolved in branch fix144

Thanks again.

alexbrainman commented 4 years ago

Thank you for the fast turnaround.

No worries.

I can confirm the issue is now resolved in branch fix144

Thanks for checking. The fix should be on master now.

Alex