Skip to content

Commit

Permalink
Check buffer lengths in stream filtering functions before access (#275)
Browse files Browse the repository at this point in the history
* Check buffer lengths in stream filtering functions before access

A number of exported functions in stream_filt.jl have @inbounds access to
buffers without verifying their lengths. This can cause segfaults, as seen in
issue #262.

* Fix buffer length checking

I can't figure out the buffer length requirement for one function, and removed
the `@inbounds` macro until I can.

* Update src/Filters/stream_filt.jl

Co-Authored-By: galenlynch <[email protected]>

* Update src/Filters/stream_filt.jl

Co-Authored-By: galenlynch <[email protected]>

* Fix estimation of buffer length required
  • Loading branch information
galenlynch authored Apr 11, 2019
1 parent 48b2281 commit 431cccd
Showing 1 changed file with 12 additions and 2 deletions.
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

0 comments on commit 431cccd

Please sign in to comment.