Navigation Menu

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

fmt: cannot read more than 254 characters in Windows when calling Fscanln #42551

Open
jimen0 opened this issue Nov 12, 2020 · 4 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@jimen0
Copy link
Contributor

jimen0 commented Nov 12, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.4 darwin/amd64
$ gotip version
go version devel +b34b0aaf69 Thu Nov 12 15:28:43 2020 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes. It also reproduces with current tip go version devel +b34b0aaf69 Thu Nov 12 15:28:43 2020 +0000 darwin/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
N/A

What did you do?

package main

import (
	"fmt"
	"os"
)

func main() {
	var s string
	_, err := fmt.Fscanln(os.Stdin, &s)
	if err != nil {
		panic(err)
	}

	fmt.Println(s)
}
$ GOOS=windows go build -o reproducer_stdin.exe main.go
$ GOOS=windows gotip build -o reproducer_stdin_tip.exe main.go

What did you expect to see?

On the Windows host, I type A character 257 times after running the program, then press enter key. The Go program reads them correctly.

PS C:\Users\mig\Downloads> .\reproducer_stdin_tip.exe | Measure-Object -char | Select-Object -expand Characters
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
257
PS C:\Users\mig\Downloads> .\reproducer_stdin.exe | Measure-Object -char | Select-Object -expand Characters
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
257

What did you see instead?

On the Windows host, I typed A character 257 times after running the program, then presed enter key. The Go program stdin froze after 254 characters.

PS C:\Users\mig\Downloads> .\reproducer_stdin_tip.exe | Measure-Object -char | Select-Object -expand Characters
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
254
PS C:\Users\mig\Downloads> .\reproducer_stdin.exe | Measure-Object -char | Select-Object -expand Characters
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
254

NOTE: this seems to also be borken when copying&pasting text.

Note: this was tested using a Windows (Windows Server 2012 R2 Datacenter) Azure machine with default settings and environment. Just downloaded the cross-compiled binaries into it and ran them.
Edit: This is also reproducible using Windows Command Prompt using an Azure Windows (Windows 10 Pro) default machine.


I don't really know if this is related to Go at all or an issue with Powershell itself, but I thought it was worth submitting an issue. This was initially discovered by @karelorigin.

Best regards,
Miguel

@jimen0 jimen0 changed the title fmt/Fscanln: cannot read more than 254 characters in Windows Powershell fmt/Fscanln: cannot read more than 254 characters in Windows Nov 12, 2020
@cagedmantis cagedmantis changed the title fmt/Fscanln: cannot read more than 254 characters in Windows fmt: cannot read more than 254 characters in Windows when calling Fscanln Nov 13, 2020
@cagedmantis cagedmantis added OS-Windows NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Nov 13, 2020
@cagedmantis cagedmantis added this to the Backlog milestone Nov 16, 2020
@networkimprov
Copy link

cc @alexbrainman @mattn

@reznik99
Copy link

reznik99 commented Jan 4, 2022

Has it been confirmed if it's a Go issue?
I have tried CMD and Powershell still having this issue.

Input is truncated to a maximum of 254 characters whether typing or pasting.
Works fine on Linux build

@sgtxn
Copy link

sgtxn commented Feb 4, 2023

Same, stumbled upon this today when trying to paste a long authorization code into fmt.Scan input, seems to occur no matter if I'm using the old cmd.exe, the new Windows Terminal or the VSCode built-in terminal. Also occurs in cmd, Powershell and Git Bash.

Wrapping os.Stdin in bufio.NewReader solves the issue though:

var code string
in := bufio.NewReader(os.Stdin)
if _, err := fmt.Fscan(in, &code); err != nil {
    log.Fatal(err)
}

@karelorigin
Copy link

@reznik99, verifying that it is Go specific is actually quite simple. Try the following python script:

s = input("input more than 254 characters:\n")
print("Received: " + s)

You can execute this in your browser devtools to quickly generate a long string:

copy("a".repeat(254) + "b")
> python3 input.py
input more than 254 characters:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab
Received: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

6 participants