Skip to content

Clang-Tidy: cppcoreguidelines-narrowing-conversions warning and fix#3098

Open
Ritesh200422 wants to merge 6 commits intopgRouting:developfrom
Ritesh200422:trial-branch
Open

Clang-Tidy: cppcoreguidelines-narrowing-conversions warning and fix#3098
Ritesh200422 wants to merge 6 commits intopgRouting:developfrom
Ritesh200422:trial-branch

Conversation

@Ritesh200422
Copy link

@Ritesh200422 Ritesh200422 commented Mar 22, 2026

Fixes # .

Changes proposed in this pull request:

  • A narrowing conversion warning was shown when an unsigned long value was passed to a function expecting an int.
  • I fixed it by implementing a static cast.
  • adding run time check as a safety check before the static casting.

@pgRouting/admins

Summary by CodeRabbit

  • Chores
    • Updated static analysis configuration to re-enable narrowing-conversion checks, strengthening code-quality enforcement.
  • Bug Fixes
    • Added a runtime validation when adding vertices to guard against integer conversion/overflow issues during graph growth.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 22, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Removed the explicit disable for clang-tidy cppcoreguidelines-narrowing-conversions, and made vertex insertion explicitly check for int overflow and use a safe static_cast<int> when calling add_vertex().

Changes

Cohort / File(s) Summary
Clang-tidy configuration
.clang-tidy
Removed -cppcoreguidelines-narrowing-conversions from the Checks list so the narrowing-conversions check is no longer disabled.
Graph implementation
src/cpp_common/undirectedHasCostBG.cpp
Added <cassert>/<stdexcept> includes; capture vertex count into vertex_count, validate vertex_count <= std::numeric_limits<int>::max() and throw std::overflow_error("vertex index overflow") on overflow; use static_cast<int>(vertex_count) when calling add_vertex.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Enhancement, C/C++

Suggested reviewers

  • cvvergara
  • robe2

Poem

🐇 I hopped through headers, checked each size,

Caught a narrow fall before it could rise.
A cast, a guard, a careful cheer—
Safe vertices now hop without fear. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: enabling the cppcoreguidelines-narrowing-conversions check and implementing a fix with overflow validation.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cpp_common/undirectedHasCostBG.cpp`:
- Line 169: The current call auto v =
add_vertex(static_cast<int>(m_id_to_V.size()), m_graph); performs a narrowing
cast from size_t to int; add a runtime guard to assert or check that
m_id_to_V.size() <= static_cast<size_t>(std::numeric_limits<int>::max()) before
the static_cast to prevent overflow, and fail-fast (assert/throw/log) if the
check fails—update the code around the add_vertex call (referencing m_id_to_V,
add_vertex, and m_graph) to perform this guard and then perform the safe
static_cast.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 54727ade-1bd8-4797-84c6-cc9576beb225

📥 Commits

Reviewing files that changed from the base of the PR and between e06c177 and cc1528d.

📒 Files selected for processing (2)
  • .clang-tidy
  • src/cpp_common/undirectedHasCostBG.cpp
💤 Files with no reviewable changes (1)
  • .clang-tidy

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cpp_common/undirectedHasCostBG.cpp`:
- Around line 35-36: The file undirectedHasCostBG.cpp contains a redundant
duplicate include of <limits>; remove the extra line so only one `#include`
<limits> remains alongside the other headers (the existing include block with
`#include`<cassert> and `#include` <limits>), leaving a single <limits> include to
avoid redundant headers.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 16b38034-4574-4d5a-9d2d-7cbb041974f8

📥 Commits

Reviewing files that changed from the base of the PR and between cc1528d and 6a2c3e4.

📒 Files selected for processing (1)
  • src/cpp_common/undirectedHasCostBG.cpp

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
src/cpp_common/undirectedHasCostBG.cpp (1)

34-34: 🧹 Nitpick | 🔵 Trivial

Add space after #include for consistency.

The include directive is missing a space, which is inconsistent with the other includes in this file (e.g., line 35).

