Skip to content

[BUG] Server API uses empty database path when current directory is invalid #44

@Crimsonyx412

Description

@Crimsonyx412

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 name

Debug 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 231

Screenshots

No response

Steps to Reproduce

  1. Start vgrep server: cd /tmp/testdir && vgrep serve
  2. In another terminal, delete the server's working directory: rm -rf /tmp/testdir
  3. Send a search request: curl -X POST http://localhost:7777/search -H "Content-Type: application/json" -d '{"query":"test"}'
  4. 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)?;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvalidValid issuevgrep

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions