Skip to content

clang-tidy: check and fix cppcoreguidelines-avoid-c-arrays#3084

Closed
Cellzawy wants to merge 2 commits into
pgRouting:developfrom
Cellzawy:cppcoreguidelines-avoid-c-arrays
Closed

clang-tidy: check and fix cppcoreguidelines-avoid-c-arrays#3084
Cellzawy wants to merge 2 commits into
pgRouting:developfrom
Cellzawy:cppcoreguidelines-avoid-c-arrays

Conversation

@Cellzawy
Copy link
Copy Markdown

@Cellzawy Cellzawy commented Feb 28, 2026

This PR adds cppcoreguidelines-avoid-c-arrays check in clang-tidy (by removing it from the disabled list).

Summary by CodeRabbit

  • Chores
    • Enabled an additional static analysis check to enforce safer C++ practices.
  • Bug Fixes
    • Improved memory-safety and backtrace/debugging reliability to reduce crash risk and provide clearer diagnostics.
  • Style
    • Internal formatting adjustments for code consistency and maintainability.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 28, 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

Enabled the cppcoreguidelines-avoid-c-arrays clang-tidy check and replaced a C-style backtrace buffer with std::array<void*, 16>, updating calls to backtrace/backtrace_symbols to use .data() and adding null-check handling for symbol resolution.

Changes

Cohort / File(s) Summary
Clang-tidy configuration
./.clang-tidy
Removed the negation prefix from -cppcoreguidelines-avoid-c-arrays, enabling that check.
Assert implementation
src/common/assert.cpp
Replaced void* trace[16] with std::array<void*, 16>, added #include <array> (where applicable), passed trace.data() to backtrace/backtrace_symbols, and added null-check handling when reading symbol names.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

Enhancement, C/C++

Suggested reviewers

  • cvvergara

Poem

🐰
I swapped old pointers for a tidy array bright,
Traces hop steady through the debugging night.
Clang whispered guidance, I tightened each seam,
Hopping through code, chasing the dream. 🥕

🚥 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: enabling the cppcoreguidelines-avoid-c-arrays check and applying fixes to conform.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
Copy Markdown
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 @.clang-tidy:
- Around line 9-10: Remove the redundant explicit rule entry by deleting the
"cppcoreguidelines-avoid-c-arrays" line since "cppcoreguidelines-*" already
enables that rule; locate the block containing "cppcoreguidelines-*" and remove
the separate "cppcoreguidelines-avoid-c-arrays" entry so only the wildcard
remains.

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4912a28 and 7099d52.

📒 Files selected for processing (1)
  • .clang-tidy

Comment thread .clang-tidy Outdated
@Cellzawy Cellzawy force-pushed the cppcoreguidelines-avoid-c-arrays branch 2 times, most recently from 211bbe4 to efea7a1 Compare February 28, 2026 15:39
@Cellzawy Cellzawy changed the title clang-tidy: add cppcoreguidelines-avoid-c-arrays clang-tidy: check and fix cppcoreguidelines-avoid-c-arrays Feb 28, 2026
Copy link
Copy Markdown
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.

Caution

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

⚠️ Outside diff range comments (1)
src/common/assert.cpp (1)

49-55: ⚠️ Potential issue | 🟠 Major

Guard backtrace_symbols failure before dereference.

On line 49, backtrace_symbols() can return nullptr on allocation failure. Line 54 unconditionally dereferences funcNames[i], which crashes inside exception handling.

Suggested fix
-        trace_size = backtrace(trace.data(), 16);
+        trace_size = backtrace(trace.data(), static_cast<int>(trace.size()));
         char** funcNames = backtrace_symbols(trace.data(), trace_size);
+        if (funcNames == nullptr) {
+            return "\n*** Execution path***\n[bt] backtrace_symbols unavailable\n";
+        }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/common/assert.cpp` around lines 49 - 55, The call to backtrace_symbols
may return nullptr and the code unconditionally dereferences funcNames[i];
update the block around backtrace_symbols(trace.data(), trace_size) and the loop
that builds message to check if funcNames is non-null before accessing
funcNames[i], and if it is null append a safe fallback (e.g., a placeholder line
per frame or a single error note) instead of dereferencing; ensure funcNames is
freed only if non-null and keep trace_size handling in the loop (symbols from
backtrace_symbols, funcNames, trace_size, and message).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/common/assert.cpp`:
- Around line 49-55: The call to backtrace_symbols may return nullptr and the
code unconditionally dereferences funcNames[i]; update the block around
backtrace_symbols(trace.data(), trace_size) and the loop that builds message to
check if funcNames is non-null before accessing funcNames[i], and if it is null
append a safe fallback (e.g., a placeholder line per frame or a single error
note) instead of dereferencing; ensure funcNames is freed only if non-null and
keep trace_size handling in the loop (symbols from backtrace_symbols, funcNames,
trace_size, and message).

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3bc74b3 and efea7a1.

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

