bus error on usage of rusqlite with spatialite extension

258 Views Asked by At

I'm seeing a bus error on cargo run when attempting to load the spatialite extension with rusqlite:

Finished dev [unoptimized + debuginfo] target(s) in 1.19s
Running `target/debug/rust-spatialite-example`
[1]    33253 bus error  cargo run --verbose

My suspicion is that there's a mismatch of sqlite version and spatialite and that they need to be built together rather than using the bundled feature of rusqlite, though it seems like that'd result in a different error?

Here's how things are set up:

Cargo.toml

[package]
name = "rust-spatialite-example"
version = "0.0.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rusqlite = { version = "0.28.0", features = ["load_extension", "bundled"] }

init.sql

CREATE TABLE place (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);
SELECT AddGeometryColumn('place', 'geom', 4326, 'POINT', 'XY', 0);
SELECT CreateSpatialIndex('place', 'geom');

main.rs

use rusqlite::{Connection, Result, LoadExtensionGuard};

#[derive(Debug)]
struct Place {
    id: i32,
    name: String,
    geom: String,
}

fn load_spatialite(conn: &Connection) -> Result<()> {
    unsafe {
        let _guard = LoadExtensionGuard::new(conn)?;
        conn.load_extension("/opt/homebrew/Cellar/libspatialite/5.0.1_2/lib/mod_spatialite", None)
    }
}

fn main() -> Result<()> {
    let conn = Connection::open("./geo.db")?;

    load_spatialite(&conn)?;

    // ... sql statements that aren't executed

    Ok(())
}

Running:

cat init.sql | spatialite geo.db
cargo run

The mod_spatialite path is correct (there's an expected SqliteFailure error when that path is wrong). I tried explicitly setting sqlite3_modspatialite_init as the entry point and the behavior stayed the same.

1

There are 1 best solutions below

0
jallen7usa On

I ran into this recently as well. The underlying error message I was provided was "not authorized" which eventually led me to load_extension_enable.

I started with basically the same code as you then added calls to load_extension_enable (and load_extension_disable) which resolved the issue for me.

Here's an updated version of your load_spatialite function.

fn load_spatialite(conn: &Connection) -> Result<()> {
    unsafe {
        let _guard = LoadExtensionGuard::new(conn)?;
        conn.load_extension_enable()?;
        conn.load_extension("/opt/homebrew/Cellar/libspatialite/5.0.1_2/lib/mod_spatialite", None)
        conn.load_extension_disable()?;
    }
}