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

Check buffer lengths in stream filtering functions before access #275

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Filters/stream_filt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ end

function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRDecimator{Th}}, x::AbstractVector{Tx}) where {Tb,Th,Tx}
kernel = self.kernel
bufLen = length(buffer)
xLen = length(x)
history::Vector{Tx} = self.history
bufIdx = 0
Expand All @@ -552,6 +553,9 @@ function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRDecimator{Th}}, x:
outLen = outputlength(self, xLen)
inputIdx = kernel.inputDeficit

nbufout = fld(xLen - inputIdx, kernel.decimation) + 1
bufLen >= nbufout || throw(ArgumentError("buffer length insufficient"))

while inputIdx <= xLen
bufIdx += 1

Expand Down Expand Up @@ -600,7 +604,11 @@ function update(kernel::FIRArbitrary)
kernel.α = kernel.ϕAccumulator - kernel.ϕIdx
end

function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRArbitrary{Th}}, x::AbstractVector{Tx}) where {Tb,Th,Tx}
function filt!(
buffer::AbstractVector{Tb},
self::FIRFilter{FIRArbitrary{Th}},
x::AbstractVector{Tx}
) where {Tb,Th,Tx}
kernel = self.kernel
pfb = kernel.pfb
dpfb = kernel.dpfb
Expand Down Expand Up @@ -632,7 +640,9 @@ function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRArbitrary{Th}}, x:
yUpper = unsafe_dot(dpfb, kernel.ϕIdx, x, kernel.xIdx)
end

@inbounds buffer[bufIdx] = yLower + yUpper * kernel.α
# Used to have @inbounds. Restore @inbounds if buffer length
# can be verified prior to access.
buffer[bufIdx] = yLower + yUpper * kernel.α
update(kernel)
end

Expand Down