Skip to content

ScanType() should not return sql.RawBytes #1423

Closed
@methane

Description

@methane

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/[email protected]/buffer.go:90 +0x33c
  github.com/go-sql-driver/mysql.(*buffer).readNext()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/[email protected]/buffer.go:119 +0x54
  github.com/go-sql-driver/mysql.(*mysqlConn).readPacket()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/[email protected]/packets.go:32 +0xa4
  github.com/go-sql-driver/mysql.(*mysqlStmt).readPrepareResultPacket()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/[email protected]/packets.go:830 +0x38
  github.com/go-sql-driver/mysql.(*mysqlConn).Prepare()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/[email protected]/connection.go:182 +0x2e8
  github.com/go-sql-driver/mysql.(*mysqlConn).PrepareContext()
      /Users/inada-n/go/pkg/mod/github.com/go-sql-driver/[email protected]/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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions