-
Notifications
You must be signed in to change notification settings - Fork 29
[BUG] Server API uses empty database path when current directory is invalid #44
Description
Project
vgrep
Description
The server API handlers status() and search() use db_path().unwrap_or_default() which silently converts a Result::Err into an empty PathBuf. When std::env::current_dir() fails (which db_path() calls internally), the database is opened with an empty path "", causing undefined behavior or silent failures.
Error Message
# When the bug triggers, you may see confusing errors like:
{"error":"Failed to open database: unable to open database file: "}
# Or worse, no error at all if SQLite creates a database with empty nameDebug Logs
# No specific debug logs - the error is silently converted to a default value
# The problematic code at src/server/api.rs:146 and :231:
let db = match Database::new(&state.config.db_path().unwrap_or_default()) {System Information
Affects all platforms. Issue is in source code logic, not environment-specific.
- OS: Any
- vgrep version: current main branch
- File: src/server/api.rs, lines 146 and 231Screenshots
No response
Steps to Reproduce
- Start vgrep server: cd /tmp/testdir && vgrep serve
- In another terminal, delete the server's working directory: rm -rf /tmp/testdir
- Send a search request: curl -X POST http://localhost:7777/search -H "Content-Type: application/json" -d '{"query":"test"}'
- Observe undefined database behavior
Expected Behavior
The server should return a clear HTTP 500 error with a message like "Failed to determine database path: current directory is invalid" when db_path() fails.
Actual Behavior
The server silently uses an empty path for the database, leading to:
- Confusing error messages about empty paths
- Potential creation of a database file with empty/invalid name
- Wrong database being accessed in edge cases
Additional Context
// Instead of:
let db = Database::new(&state.config.db_path().unwrap_or_default())?;
// Should be:
let db_path = state.config.db_path().map_err(|e| {
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({"error": format!("Failed to get database path: {}", e)})))
})?;
let db = Database::new(&db_path)?;