Skip to content

clang-tidy: fix warnings of cppcoreguidelines-init-variables#3083

Open
ayushjariyal wants to merge 7 commits intopgRouting:developfrom
ayushjariyal:fix-clag-tidy-for-init-variables
Open

clang-tidy: fix warnings of cppcoreguidelines-init-variables#3083
ayushjariyal wants to merge 7 commits intopgRouting:developfrom
ayushjariyal:fix-clag-tidy-for-init-variables

Conversation

@ayushjariyal
Copy link

@ayushjariyal ayushjariyal commented Feb 28, 2026

Fixes #3036 ,

while running run-clang-tidy -checks='-*,cppcoreguidelines-init-variables', I notice that there are still warnings in .c files.
This PR initializes the affected variables in the .c files to
resolve the remaining clang-tidy warnings.

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability and determinism across many graph algorithms and streaming query results by adding safer initializations, reducing intermittent crashes, incorrect/missing rows, and unpredictable behavior during long-running or repeated queries.

@coderabbitai
Copy link
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

Adds explicit NULL/zero initializations to many previously uninitialized local variables (FuncCallContext, TupleDesc, MemoryContext oldcontext, HeapTuple, Datum, Datum*, bool*, loop indices, SPI locals) across numerous SRF and helper C functions; no control-flow or public API changes.

Changes

