Skip to content

Instantly share code, notes, and snippets.

@davisp
Created February 20, 2015 07:50
Show Gist options
  • Select an option

  • Save davisp/ed423c9f29c3e17fe824 to your computer and use it in GitHub Desktop.

Select an option

Save davisp/ed423c9f29c3e17fe824 to your computer and use it in GitHub Desktop.
Trigger bug in libspatialindex index validation
#include <assert.h>
#include <stdio.h>
#include <spatialindex/capi/sidx_api.h>
void
insert_box(IndexH idx, int id, double x, double y, double l)
{
double mins[] = {x - l, y - l};
double maxs[] = {x + l, y + l};
if(Index_InsertData(idx, id, mins, maxs, 2, NULL, 0) != RT_None) {
fprintf(stderr, "Error inserting box: %s\n", Error_GetLastErrorMsg());
exit(2);
}
fprintf(stderr, "ID: %d\n", id);
Index_IsValid(idx);
}
void
remove_box(IndexH idx, int id, double x, double y, double l)
{
double mins[] = {x - l, y - l};
double maxs[] = {x + l, y + l};
if(Index_DeleteData(idx, id, mins, maxs, 2) != RT_None) {
fprintf(stderr, "Error removing box: %s\n", Error_GetLastErrorMsg());
exit(3);
}
fprintf(stderr, "ID: %d\n", id);
Index_IsValid(idx);
}
int
main(int argc, char* argv[])
{
IndexH idx = NULL;
IndexPropertyH props = IndexProperty_Create();
if(IndexProperty_SetIndexStorage(props, RT_Memory) != RT_None) {
exit(1);
}
if(IndexProperty_SetIndexType(props, RT_RTree) != RT_None) {
exit(1);
}
if(IndexProperty_SetIndexVariant(props, RT_Linear) != RT_None) {
exit(1);
}
if(IndexProperty_SetFillFactor(props, 0.49) != RT_None) {
exit(1);
}
if(IndexProperty_SetNearMinimumOverlapFactor(props, 3) != RT_None) {
exit(1);
}
if(IndexProperty_SetDimension(props, 2) != RT_None) {
exit(1);
}
if(IndexProperty_SetIndexCapacity(props, 4) != RT_None) {
exit(1);
}
if(IndexProperty_SetLeafCapacity(props, 4) != RT_None) {
exit(1);
}
idx = Index_Create(props);
if(idx == NULL) {
fprintf(stderr, "Error creating index: %s\n", Error_GetLastErrorMsg());
exit(1);
}
fprintf(stderr, "Inserting left boxes\n");
insert_box(idx, 1, -5.0, 0.0, 1.0);
insert_box(idx, 2, -5.0, 0.0, 1.1);
insert_box(idx, 3, -5.0, 0.0, 1.2);
insert_box(idx, 4, -5.0, 0.0, 1.3);
fprintf(stderr, "Inserting right boxes\n");
insert_box(idx, 5, 5.0, 0.0, 1.0);
insert_box(idx, 6, 5.0, 0.0, 1.1);
insert_box(idx, 7, 5.0, 0.0, 1.2);
insert_box(idx, 8, 5.0, 0.0, 1.3);
fprintf(stderr, "Inserting box to split\n");
insert_box(idx, 9, 6.0, 0.0, 1.4);
fprintf(stderr, "Remove last box\n");
remove_box(idx, 9, 6.0, 0.0, 1.4);
fprintf(stderr, "\n\nValidating...\n");
if(!Index_IsValid(idx)) {
fprintf(stderr, "FAIL\n");
} else {
fprintf(stderr, "OK\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment