fix: prevent nil pointer panic when piping input to sqlcmd (fixes #607)#640
Merged
dlevy-msft-sql merged 1 commit intomicrosoft:mainfrom Jan 27, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a nil pointer dereference panic (#607) that occurred when piping input to sqlcmd with ODBC-compatible flags (e.g., -G for Azure AD authentication). The panic happened because the Console object was not initialized when stdin was redirected/piped and no password prompt was needed, causing a crash when trying to read from stdin.
Changes:
- Modified
isConsoleInitializationRequired()to returnneedsConsole = truewhen stdin is redirected and no input file, query, or ChangePasswordAndExit is specified - Updated existing test expectations to reflect the fix and added a new comprehensive test for piped input scenarios
- Added README documentation showing how to pipe input to sqlcmd
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| cmd/sqlcmd/sqlcmd.go | Added condition to detect piped stdin and require console initialization to prevent nil pointer dereference |
| cmd/sqlcmd/stdin_console_test.go | Updated existing test expectations and added comprehensive test case for piped input scenarios including edge case with ChangePasswordAndExit |
| README.md | Added new documentation section with examples of piping input to sqlcmd in PowerShell and Bash |
…rosoft#607) When stdin is redirected (piped input) with ODBC-compat flags like -G, the Console was not being initialized, causing a nil pointer dereference in scanNext() when calling s.lineIo.Readline(). The fix ensures isConsoleInitializationRequired() returns true when stdin is redirected and no input file or query is specified, so that console.NewConsole() is called to handle the piped input. Changes: - Add condition in isConsoleInitializationRequired() for piped stdin - Add test case using os.Pipe() to verify piped input detection - Add README section documenting how to pipe input to sqlcmd
a595f6d to
14fb827
Compare
Collaborator
|
feels like we need some tests that build the app in /cmd/modern and run it. This case is a good one to start with. |
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work The tests build the binary once and reuse it for all tests. Non-connection tests can run anywhere without a SQL Server. Addresses feedback from @shueybubbles in PR microsoft#640.
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: Non-connection tests (always run): - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work Live connection tests (run when SQLCMDSERVER is set): - TestE2E_PipedInput_LiveConnection: piped SQL with real server - TestE2E_QueryFlag_LiveConnection: -Q flag with real server - TestE2E_InputFile_LiveConnection: -i flag with real server The tests build the binary once and reuse it for all tests. Live connection tests use SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD env vars. Addresses feedback from @shueybubbles in PR microsoft#640.
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: Non-connection tests (always run): - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work Live connection tests (run when SQLCMDSERVER is set): - TestE2E_PipedInput_LiveConnection: piped SQL with real server - TestE2E_QueryFlag_LiveConnection: -Q flag with real server - TestE2E_InputFile_LiveConnection: -i flag with real server The tests build the binary once and reuse it for all tests. Live connection tests use SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD env vars. Addresses feedback from @shueybubbles in PR microsoft#640.
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: Non-connection tests (always run): - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work Live connection tests (run when SQLCMDSERVER is set): - TestE2E_PipedInput_LiveConnection: piped SQL with real server - TestE2E_QueryFlag_LiveConnection: -Q flag with real server - TestE2E_InputFile_LiveConnection: -i flag with real server The tests build the binary once and reuse it for all tests. Live connection tests use SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD env vars. Addresses feedback from @shueybubbles in PR microsoft#640.
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: Non-connection tests (always run): - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work Live connection tests (run when SQLCMDSERVER is set): - TestE2E_PipedInput_LiveConnection: piped SQL with real server - TestE2E_QueryFlag_LiveConnection: -Q flag with real server - TestE2E_InputFile_LiveConnection: -i flag with real server The tests build the binary once and reuse it for all tests. Live connection tests use SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD env vars. Addresses feedback from @shueybubbles in PR microsoft#640.
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: Non-connection tests (always run): - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work Live connection tests (run when SQLCMDSERVER is set): - TestE2E_PipedInput_LiveConnection: piped SQL with real server - TestE2E_QueryFlag_LiveConnection: -Q flag with real server - TestE2E_InputFile_LiveConnection: -i flag with real server The tests build the binary once and reuse it for all tests. Live connection tests use SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD env vars. Addresses feedback from @shueybubbles in PR microsoft#640.
dlevy-msft-sql
added a commit
to dlevy-msft-sql/go-sqlcmd
that referenced
this pull request
Jan 27, 2026
Add e2e tests that build the sqlcmd binary and exercise real-world scenarios: Non-connection tests (always run): - TestE2E_Help: verifies --help flag works - TestE2E_Version: verifies --version flag works - TestE2E_PipedInput_NoPanic: regression test for microsoft#607 (piped input panic) - TestE2E_PipedInput_EmptyInput: empty piped input doesn't panic - TestE2E_InvalidFlag: invalid flags produce helpful errors - TestE2E_QueryFlag_NoServer: -Q flag without server doesn't panic - TestE2E_InputFile_NotFound: missing input file errors gracefully - TestE2E_PipedInput_WithStdinReader: GO batches in piped input work Live connection tests (run when SQLCMDSERVER is set): - TestE2E_PipedInput_LiveConnection: piped SQL with real server - TestE2E_QueryFlag_LiveConnection: -Q flag with real server - TestE2E_InputFile_LiveConnection: -i flag with real server The tests build the binary once and reuse it for all tests. Live connection tests use SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD env vars. Addresses feedback from @shueybubbles in PR microsoft#640.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #607 - nil pointer dereference when piping input to sqlcmd with ODBC-compatible flags.
Problem
When running:
A panic occurred in scanNext() due to a nil Console pointer.
Root Cause
In cmd/sqlcmd/sqlcmd.go, when using ODBC-compat flags with piped stdin, the isConsoleInitializationRequired() function returned false because stdin was detected as redirected and no password was required. This caused sqlcmd.New(nil, ...) to be called with a nil Console.
Solution
Added a condition in isConsoleInitializationRequired() to return needsConsole = true when stdin is redirected and no input file or query is specified.
Changes
Testing