Skip to content

[Bug] Fix undefined behavior in INT8Quantizer::ComputeImpl due to missing return statement #1718

@LHT129

Description

@LHT129

Description

The ComputeImpl method in INT8Quantizer class has undefined behavior when the L2SQR branch is used. The function may reach the end without a return statement, which is undefined behavior for a non-void function.

Background

During code review of the quantization module, it was discovered that the ComputeImpl method in src/quantization/int8_quantizer.cpp has a code path that could lead to undefined behavior:

  • The IP and COSINE branches use if constexpr, which allows compile-time branch elimination
  • The L2SQR branch uses a regular if (without constexpr), so the compiler cannot eliminate the fall-through path
  • If control flow doesn't enter any branch (theoretically shouldn't happen, but the compiler can't prove it), the function ends without a return statement
  • Missing return in a non-void function is undefined behavior, and the compiler may generate unpredictable code

Requirements

  • Add static_assert to ensure only supported metric types are used
  • Change else if to else if constexpr for L2SQR branch
  • Ensure all branches use if constexpr for compile-time branch elimination
  • Eliminate undefined behavior

Technical Details

File Modified: src/quantization/int8_quantizer.cpp

Changes Made:

  1. Added static_assert at the beginning of ComputeImpl function to validate metric type
  2. Changed else if (metric == MetricType::METRIC_TYPE_L2SQR) to else if constexpr (metric == MetricType::METRIC_TYPE_L2SQR)

Acceptance Criteria

  • Code compiles without warnings
  • All existing tests pass
  • No undefined behavior in the function
  • Compile-time branch elimination works correctly for all metric types

Related

  • Original task file: agent-hive/tasks/2026-03-18-修复-int8-quantizer-computeimpl-缺少-return.md
  • Reference implementation: ComputeDistImpl function in the same file (lines 156-186)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions