Skip to content

Potential fix for code scanning alert no. 87: String concatenation in loop #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vb2ae
Copy link
Member

@vb2ae vb2ae commented Aug 9, 2025

Potential fix for https://github.com/Caliburn-Micro/Caliburn.Micro/security/code-scanning/87

To fix the problem, replace the string concatenation in the loop with a StringBuilder. Specifically, in the region where message is built up by appending parameter names and commas, instantiate a StringBuilder, append the method name, and then append each parameter name and comma inside the loop. After the loop, remove the trailing comma and append the closing parenthesis. Finally, convert the StringBuilder to a string and assign it to message. This change should be made in the block starting at line 179, where message is constructed. You will need to add using System.Text; at the top of the file if it is not already present.

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

… loop

use string builder to combine strings in a loop

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@vb2ae vb2ae requested a review from Copilot August 9, 2025 01:43
Copy link
Contributor

@Copilot Copilot AI 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

This PR addresses a code scanning alert by replacing inefficient string concatenation in a loop with a StringBuilder. The change optimizes the performance of message construction when building method signatures for logging purposes.

Key Changes

  • Replaces string concatenation with StringBuilder for building method signatures
  • Adds proper using System.Text; import statement
  • Improves performance by avoiding repeated string allocations in loops

Comment on lines 200 to 202
var message = messageBuilder.ToString();

}
Copy link
Preview

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

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

The variable message is declared inside the if block but used outside of it at line 204. This will cause a compilation error because message is not in scope when parameters.Length is 0. Move the declaration outside the if block or ensure it's accessible in all code paths.

Suggested change
var message = messageBuilder.ToString();
}
}
message = messageBuilder.ToString();

Copilot uses AI. Check for mistakes.

Comment on lines +197 to +198
if (parameters.Length > 0)
messageBuilder.Length -= 1;
Copy link
Preview

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

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

This condition check is redundant since this code is already inside a block that only executes when parameters.Length > 0 (line 183). The condition will always be true and can be removed.

Suggested change
if (parameters.Length > 0)
messageBuilder.Length -= 1;
messageBuilder.Length -= 1;

Copilot uses AI. Check for mistakes.

Added line breaks and formatting adjustments to enhance clarity.
Reformatted comments and logging statements for consistency.
No functional changes were made; the code is now more maintainable.
var vmType = viewModel.GetType();
if (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension") {
if (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension")

Check warning

Code scanning / CodeQL

Erroneous class compare Warning

Erroneous class compare.

Copilot Autofix

AI 1 day ago

To fix the problem, replace the string-based type comparison (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension") with a direct type comparison using == typeof(...). This ensures that the check is robust and not vulnerable to spoofing by types with the same name in different assemblies. The change should be made only in the relevant region (line 249) of src/Caliburn.Micro.Platform/ViewModelBinder.cs. No new imports are needed, as typeof is a built-in C# operator.


Suggested changeset 1
src/Caliburn.Micro.Platform/ViewModelBinder.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Caliburn.Micro.Platform/ViewModelBinder.cs b/src/Caliburn.Micro.Platform/ViewModelBinder.cs
--- a/src/Caliburn.Micro.Platform/ViewModelBinder.cs
+++ b/src/Caliburn.Micro.Platform/ViewModelBinder.cs
@@ -246,7 +246,7 @@
             if (View.InDesignMode)
             {
                 var vmType = viewModel.GetType();
-                if (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension")
+                if (vmType == Type.GetType("Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension", false))
                 {
                     var propInfo = vmType.GetProperty("Instance", BindingFlags.Instance | BindingFlags.NonPublic);
                     viewModel = propInfo.GetValue(viewModel, null);
EOF
@@ -246,7 +246,7 @@
if (View.InDesignMode)
{
var vmType = viewModel.GetType();
if (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension")
if (vmType == Type.GetType("Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension", false))
{
var propInfo = vmType.GetProperty("Instance", BindingFlags.Instance | BindingFlags.NonPublic);
viewModel = propInfo.GetValue(viewModel, null);
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
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