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

bufio: invalid use of UnreadByte #3

Closed
ramainen opened this issue Jun 21, 2019 · 8 comments
Closed

bufio: invalid use of UnreadByte #3

ramainen opened this issue Jun 21, 2019 · 8 comments

Comments

@ramainen
Copy link

go version go1.12.5 windows/amd64
xml-stream-parser: latest ("Skip XML declatarions at beginning" commit)
There is error while try to use Stream(),

for xml := range parserXML.Stream() {

xml.Err.Error() contains "Invalid xml"

I checked out, there is sendError() method shows this, and error actually contains "bufio: invalid use of UnreadByte".

He sterted at here: https://github.com/tamerh/xml-stream-parser/blob/master/xmlparser.go#L453

and before at here:

err := x.reader.UnreadByte()

Next is bufio itself.

@ramainen
Copy link
Author

It works well at another PC, go1.11.4 windows/amd64, if it helps

@ramainen
Copy link
Author

ramainen commented Jun 21, 2019

in UnreadByte code:

// UnreadByte unreads the last byte. Only the most recently read byte can be unread.
//
// UnreadByte returns an error if the most recent method called on the
// Reader was not a read operation. Notably, Peek is not considered a
// read operation.

previous call is exactly Peek:

b, err = x.reader.Peek(1) // this is needed because we cant unread 2 bytes consequtively

@ramainen
Copy link
Author

ramainen commented Jun 21, 2019

If change code to:

	//	b, err = x.reader.Peek(1) // this is needed because we cant unread 2 bytes consequtively
			z, err := x.readByte()

			if err != nil {
				return err
			}

			if z == '!' || z == '?' { // either comment or decleration

it works, but i do not know, how well.

@ramainen
Copy link
Author

Can I make pull request with this?

@tamerh
Copy link
Owner

tamerh commented Jun 21, 2019

Hi @ramainen thanks for raising this issue.

I realised that i was using 1.11 and Peek function has changed in 1.12
https://github.com/golang/go/blob/release-branch.go1.11/src/bufio/bufio.go
https://github.com/golang/go/blob/release-branch.go1.12/src/bufio/bufio.go

unfortunately we can't replace the peek with the readByte like in your pull request, in that way we might lost bytes.

I am now reviewing and fixing the function that use Peek using 1.12. Let me know if you have suggestions.

@tamerh tamerh closed this as completed in cf60436 Jun 21, 2019
@tamerh
Copy link
Owner

tamerh commented Jun 21, 2019

I fixed the function now seems that working with both 1.11 and 1.12

@tamerh tamerh reopened this Jun 21, 2019
tamerh pushed a commit that referenced this issue Jun 21, 2019
@tamerh
Copy link
Owner

tamerh commented Jun 21, 2019

Hopefully now fixed with parser version v1.14

@ramainen
Copy link
Author

So fast, thank you

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

2 participants