@Cellzawy Cellzawy force-pushed the cppcoreguidelines-avoid-c-arrays branch from efea7a1 to 7726a3f Compare February 28, 2026 15:57
Copy link
Copy Markdown
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/common/assert.cpp`:
- Line 81: The AssertFailedException constructor currently copies the parameter
msg into member str; change it to move msg into str by adding `#include` <utility>
and using std::move(msg) in the initializer list of
AssertFailedException::AssertFailedException(std::string msg) so the value is
moved into the member (refer to AssertFailedException, its constructor, and
member str).

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between efea7a1 and 7726a3f.

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

Comment thread src/common/assert.cpp
@Cellzawy Cellzawy force-pushed the cppcoreguidelines-avoid-c-arrays branch from 7726a3f to 95d0119 Compare February 28, 2026 16:22
Copy link
Copy Markdown
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/common/assert.cpp`:
- Around line 45-49: Replace the hardcoded literal 16 in the backtrace call with
the std::array's size to avoid duplication: call backtrace(trace.data(),
trace.size()) and keep using trace_size for the returned count
(backtrace_symbols(trace.data(), trace_size)); update any related
comments/assumptions around trace and trace_size if needed so the code relies on
trace.size() rather than the magic number.

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7726a3f and 95d0119.

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

Comment thread src/common/assert.cpp
Comment on lines +45 to +49
std::array<void *, 16> trace;
int trace_size = 0;

trace_size = backtrace(trace.data(), 16);
char **funcNames = backtrace_symbols(trace.data(), trace_size);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Good conversion from C-array to std::array; consider using trace.size() to avoid duplication.

The conversion to std::array<void*, 16> correctly addresses the cppcoreguidelines-avoid-c-arrays check. Consider using trace.size() instead of hardcoding 16 again in the backtrace call to avoid duplication and ensure consistency if the size changes.

♻️ Proposed fix
-    trace_size = backtrace(trace.data(), 16);
-    char **funcNames = backtrace_symbols(trace.data(), trace_size);
+    trace_size = backtrace(trace.data(), trace.size());
+    char **funcNames = backtrace_symbols(trace.data(), trace_size);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
std::array<void *, 16> trace;
int trace_size = 0;
trace_size = backtrace(trace.data(), 16);
char **funcNames = backtrace_symbols(trace.data(), trace_size);
std::array<void *, 16> trace;
int trace_size = 0;
trace_size = backtrace(trace.data(), trace.size());
char **funcNames = backtrace_symbols(trace.data(), trace_size);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/common/assert.cpp` around lines 45 - 49, Replace the hardcoded literal 16
in the backtrace call with the std::array's size to avoid duplication: call
backtrace(trace.data(), trace.size()) and keep using trace_size for the returned
count (backtrace_symbols(trace.data(), trace_size)); update any related
comments/assumptions around trace and trace_size if needed so the code relies on
trace.size() rather than the magic number.

@Cellzawy Cellzawy force-pushed the cppcoreguidelines-avoid-c-arrays branch from 95d0119 to 30f3c84 Compare February 28, 2026 16:41
@Cellzawy
Copy link
Copy Markdown
Author

Fixed warnings raised by checks

In src/common/assert.cpp:

  • Replaced the C-style array:
        void *trace[16];
    with a std::array:
        std::array<void *, 16> trace;
  • Updated the backtrace() call to use trace.size() instead of the hardcoded 16.

@cvvergara cvvergara added this to the Release 4.1.0 milestone Mar 1, 2026
@cvvergara cvvergara removed this from the Release 4.1.0 milestone Mar 1, 2026
@cvvergara
Copy link
Copy Markdown
Member

Your solution was nice, unfortunately a previous PR also solved this problem.
Dont worry if it was not merged, for GSoC application this counts.

@cvvergara cvvergara closed this Mar 1, 2026
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