atc: fix divide by zero in subnet_calc - #17
Open
billzhong wants to merge 1 commit into
Open
Conversation
subnet_calc() widens the block until the address is neither the network nor the broadcast address of it. For an address ending in .0 that condition is never met: D%y stays 0 for every y, so the loop keeps doubling y until it overflows to a negative value and then to 0, and $((D%y)) aborts with "division by 0". The function prints nothing, which leaves v4netmask and v4gateway empty and breaks the interface setup. Bound the search at /24, which is also the shortest prefix this function can express since only the last octet is replaced when the gateway is printed. x.x.x.0 and x.x.x.255 now fall back to /24 with x.x.x.1 as the gateway. The gateway is now taken as the first host of the block, stepping to the second one when that is the address itself. This is equivalent to the previous res==1 special case and also holds for the new /24 fallback. Results are unchanged for every address ending in .1 to .254. The function is duplicated across all atc packages, so all of them get the same fix: atc-fib-fg621_ea, atc-fib-fm350_gl, atc-fib-l850_gl, atc-fib-l8x0_gl, atc-hua-e3372h_320, atc-hua-me909s_120, atc-mik-r11e_lte, atc-mik-r11e_lte6 and atc-zte-mf286r.
There was a problem hiding this comment.
Pull request overview
This PR fixes a long-standing edge case in the subnet_calc() helper used by multiple atc-* netifd proto scripts, preventing a modulo-by-zero crash when the IPv4 address ends in .0 (and addressing .255 similarly). The change keeps existing behavior for typical host addresses while ensuring interface setup does not break due to empty v4netmask/v4gateway.
Changes:
- Bound the subnet “widening” loop so it cannot overflow
yand reachD % 0, with a/24fallback for.0and.255. - Simplified gateway selection to “first host in block, or second if it matches the local address,” matching the prior special-case behavior.
- Applied the identical fix across all
atc-*packages that carry this duplicated function.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| atc-zte-mf286r/files/lib/netifd/proto/atc.sh | Fixes subnet_calc() loop overflow and stabilizes gateway selection. |
| atc-mik-r11e_lte6/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-mik-r11e_lte/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-hua-me909s_120/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-hua-e3372h_320/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-fib-l8x0_gl/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-fib-l850_gl/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-fib-fm350_gl/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
| atc-fib-fg621_ea/files/lib/netifd/proto/atc.sh | Same subnet_calc() safety bound and gateway computation update. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
subnet_calc() widens the block until the address is neither the network nor the broadcast address of it. For an address ending in .0 that condition is never met: D%y stays 0 for every y, so the loop keeps doubling y until it overflows to a negative value and then to 0, and $((D%y)) aborts with "division by 0". The function prints nothing, which leaves v4netmask and v4gateway empty and breaks the interface setup.
Bound the search at /24, which is also the shortest prefix this function can express since only the last octet is replaced when the gateway is printed. x.x.x.0 and x.x.x.255 now fall back to /24 with x.x.x.1 as the gateway.
The gateway is now taken as the first host of the block, stepping to the second one when that is the address itself. This is equivalent to the previous res==1 special case and also holds for the new /24 fallback.
Results are unchanged for every address ending in .1 to .254.
The function is duplicated across all atc packages, so all of them get the same fix: atc-fib-fg621_ea, atc-fib-fm350_gl, atc-fib-l850_gl, atc-fib-l8x0_gl, atc-hua-e3372h_320, atc-hua-me909s_120, atc-mik-r11e_lte, atc-mik-r11e_lte6 and atc-zte-mf286r.