Cohort / File(s) Summary
Routing & shortest-path families
src/allpairs/..., src/dijkstra/..., src/trsp/..., src/driving_distance/*, src/ksp/..., src/tsp/...
Initialize SRF context (funcctx, tuple_desc), old memory context, per-call temporaries (tuple, result, values, nulls) and loop indices to NULL/0 across v4 and legacy entry points.
Search & traversal
src/astar/astar.c, src/breadthFirstSearch/*, src/traversal/depthFirstSearch.c
Add explicit NULL/0 initialization for SRF context, per-call temporaries, and iteration counters.
Components & connectivity
src/components/..., src/contraction/..., src/transitiveClosure/...
Initialize SRF contexts and array/type-related locals (ArrayType*, typlen, typbyval, typalign), plus per-call Datum/tuple buffers to NULL/0.
Spanning tree, planar & graph utilities
src/spanningTree/*, src/chinese/..., src/circuits/*, src/lineGraph/*, src/planar/*
Defensive initialization of funcctx, tuple_desc, oldcontext, per-call variables, and loop counters in SRF implementations.
Flow, matching & mincut
src/max_flow/*, src/mincut/stoerWagner.c, src/max_flow/edge_disjoint_paths.c
Initialize SRF context pointers, per-call result buffers, typlen/arrayType where present, and loop indices to defined defaults; minor signature formatting in one file.
Ordering, metrics, utilities & SPI
src/ordering/*, src/metrics/*, src/common/postgres_connection.c, src/withPoints/*
Set SPI-related locals (SPIcode, SPIplan, SPIportal) and SRF locals to safe defaults; explicit initialization across ordering, metrics and utility SRFs.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Enhancement, C/C++

Suggested reviewers

  • cvvergara
  • robe2

Poem

🐇 I hop through lines of C so neat,
I set each pointer safe and fleet.
Nulls in place, no ghosts remain,
Stack is quiet—no undefined pain.
🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 1.30% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely summarizes the main change: fixing clang-tidy warnings for the cppcoreguidelines-init-variables check by initializing variables.
Linked Issues check ✅ Passed The PR addresses the cppcoreguidelines-init-variables check requirement from linked issue #3036 by initializing previously uninitialized variables across 63 C source files.
Out of Scope Changes check ✅ Passed All changes are scoped to initializing variables to fix cppcoreguidelines-init-variables warnings; no unrelated modifications or scope creep detected.

✏️ 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

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

@ayushjariyal
Copy link
Author

@cvvergara, Please review this PR.

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.

Caution

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

⚠️ Outside diff range comments (2)
src/coloring/edgeColoring.c (1)

128-141: ⚠️ Potential issue | 🟠 Major

Fix uninitialized third array element: reduce num from 3 to 2.

The SQL function _pgr_edgeColoring declares exactly 2 output columns (edge_id and color_id), but the C code allocates arrays for 3 elements and only initializes the first 2. The uninitialized values[2] is then passed to heap_form_tuple, causing undefined behavior.

Fix
-        size_t num  = 3;
+        size_t num  = 2;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/coloring/edgeColoring.c` around lines 128 - 141, The arrays 'values' and
'nulls' are overallocated to 3 but the SQL function `_pgr_edgeColoring` returns
only 2 columns, leaving values[2] uninitialized and passed into heap_form_tuple;
change the allocation/count variable 'num' from 3 to 2, allocate values and
nulls with num (2) entries, ensure the loop that sets nulls runs for i < num,
and only assign values[0] and values[1] before calling heap_form_tuple so no
uninitialized element is passed.
src/planar/boyerMyrvold.c (1)

78-127: ⚠️ Potential issue | 🔴 Critical

Type mismatch: NULL assigned to size_t variable.

Line 124 initializes size_t i with NULL, which is a pointer constant. This is incorrect for an integer type and may cause compiler warnings or undefined behavior.

All other files in this PR correctly use size_t i = 0;.

🐛 Proposed fix
-        size_t i = NULL;
+        size_t i = 0;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/planar/boyerMyrvold.c` around lines 78 - 127, In _pgr_boyermyrvold, the
local variable declaration "size_t i = NULL;" is a type mismatch (NULL is a
pointer constant); change the initializer to an integer zero (e.g., "size_t i =
0;") so the loop variable i has the correct type/value; update the declaration
near the loop that sets nulls[i] to false inside the SRF per-call block.
🤖 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/coloring/edgeColoring.c`:
- Around line 128-141: The arrays 'values' and 'nulls' are overallocated to 3
but the SQL function `_pgr_edgeColoring` returns only 2 columns, leaving
values[2] uninitialized and passed into heap_form_tuple; change the
allocation/count variable 'num' from 3 to 2, allocate values and nulls with num
(2) entries, ensure the loop that sets nulls runs for i < num, and only assign
values[0] and values[1] before calling heap_form_tuple so no uninitialized
element is passed.

In `@src/planar/boyerMyrvold.c`:
- Around line 78-127: In _pgr_boyermyrvold, the local variable declaration
"size_t i = NULL;" is a type mismatch (NULL is a pointer constant); change the
initializer to an integer zero (e.g., "size_t i = 0;") so the loop variable i
has the correct type/value; update the declaration near the loop that sets
nulls[i] to false inside the SRF per-call block.

ℹ️ 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 a766967.

📒 Files selected for processing (60)
  • src/allpairs/floydWarshall.c
  • src/allpairs/johnson.c
  • src/astar/astar.c
  • src/bdAstar/bdAstar.c
  • src/bdDijkstra/bdDijkstra.c
  • src/bellman_ford/bellman_ford.c
  • src/bellman_ford/bellman_ford_neg.c
  • src/bellman_ford/edwardMoore.c
  • src/breadthFirstSearch/binaryBreadthFirstSearch.c
  • src/breadthFirstSearch/breadthFirstSearch.c
  • src/chinese/chinesePostman.c
  • src/circuits/hawickCircuits.c
  • src/coloring/bipartite.c
  • src/coloring/edgeColoring.c
  • src/coloring/sequentialVertexColoring.c
  • src/common/postgres_connection.c
  • src/components/articulationPoints.c
  • src/components/biconnectedComponents.c
  • src/components/bridges.c
  • src/components/connectedComponents.c
  • src/components/makeConnected.c
  • src/components/strongComponents.c
  • src/contraction/contractGraph.c
  • src/contraction/contractionHierarchies.c
  • src/dagShortestPath/dagShortestPath.c
  • src/dijkstra/dijkstra.c
  • src/dijkstra/dijkstraVia.c
  • src/dominator/lengauerTarjanDominatorTree.c
  • src/driving_distance/driving_distance.c
  • src/driving_distance/driving_distance_withPoints.c
  • src/ksp/ksp.c
  • src/ksp/turnRestrictedPath.c
  • src/ksp/withPoints_ksp.c
  • src/lineGraph/lineGraph.c
  • src/lineGraph/lineGraphFull.c
  • src/max_flow/edge_disjoint_paths.c
  • src/max_flow/max_flow.c
  • src/max_flow/maximum_cardinality_matching.c
  • src/max_flow/minCostMaxFlow.c
  • src/metrics/betweennessCentrality.c
  • src/mincut/stoerWagner.c
  • src/ordering/cuthillMckeeOrdering.c
  • src/ordering/kingOrdering.c
  • src/ordering/sloanOrdering.c
  • src/ordering/topologicalSort.c
  • src/pickDeliver/pickDeliver.c
  • src/pickDeliver/pickDeliverEuclidean.c
  • src/planar/boyerMyrvold.c
  • src/spanningTree/kruskal.c
  • src/spanningTree/prim.c
  • src/transitiveClosure/transitiveClosure.c
  • src/traversal/depthFirstSearch.c
  • src/trsp/trsp.c
  • src/trsp/trspVia.c
  • src/trsp/trspVia_withPoints.c
  • src/trsp/trsp_withPoints.c
  • src/tsp/TSP.c
  • src/tsp/euclideanTSP.c
  • src/withPoints/withPoints.c
  • src/withPoints/withPointsVia.c

Copy link
Member

@cvvergara cvvergara left a comment

Choose a reason for hiding this comment

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

I dont see your actions running

@ayushjariyal ayushjariyal requested a review from cvvergara March 2, 2026 05:45
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/max_flow/max_flow.c`:
- Around line 39-42: Remove the duplicated macro invocation by keeping a single
PG_FUNCTION_INFO_V1 for the _pgr_maxflow function: locate both occurrences of
PG_FUNCTION_INFO_V1(_pgr_maxflow) and delete one so only one
PG_FUNCTION_INFO_V1(_pgr_maxflow) remains in the file (ensuring the remaining
macro stays immediately before or near the corresponding _pgr_maxflow function
definition).

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f584725 and bd52055.

📒 Files selected for processing (1)
  • src/max_flow/max_flow.c

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/max_flow/max_flow.c`:
- Around line 113-116: The loop variable i is declared outside but
re-initialized in the for header; change the declaration to scope i within the
loop by moving `size_t i` into the for statement (for example: `for (size_t i =
0; i < 6; ++i)`), updating the code that sets `nulls[i] = false` accordingly;
this reduces scope and follows cppcoreguidelines-init-variables while leaving
the `nulls` array and its usage unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9e2b019a-3ebf-4d9a-9068-e31c94ee3d7d

📥 Commits

Reviewing files that changed from the base of the PR and between bd52055 and ff97ab8.

📒 Files selected for processing (1)
  • src/max_flow/max_flow.c

ayushjariyal and others added 2 commits March 4, 2026 19:31
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@ayushjariyal
Copy link
Author

@cvvergara , Could you please review this? I have updated the code according to the suggestions from CodeRabbit, and the GitHub Actions are running successfully on my fork.

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.

Clang tidy checks that need fixes

2 participants