Skip to content

fix(autoware_ndt_scan_matcher): zero div if particles_num < 20#996

Open
kazu-321 wants to merge 7 commits intoautowarefoundation:mainfrom
kazu-321:fix/ndt_particle_zero_div
Open

fix(autoware_ndt_scan_matcher): zero div if particles_num < 20#996
kazu-321 wants to merge 7 commits intoautowarefoundation:mainfrom
kazu-321:fix/ndt_particle_zero_div

Conversation

@kazu-321
Copy link
Copy Markdown

Description

This fixes an error that occurs when particles_num is less than 20.
publish_interval becomes 0, resulting in division by zero.
This causes the node to crash with an error at runtime.
This change avoids division by zero by adding publish_interval != 0 before the division if publish_interval is 0.

Related links

Parent Issue:

  • Link

How was this PR tested?

Confirm that no error occurs when particles_num is set to less than 20.

Notes for reviewers

None.

Interface changes

None.

Effects on system behavior

None.

Signed-off-by: kazu-321 <kzs321kzs@gmail.com>
Copilot AI review requested due to automatic review settings March 26, 2026 01:40
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 26, 2026

Thank you for contributing to the Autoware project!

🚧 If your pull request is in progress, switch it to draft mode.

Please ensure:

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 runtime crash in autoware_ndt_scan_matcher that can occur during Monte Carlo initial pose estimation when initial_pose_estimation.particles_num is less than 20, by preventing a modulo-by-zero in the marker publish logic.

Changes:

  • Guarded the marker publish condition to avoid evaluating (i + 1) % publish_interval when publish_interval == 0.
  • Minor formatting adjustment to the out_of_map_range warning if statement.

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

Comment thread localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp Outdated
Comment thread localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp Outdated
Comment thread localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp Outdated
@YamatoAndo YamatoAndo added the run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci) label Mar 26, 2026
kazu-321 and others added 2 commits April 10, 2026 00:30
@xmfcx xmfcx force-pushed the fix/ndt_particle_zero_div branch from 287b0ab to 003e245 Compare April 9, 2026 21:31
Comment on lines +1143 to +1144
(publish_interval != 0 && (i + 1) % publish_interval == 0) ||
(i + 1) == param_.initial_pose_estimation.particles_num) {
Copy link
Copy Markdown
Contributor

@xmfcx xmfcx Apr 9, 2026

Choose a reason for hiding this comment

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

Consider fixing this at the source by clamping publish_interval to at least 1, rather than guarding the modulo downstream.

Change line 1115

const int64_t publish_interval = param_.initial_pose_estimation.particles_num / publish_num;

to:

const int64_t publish_interval =
  std::max<int64_t>(param_.initial_pose_estimation.particles_num / publish_num, 1);

Then this condition can stay unchanged:

Suggested change
(publish_interval != 0 && (i + 1) % publish_interval == 0) ||
(i + 1) == param_.initial_pose_estimation.particles_num) {
(i + 1) % publish_interval == 0 || (i + 1) == param_.initial_pose_estimation.particles_num) {

This eliminates the zero divisor at the point of definition instead of working around it at the point of use. For small particle counts (< 20), it publishes progress every iteration, which is fine since the total is still under the 20-publish budget.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.98%. Comparing base (d0600fb) to head (7eeaa03).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #996   +/-   ##
=======================================
  Coverage   55.97%   55.98%           
=======================================
  Files         378      378           
  Lines       23654    23654           
  Branches    11175    11176    +1     
=======================================
+ Hits        13241    13242    +1     
  Misses       7540     7540           
+ Partials     2873     2872    -1     
Flag Coverage Δ *Carryforward flag
daily-humble 55.46% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb
daily-jazzy 56.17% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb
differential-humble 42.56% <100.00%> (?)
differential-jazzy 41.39% <100.00%> (?)
total 49.98% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb
total-humble 55.46% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb
total-humble-agnocast 55.30% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb
total-jazzy 55.64% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb
total-jazzy-agnocast 55.48% <ø> (-0.01%) ⬇️ Carriedforward from d0600fb

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anhnv3991
Copy link
Copy Markdown
Contributor

@kazu-321 This looks fine but do you have any dataset where the error occurs? I want to confirm that the patch can really fix the issue, i.e. before applying the patch the error occurred, but after applying the patch the error has gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run:build-and-test-differential Mark to enable build-and-test-differential workflow. (used-by-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants