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: Scanf / Sscanf does not properly treat carriage return symbol, behaves different with C function #69103

Closed
RodionGork opened this issue Aug 28, 2024 · 1 comment

Comments

@RodionGork
Copy link

RodionGork commented Aug 28, 2024

Go version

go1.23

Output of go env in your module/workspace:

GO...=n/a

What did you do?

Here is an example in playground https://go.dev/play/p/z7Rd0o0PiJH

We simply try reading with fmt.Sscanf (from the prepared string, but it is the same with fmt.Scanf reading from stdin - just more difficult to demonstrate) - there are two integer values, separated with windows-style newline (\r\n).

	var x, y int
	input := "5\r\n8\r\n"
	fmt.Sscanf(input, "%d %d", &x, &y)
	fmt.Println(x, y)

What did you see happen?

It prints 5 0

What did you expect to see?

It should print 5 8

C code for comparing behavior:

#include <stdio.h>

int main() {
  char s[] = "5\r\n8\r\n";
  int x, y;
  sscanf(s, "%d %d", &x, &y);
  printf("%d %d\n", x, y);
  return 0;
}

Also this is not in concordance with package description here https://pkg.go.dev/fmt

In all the scanning functions, a carriage return followed immediately by a newline
is treated as a plain newline (\r\n means the same as \n).
@seankhliao
Copy link
Member

A newline is not a space, even without \r your line won't match.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2024
@golang golang deleted a comment Aug 28, 2024
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

No branches or pull requests

5 participants
@RodionGork @seankhliao and others