Fix division by zero issue#1080
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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 inminja::Value::operator%. - Add
ChatTemplateDivisionByZerotest to validateOrtxApplyChatTemplatefails 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.
kunal-vaishnavi
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request adds explicit error handling for division and modulo by zero in the
Valueclass 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:
Valueclass's division (operator/) and modulo (operator%) operators to throw astd::runtime_errorwhen attempting to divide or modulo by zero, preventing undefined behavior and crashes.Testing enhancements:
ChatTemplateDivisionByZero, to verify that division and modulo by zero in chat templates return an error code instead of causing a crash (e.g., SIGFPE).