-
Notifications
You must be signed in to change notification settings - Fork 18k
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: diagnose when Scanf consumes a newline not matched by the format #8944
Comments
I don't know if there is anything to change here. I think the difference between GNU/Linux and Windows is that Windows generates \r\n. Your formats don't use any spaces, and you are reading one element at a time. Reading one element at a time means that the scanner starts fresh each time. Not using any spaces means that spaces are not skipped. Reading the second number on the first line picks up the first character after the 20. On Windows this is \r, on GNU/Linux this is \n. That stops the %d, so the scanner stops. Your next call has forgotten the previous state, so it reads the next character. On Windows this is \n, on GNU/Linux it is the first character of the next line. We could probably make the behaviour more consistent across operating systems by rigidly looking for \n following \r, and treating them as a single character. I'll leave this issue open in case we think that is a good choice. For your particular case, though, you should probably write your code differently. If you want to read a line at a time, then read a line at a time and call Sscanf on that line. Labels changed: added repo-main, release-go1.5, os-windows. Status changed to Accepted. |
Portable demonstration / test case.
|
My memory is that Rob was already working on this. |
The test program is erroneous. The issue is that the input contains newlines but the pattern ("%d") does not. The documentation clearly states: If the number is instead followed by \r or ' ', that is consumed, the newline remains in the input stream, and the next call to Scanf fails because there is an unexpected newline, as diagnosed. If you use Scan instead of Scanf, the program works just fine regardless. Scanf is fussy about newlines, on purpose (you have Scan if you don't care about them), but is sometimes thwarted by the clean separation between the fmt package and I/O, and the consequences of reading too far. I admit this is unsatisfactory but it is working as documented, if not exactly as intended. One possibility is for Scanf to notice this case and complain on the previous call that it is consuming a newline that cannot be matched by the pattern, but I am reluctant to do that this late in the cycle. Deferring to 1.6. I will also change the title of the bug. |
I believe this is fixed in 1.8. Please reopen if that is not the case. |
by amirtaghavi2020:
The text was updated successfully, but these errors were encountered: