Skip to content

bufio: bug in the emptyFinalToken example for Scanner #25909

@pam4

Description

@pam4

The SplitFunc in the emptyFinalToken example for bufio.Scanner doesn't handle the case of requesting more data if necessary.

The example only happens to work because the whole input is returned by a single call to Read (it uses a strings.Reader).

This should be enough to fix it:

@@ -21,7 +21,10 @@
        // There is one final token to be delivered, which may be the empty string.
        // Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this
        // but does not trigger an error to be returned from Scan itself.
-       return 0, data, bufio.ErrFinalToken
+       if atEOF {
+           return 0, data, bufio.ErrFinalToken
+       }
+       return 0, nil, nil
    }
    scanner.Split(onComma)
    // Scan.

The problem is demonstrated here:
https://play.golang.org/p/CM-sWovg8mg

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions