wrapUserFile rejects non-regular sources and returns an fs.File that errors out if more than maxUserFileBytes are read from it.
(f fs.File, name string)
| 1891 | // wrapUserFile rejects non-regular sources and returns an fs.File that |
| 1892 | // errors out if more than maxUserFileBytes are read from it. |
| 1893 | func wrapUserFile(f fs.File, name string) (fs.File, error) { |
| 1894 | info, err := f.Stat() |
| 1895 | if err != nil { |
| 1896 | f.Close() |
| 1897 | return nil, fmt.Errorf("stat %s: %w", name, err) |
| 1898 | } |
| 1899 | if !info.Mode().IsRegular() { |
| 1900 | f.Close() |
| 1901 | return nil, fmt.Errorf("%s is not a regular file", name) |
| 1902 | } |
| 1903 | return &limitedFile{ |
| 1904 | File: f, |
| 1905 | // Allow one byte past the cap so an overflow surfaces as an |
| 1906 | // error rather than a silent EOF that the parser would treat as |
| 1907 | // a clean end-of-file (and miss any entries past the cap). |
| 1908 | r: &io.LimitedReader{R: f, N: maxUserFileBytes + 1}, |
| 1909 | name: name, |
| 1910 | }, nil |
| 1911 | } |
| 1912 | |
| 1913 | // limitedFile is an fs.File whose Read returns an error once more than |
| 1914 | // maxUserFileBytes have been read. |
no test coverage detected
searching dependent graphs…