Skip to content

Packages: fix -Wstringop-overflow warning#2361

Open
fam007e wants to merge 1 commit into
fastfetch-cli:devfrom
fam007e:fix-packages-warning
Open

Packages: fix -Wstringop-overflow warning#2361
fam007e wants to merge 1 commit into
fastfetch-cli:devfrom
fam007e:fix-packages-warning

Conversation

@fam007e
Copy link
Copy Markdown
Contributor

@fam007e fam007e commented Jun 1, 2026

Summary

Fix -Wstringop-overflow compiler warning in src/modules/packages/packages.c by replacing direct buffer index assignment with safe FFstrbuf API calls.

Note on output behavior: The original code removed the trailing , via two operations — ffStrbufSubstrBefore(&output, output.length - 1) truncating the space, then a direct char write overwriting the comma with \n. The new code achieves the exact same result via ffStrbufSubstrBefore(&output, output.length - 2) + ffStrbufAppendC. Output is byte-for-byte identical. No behavioral change.

Related issue (required for new logos for new distros)

N/A

Changes

  • Replaced manual index assignment output.chars[output.length - 1] = '\n' with ffStrbufSubstrBefore + ffStrbufAppendC
  • Preserves original assert(output.length >= 2) invariant
  • Output format unchanged: trailing , is removed, newline appended
  • GCC's static analysis could not prove buffer non-empty after ffStrbufSubstrBefore, causing a false positive; the API-based rewrite resolves this

Screenshots

N/A

Checklist

  • I have tested my changes locally.

@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

Copy link
Copy Markdown

@codacy-production codacy-production Bot 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

The pull request successfully addresses the -Wstringop-overflow compiler warning by migrating to the FFstrbuf API. The build quality is currently up to standards according to Codacy analysis, and no critical security flaws or major logic bugs were identified.

There is a minor discrepancy between the implementation and the PR description regarding output formatting. The new logic removes the trailing comma from the package manager list, whereas the previous code (and description) implied behavior would remain identical. This change in the final string format should be confirmed as intentional before merging.

About this PR

  • The PR lacks accompanying unit or integration tests to verify that the string manipulation logic correctly formats the output across different package manager configurations. Without a coverage report or tests, it is difficult to verify that this specific execution path is exercised safely.

Test suggestions

  • Verify that when package counts are printed, the resulting string ends with a newline and no trailing comma/space.
  • Verify that the program does not crash/assert when only one package manager is reported (minimum length requirement).
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify that when package counts are printed, the resulting string ends with a newline and no trailing comma/space.
2. Verify that the program does not crash/assert when only one package manager is reported (minimum length requirement).

TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback

assert(output.length >= 2); // counts.all > 0 guarantees that at least one package count was printed, which guarantees that ", " was appended at least once
ffStrbufSubstrBefore(&output, output.length - 1);
output.chars[output.length - 1] = '\n';
ffStrbufSubstrBefore(&output, output.length - 2);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚪ LOW RISK

Suggestion: The implementation changes the output from 'package,\n' to 'package\n' (removing the comma). While this is likely an improvement, it contradicts the 'identical behavior' claim in the PR description. Ensure this change in output format is intended for the packages module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a bot misread. The original code performed two operations:

  1. ffStrbufSubstrBefore(&output, output.length - 1) — truncates, removing the trailing space
  2. output.chars[output.length - 1] = '\n' — overwrites what is now the last char (the ,) with \n

Both the comma and space were removed; output never ended with a comma. The new code produces byte-for-byte identical output via ffStrbufSubstrBefore(&output, output.length - 2) + ffStrbufAppendC(&output, '\n').

@CarterLi
Copy link
Copy Markdown
Member

CarterLi commented Jun 1, 2026

Isn't the correct way to fix a warning #pragma GCC diagnostic ignored "-Wshut-up" ?

@fam007e
Copy link
Copy Markdown
Contributor Author

fam007e commented Jun 1, 2026

Isn't the correct way to fix a warning #pragma GCC diagnostic ignored "-Wshut-up" ?

Ha, that would work too, but fixing the actual cause seemed cleaner than teaching GCC to look the other way. 🤷🏽‍♂️

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