Skip to content

Commit c594668

Browse files
committed
add boundschecks to fix #35
1 parent e464a6a commit c594668

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/LibSpatialIndex.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ module LibSpatialIndex
179179
minvalues::Vector{Float64},
180180
maxvalues::Vector{Float64}
181181
)
182+
length(minvalues) == rtree.ndim || throw(DimensionMismatch("Minimum values must have same length as RTree dimensions"))
183+
length(maxvalues) == rtree.ndim || throw(DimensionMismatch("Maximum values must have same length as RTree dimensions"))
182184
C.Index_InsertData(rtree.index, Int64(id), pointer(minvalues),
183-
pointer(maxvalues), UInt32(length(minvalues)), Ptr{UInt8}(0), Cint(0)
185+
pointer(maxvalues), UInt32(rtree.ndim), Ptr{UInt8}(0), Cint(0)
184186
)
185187
end
186188
function insert!(rtree::RTree, id::Integer, extent::GI.Extent)
@@ -213,10 +215,13 @@ module LibSpatialIndex
213215
minvalues::Vector{Float64},
214216
maxvalues::Vector{Float64}
215217
)
218+
length(minvalues) == rtree.ndim || throw(DimensionMismatch("Minimum values must have same length as RTree dimensions"))
219+
length(maxvalues) == rtree.ndim || throw(DimensionMismatch("Maximum values must have same length as RTree dimensions"))
220+
216221
items = Ref{Ptr{Int64}}()
217222
nresults = Ref{UInt64}()
218223
result = C.Index_Intersects_id(rtree.index, pointer(minvalues),
219-
pointer(maxvalues), UInt32(length(minvalues)), items, nresults
224+
pointer(maxvalues), UInt32(rtree.ndim), items, nresults
220225
)
221226
_checkresult(result, "Index_Intersects_id: Failed to evaluate")
222227
unsafe_wrap(Array, items[], nresults[])
@@ -260,10 +265,13 @@ module LibSpatialIndex
260265
maxvalues::Vector{Float64},
261266
k::Integer
262267
)
268+
length(minvalues) == rtree.ndim || throw(DimensionMismatch("Minimum values must have same length as RTree dimensions"))
269+
length(maxvalues) == rtree.ndim || throw(DimensionMismatch("Maximum values must have same length as RTree dimensions"))
270+
263271
items = Ref{Ptr{Int64}}()
264272
nresults = Ref{UInt64}(k)
265273
result = C.Index_NearestNeighbors_id(rtree.index, pointer(minvalues),
266-
pointer(maxvalues), UInt32(length(minvalues)), items, nresults)
274+
pointer(maxvalues), UInt32(rtree.ndim), items, nresults)
267275
_checkresult(result, "Index_NearestNeighbors_id: Failed to evaluate")
268276
unsafe_wrap(Array, items[], nresults[])
269277
end

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ end
7070
@test sort(SI.knn(rtree, (2.0, 2.0), 1)) == [2]
7171
end
7272

73+
@testset "Bounds checks" begin
74+
# confirm that bounds checks on the Julia side are working
75+
tree = SI.RTree(2)
76+
@test_throws DimensionMismatch SI.insert!(tree, 0, [0.0], [0.0, 0.1])
77+
@test_throws DimensionMismatch SI.insert!(tree, 0, [0.0, 1.0], [0.1])
78+
# should throw even if they're the same length if they don't match dimensions
79+
@test_throws DimensionMismatch SI.insert!(tree, 0, [0.0, 0.1, 1.1], [0.0, 0.1, 1.1])
80+
81+
@test_throws DimensionMismatch SI.intersects(tree, [0.0], [0.0, 0.1])
82+
@test_throws DimensionMismatch SI.intersects(tree, [0.0, 1.0], [0.1])
83+
@test_throws DimensionMismatch SI.intersects(tree, [0.0, 0.1, 1.1], [0.0, 0.1, 1.1])
84+
85+
@test_throws DimensionMismatch SI.knn(tree, [0.0], [0.0, 0.1], 1)
86+
@test_throws DimensionMismatch SI.knn(tree, [0.0, 1.0], [0.1], 1)
87+
@test_throws DimensionMismatch SI.knn(tree, [0.0, 0.1, 1.1], [0.0, 0.1, 1.1], 1)
88+
end
89+
7390
@testset "Aqua" begin
7491
Aqua.test_all(LibSpatialIndex)
7592
end

0 commit comments

Comments
 (0)