Skip to content

atc: fix divide by zero in subnet_calc - #17

Open
billzhong wants to merge 1 commit into
mrhaav:mainfrom
billzhong:fix-subnet-calc-divide-by-zero
Open

atc: fix divide by zero in subnet_calc#17
billzhong wants to merge 1 commit into
mrhaav:mainfrom
billzhong:fix-subnet-calc-divide-by-zero

Conversation

@billzhong

Copy link
Copy Markdown

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.

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.
Copilot AI lite review requested due to automatic review settings August 13, 2026 18:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 y and reach D % 0, with a /24 fallback for .0 and .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.

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.

2 participants