Skip to content

fix: recover from driver panic on GEOGRAPHY/GEOMETRY columns#704

Open
dlevy-msft-sql wants to merge 1 commit intomicrosoft:mainfrom
dlevy-msft-sql:fix-geography-panic
Open

fix: recover from driver panic on GEOGRAPHY/GEOMETRY columns#704
dlevy-msft-sql wants to merge 1 commit intomicrosoft:mainfrom
dlevy-msft-sql:fix-geography-panic

Conversation

@dlevy-msft-sql
Copy link
Contributor

Problem

sqlcmd crashes with a panic when selecting NULL for GEOGRAPHY or GEOMETRY data types:

declare @v geography
select @v
go
panic: not implemented makeGoLangScanType for type 240

Root Cause

The go-mssqldb driver panics in makeGoLangScanType when encountering type 240 (GEOGRAPHY/GEOMETRY). The rows.ColumnTypes() call propagates this panic up, crashing sqlcmd.

Code Change

Added safeColumnTypes() wrapper that uses defer/recover to catch the panic and convert it to an error:

func safeColumnTypes(rows *sql.Rows) (cols []*sql.ColumnType, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = localizer.Errorf("unsupported column type: %v", r)
            cols = nil
        }
    }()
    return rows.ColumnTypes()
}

Testing

  • Build passes: go build ./...
  • New test TestSafeColumnTypesRecoversPanic passes
  • TestQueryTimeout and TestQueryServerPropertyReturnsColumnName pass with live database

Fixes #553

The go-mssqldb driver panics when calling ColumnTypes() for
GEOGRAPHY/GEOMETRY types (type 240). Wrap the call with
panic recovery to return an error instead of crashing.
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.

Crash when selecting NULL for GEOGRAPHY/GEOMETRY data type

1 participant