Skip to content

[BUG] Multiplication overflow on max_results * 3 causes panic or wrong results #126

@Cute0110

Description

@Cute0110

Project

vgrep

Description

The search code multiplies max_results by 3 for reranking purposes, but this multiplication can overflow for large values, causing panic in debug mode or incorrect behavior in release mode.

Error Observation

With large max_results values, the multiplication overflows.

Error Message

thread 'main' panicked at 'attempt to multiply with overflow'

Debug Logs

System Information

Version: 0.1.0

## Operating System
  OS: Ubuntu 24.04.3 LTS
  Kernel: 6.8.0-79-generic
  Arch: x86_64

## Hardware
  CPU: AMD Ryzen 9 5950X 16-Core Processor (4 cores)
  RAM: 11 GB

## Build Environment
  Rust: rustc 1.92.0 (ded5c06cf 2025-12-08)
  Target: x86_64

Screenshots

No response

Steps to Reproduce

  1. Open src/core/search.rs line 51:
let candidates = self
    .db
    .search_similar(&query_embedding, &abs_path, max_results * 3)?;  // OVERFLOW!
  1. Open src/core/db.rs line 197:
results.truncate(limit * 3); // Get more for reranking - OVERFLOW!
  1. Set max_results = 6148914691236517206 (usize::MAX / 3 + 1)
  2. Observe overflow panic or wrapping behavior

Expected Behavior

Should use checked_mul or saturating_mul to prevent overflow.

Actual Behavior

Panic in debug mode; silent wraparound producing wrong results in release mode.

Additional Context

No response

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