Skip to content

Parse the MySQL address with net.SplitHostPort - #829

Closed
arpitjain099 wants to merge 1 commit into
in-toto:mainfrom
arpitjain099:fix/mysql-dsn-addr-parsing
Closed

Parse the MySQL address with net.SplitHostPort#829
arpitjain099 wants to merge 1 commit into
in-toto:mainfrom
arpitjain099:fix/mysql-dsn-addr-parsing

Conversation

@arpitjain099

Copy link
Copy Markdown

ConfigFromMySQL splits dbConfig.Addr on ":" and indexes element 1:

addr := strings.Split(dbConfig.Addr, ":")
dc.Host = addr[0]
port, err := strconv.Atoi(addr[1])

go-sql-driver's ParseDSN only fills in a default port when Net is "tcp". For a unix socket DSN it leaves Addr as a bare socket path with no colon, so there is no element 1:

user:pass@tcp(127.0.0.1:3306)/testify      -> Host="127.0.0.1" Port=3306
user:pass@unix(/tmp/mysql.sock)/testify    -> PANIC: runtime error: index out of range [1] with length 1
user:pass@tcp([::1]:3306)/testify          -> err=could not parse mysql port: strconv.Atoi: parsing "": invalid syntax

Both production callers reach it with the configured connection string, NewEntClient in client.go and RewriteConnectionStringForIAM in iam.go, so a socket DSN takes the server down at startup rather than reporting a bad configuration.

The third line is a second, quieter problem from the same split: a bracketed IPv6 host leaves Host as "[" and the port empty.

net.SplitHostPort handles both shapes, and a non-tcp network is now an explicit error that says what is supported rather than a panic.

Added utils_test.go covering tcp, IPv6, the unix socket, and an unparseable DSN. Without the change the IPv6 and unix cases both fail, the latter by panicking.

@netlify

netlify Bot commented Aug 17, 2026

Copy link
Copy Markdown

Deploy Preview for archivista-dev ready!

Name Link
🔨 Latest commit 98236a7
🔍 Latest deploy log https://app.netlify.com/projects/archivista-dev/deploys/6a9abeeb4003a100087a92a1
😎 Deploy Preview https://deploy-preview-829--archivista-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

ConfigFromMySQL split dbConfig.Addr on ":" and indexed element 1.
go-sql-driver's ParseDSN only fills in a default port when Net is "tcp",
so a unix socket DSN leaves Addr as a bare socket path with no colon:

  user:pass@unix(/tmp/mysql.sock)/testify
  panic: runtime error: index out of range [1] with length 1

NewEntClient and RewriteConnectionStringForIAM both call this with the
configured connection string, so the server binary crashed at startup
instead of reporting a bad configuration.

The same split mis-handled a bracketed IPv6 host, leaving Host as "[" and
failing with an empty-port Atoi error.

net.SplitHostPort handles both, and a non-tcp network is now an explicit
error naming what is supported.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@jkjell
jkjell force-pushed the fix/mysql-dsn-addr-parsing branch from 2f539e5 to 98236a7 Compare September 4, 2026 12:51
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 this pull request may close these issues.

2 participants