🧹 Suggested fix
-#include<cassert>
+#include <cassert>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/cpp_common/undirectedHasCostBG.cpp` at line 34, The include directive for
the C assert header is missing a space; change the token '#include<cassert>' to
use the consistent spacing form '#include <cassert>' so it matches the other
includes (replace the include of the cassert header in this file).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@src/cpp_common/undirectedHasCostBG.cpp`:
- Line 34: The include directive for the C assert header is missing a space;
change the token '#include<cassert>' to use the consistent spacing form
'#include <cassert>' so it matches the other includes (replace the include of
the cassert header in this file).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 15f28932-1f47-4ceb-9c4f-5578b3c059dd

📥 Commits

Reviewing files that changed from the base of the PR and between 6a2c3e4 and 8441876.

📒 Files selected for processing (1)
  • src/cpp_common/undirectedHasCostBG.cpp

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cpp_common/undirectedHasCostBG.cpp`:
- Around line 169-170: The assert before the narrowing cast can be disabled in
release builds, so add an explicit runtime guard that checks m_id_to_V.size() <=
static_cast<size_t>(std::numeric_limits<int>::max()) before calling add_vertex;
if the check fails, handle it deterministically (e.g., throw a
std::runtime_error or call std::abort with a clear message) rather than
performing the static_cast, then proceed to call
add_vertex(static_cast<int>(m_id_to_V.size()), m_graph).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3d2738d9-0754-4f15-9ab4-5fcb1a9cb57f

📥 Commits

Reviewing files that changed from the base of the PR and between 8441876 and e85cc23.

📒 Files selected for processing (1)
  • src/cpp_common/undirectedHasCostBG.cpp

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/cpp_common/undirectedHasCostBG.cpp (1)

171-182: 🧹 Nitpick | 🔵 Trivial

The specific overflow error message is lost to the generic catch-all.

The std::overflow_error("vertex index overflow") thrown at line 172 will be caught by catch(...) at line 178, converting it to the generic message "INTERNAL: something went wrong when inserting a vertex". This loses diagnostic specificity.

Consider catching std::overflow_error explicitly before the catch-all to preserve the message:

♻️ Proposed fix to preserve overflow error context
     } catch (const std::overflow_error& e) {
+        throw std::make_pair(
+                std::string(e.what()),
+                std::string(__PGR_PRETTY_FUNCTION__));
     } catch (...) {
         throw std::make_pair(
                 std::string("INTERNAL: something went wrong when inserting a vertex"),
                 std::string(__PGR_PRETTY_FUNCTION__));
     }

Based on learnings: error messages in pgRouting are verified by pgtap tests. If this error path is exercised by tests, ensure they are updated accordingly.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/cpp_common/undirectedHasCostBG.cpp` around lines 171 - 182, The generic
catch-all is swallowing the std::overflow_error("vertex index overflow"); add an
explicit catch for std::overflow_error before the catch(...) in the block that
calls add_vertex/static_cast<int>(vertex_count) so the original error message is
preserved (either rethrow the caught overflow_error or throw a new exception
that includes e.what() combined with context like __PGR_PRETTY_FUNCTION__ and
the existing "INTERNAL: something went wrong when inserting a vertex" text);
keep the catch(...) afterwards for other exceptions. Target symbols: add_vertex,
m_id_to_V, m_V_to_id, and __PGR_PRETTY_FUNCTION__.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cpp_common/undirectedHasCostBG.cpp`:
- Line 34: The file still includes the unused header <cassert> even though the
assert macro was removed; delete the line that includes <cassert> (remove the
unused include) and verify there are no remaining uses of assert in
undirectedHasCostBG.cpp so the compilation remains clean.

---

Outside diff comments:
In `@src/cpp_common/undirectedHasCostBG.cpp`:
- Around line 171-182: The generic catch-all is swallowing the
std::overflow_error("vertex index overflow"); add an explicit catch for
std::overflow_error before the catch(...) in the block that calls
add_vertex/static_cast<int>(vertex_count) so the original error message is
preserved (either rethrow the caught overflow_error or throw a new exception
that includes e.what() combined with context like __PGR_PRETTY_FUNCTION__ and
the existing "INTERNAL: something went wrong when inserting a vertex" text);
keep the catch(...) afterwards for other exceptions. Target symbols: add_vertex,
m_id_to_V, m_V_to_id, and __PGR_PRETTY_FUNCTION__.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: b310a469-e9e8-47b8-ba80-d91a0b4ddad2

📥 Commits

Reviewing files that changed from the base of the PR and between e85cc23 and 80f4d0a.

📒 Files selected for processing (1)
  • src/cpp_common/undirectedHasCostBG.cpp

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.

1 participant