Skip to content

Fix division by zero issue#1080

Merged
apsonawane merged 4 commits into
mainfrom
asonawane/tokenizer-icm
Jun 12, 2026
Merged

Fix division by zero issue#1080
apsonawane merged 4 commits into
mainfrom
asonawane/tokenizer-icm

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request adds explicit error handling for division and modulo by zero in the Value class and introduces corresponding tests to ensure the application fails gracefully instead of crashing. The changes improve the robustness of mathematical operations and add tests to verify correct error handling.

Error handling improvements:

  • Added explicit checks in the Value class's division (operator/) and modulo (operator%) operators to throw a std::runtime_error when attempting to divide or modulo by zero, preventing undefined behavior and crashes.

Testing enhancements:

  • Added a new test, ChatTemplateDivisionByZero, to verify that division and modulo by zero in chat templates return an error code instead of causing a crash (e.g., SIGFPE).

Copilot AI review requested due to automatic review settings June 12, 2026 05:25
@apsonawane apsonawane requested a review from a team as a code owner June 12, 2026 05:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request hardens Minja template math evaluation by preventing undefined behavior (SIGFPE) on division/modulo by zero, and adds a regression test through the public chat template API path to ensure failures return a non-OK error code instead of crashing.

Changes:

  • Add explicit runtime error throwing for division by zero in minja::Value::operator/ and modulo by zero in minja::Value::operator%.
  • Add ChatTemplateDivisionByZero test to validate OrtxApplyChatTemplate fails gracefully for {{ 1/0 }} and {{ 2%0 }}.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
shared/api/minja.hpp Adds explicit zero-denominator checks in Value division/modulo operators to avoid SIGFPE/UB.
test/pp_api_test/test_tokenizer_chat.cc Adds a regression test ensuring division/modulo by zero in chat templates returns an error code.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread shared/api/minja.hpp
Comment thread shared/api/minja.hpp
Comment thread test/pp_api_test/test_tokenizer_chat.cc Outdated
Comment thread test/pp_api_test/test_tokenizer_chat.cc Outdated
@apsonawane apsonawane enabled auto-merge (squash) June 12, 2026 21:35
@apsonawane apsonawane merged commit f6940f3 into main Jun 12, 2026
38 checks passed
@apsonawane apsonawane deleted the asonawane/tokenizer-icm branch June 12, 2026 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants