Skip to content

Potential fix for code scanning alert no. 4337: Invalid string formatting#6

Draft
ChristianRosewich wants to merge 4 commits into
masterfrom
alert-autofix
Draft

Potential fix for code scanning alert no. 4337: Invalid string formatting#6
ChristianRosewich wants to merge 4 commits into
masterfrom
alert-autofix

Conversation

@ChristianRosewich

Copy link
Copy Markdown
Owner

Potential fix for https://github.com/ChristianRosewich/CSharp/security/code-scanning/4337

Use string.Format only when the selected template actually contains a format item, and write the template directly otherwise.

Best single fix in CSharpBible/SomeThing/Statistic4/Program.cs (inside Run, in the if (fOut) block around lines 133–136):

  • Keep bounds check and tmpl retrieval.
  • Replace the unconditional string.Format(..., tmpl, lastObj) calls with:
    • tmpl directly when no {0} placeholder is present.
    • formatted string with lastObj when {0} is present.
  • Reuse one computed output string for both Console.Write and Debug.Write.

This addresses all listed variants because templates without placeholders no longer receive an unused argument.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

ChristianRosewich and others added 3 commits June 17, 2026 12:25
…ting

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Console.Write(string.Format(CultureInfo.InvariantCulture, tmpl, lastObj));
System.Diagnostics.Debug.Write(string.Format(CultureInfo.InvariantCulture, tmpl, lastObj));
string output = tmpl.Contains("{0}", StringComparison.Ordinal)
? string.Format(CultureInfo.InvariantCulture, tmpl, lastObj)
Potential fix for code scanning alert no. 1: Complex condition
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