Skip to content

Commit

Permalink
Add safety checks in sniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
fbbdev committed Feb 6, 2025
1 parent c53e0dc commit 4c08fec
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions v3/internal/assetserver/content_type_sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func (rw *contentTypeSniffer) Write(chunk []byte) (int, error) {
return 0, nil
}

// Cut away at most 512 bytes from chunk.
cut := min(len(chunk), 512-len(rw.prefix))
// Cut away at most 512 bytes from chunk, and not less than 0.
cut := max(min(len(chunk), 512-len(rw.prefix)), 0)
if cut >= 512 {
// Avoid copying data if a full prefix is available on first non-zero write.
cut = len(chunk)
rw.prefix = chunk
chunk = nil
} else {
} else if cut > 0 {
// First write had less than 512 bytes -- copy data to the prefix buffer.
if rw.prefix == nil {
// Preallocate space for the prefix to be used for sniffing.
Expand Down

0 comments on commit 4c08fec

Please sign in to comment.