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

ScanType() should not return sql.RawBytes #1423

Closed
methane opened this issue May 1, 2023 · 0 comments · Fixed by #1424
Closed

ScanType() should not return sql.RawBytes #1423

methane opened this issue May 1, 2023 · 0 comments · Fixed by #1424

Comments

@methane
Copy link
Member

methane commented May 1, 2023

Issue description

When I investing #1390, I found XORM used sql.RawBytes for ScanType. It was XORM bug.
But we have same bug!

mysql/fields.go

Lines 190 to 195 in cffc85c

case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON,
fieldTypeTime:
return scanTypeRawBytes

Example code

https://github.com/methane/notes/blob/master/2023/go-mysql-race/main.go

Error log

$ go run -race .
Hello, 0 Hello, 0 Hello, 0 Hello, 0 Hello, 0
==================
WARNING: DATA RACE
Read at 0x00c000166040 by goroutine 8:
  runtime.slicebytetostring()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/runtime/string.go:81 +0x0
  main.main.func1()
      /Users/inada-n/notes/2023/go-mysql-race/main.go:46 +0x64
  main.main.func3()
      /Users/inada-n/notes/2023/go-mysql-race/main.go:52 +0x78

Previous write at 0x00c000166040 by main goroutine:
  runtime.racewriterange()
      <autogenerated>:1 +0x14
  internal/poll.ignoringEINTRIO()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/internal/poll/fd_unix.go:794 +0x2ec
  internal/poll.(*FD).Read()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/internal/poll/fd_unix.go:163 +0x20
  net.(*netFD).Read()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/net/fd_posix.go:55 +0x44
  net.(*conn).Read()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/net/net.go:183 +0x84
  net.(*TCPConn).Read()
      <autogenerated>:1 +0x4c
  github.com/go-sql-driver/mysql.(*buffer).fill()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/mysql@v1.7.1/buffer.go:90 +0x33c
  github.com/go-sql-driver/mysql.(*buffer).readNext()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/mysql@v1.7.1/buffer.go:119 +0x54
  github.com/go-sql-driver/mysql.(*mysqlConn).readPacket()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/mysql@v1.7.1/packets.go:32 +0xa4
  github.com/go-sql-driver/mysql.(*mysqlStmt).readPrepareResultPacket()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/mysql@v1.7.1/packets.go:830 +0x38
  github.com/go-sql-driver/mysql.(*mysqlConn).Prepare()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/mysql@v1.7.1/connection.go:182 +0x2e8
  github.com/go-sql-driver/mysql.(*mysqlConn).PrepareContext()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/mysql@v1.7.1/connection.go:533 +0x58
  database/sql.ctxDriverPrepare()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/ctxutil.go:15 +0x11c
  database/sql.(*DB).queryDC.func2()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1770 +0x88
  database/sql.withLock()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:3405 +0x84
  database/sql.(*DB).queryDC()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1769 +0x25c
  database/sql.(*DB).query()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1726 +0xf0
  database/sql.(*DB).QueryContext.func1()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1704 +0x9c
  database/sql.(*DB).retry()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1538 +0x4c
  database/sql.(*DB).QueryContext()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1703 +0xc8
  database/sql.(*DB).Query()
      /opt/homebrew/Cellar/go/1.20.3/libexec/src/database/sql/sql.go:1717 +0x144
  main.main()
      /Users/inada-n/notes/2023/go-mysql-race/main.go:28 +0xf0

Goroutine 8 (running) created at:
  main.main()
      /Users/inada-n/notes/2023/go-mysql-race/main.go:43 +0x180
==================

Configuration

Driver version (or git SHA):

Go version: run go version in your console

Server version: E.g. MySQL 5.6, MariaDB 10.0.20

Server OS: E.g. Debian 8.1 (Jessie), Windows 10

methane added a commit to methane/notes that referenced this issue May 1, 2023
methane added a commit to methane/mysql that referenced this issue May 1, 2023
It returned sql.RawBytes but it was dangoerous.

Fixes go-sql-driver#1423
methane added a commit that referenced this issue May 7, 2023
ColumnTypeScanType() returns []byte, string, or sql.NullString.
It returned sql.RawBytes but it was dangoerous.

Fixes #1423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant