Skip to content

CSV: Input byte array may be modified #680

@fcharlie

Description

@fcharlie

I use mimetype extensively in my internal code, but I recently discovered a weird bug where the data returned to the user was corrupted.

We first read 4K bytes of data to mimetype to detect the file type, then use io.MultiReader to merge the 4K data with the remaining reader and return it to the user. I found that the 4K bytes of data content was modified, and finally located the CSV detection parser.

func (r *Parser) readLine() []byte {
	line := r.s.ReadSlice('\n')
	n := len(line)
	if n > 0 && line[n-1] == '\r' {
		return line[:n-1] // drop \r at end of line
	}

	// Normalize \r\n to \n on all input lines.
	if n := len(line); n >= 2 && line[n-2] == '\r' && line[n-1] == '\n' {
		line[n-2] = '\n'
		return line[:n-1]
	}
	return line
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions