Skip to content
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

Tests for safety of string format calls #4194

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

HebaruSan
Copy link
Member

@HebaruSan HebaruSan commented Sep 25, 2024

Motivation

My first code fix for CKAN was to remove a superfluous String.Format call that was causing exceptions to be thrown because externally-generated strings were being used as part of a format string (see #2111).

This problem generalizes; any call to IUser.RaiseMessage or IUser.RaiseError can throw an exception if the first parameter isn't a valid format string. We can ensure that a string literal is safe by looking at it, and we can ensure that an i18n resource is safe by inspecting our resx files, but anytime a value is externally sourced, it isn't safe to use it as a format string because curly braces could turn up any time and ruin our day.

In principle, the C# compiler could check this and emit warnings similar to what it does for nullable references, but currently it doesn't. The closest thing is the StringSyntax attribute, which can be used to mark a parameter as a format string, but which doesn't have any built-in functionality associated with it.

Changes

  • Now a new MonoCecilAnalysisTestsBase base class is factored out of the test from Add test to check GUI thread safety #3914
  • Now a new StringFormatSafetyTests class inherits from MonoCecilAnalysisTestsBase and scans all code in all of our assemblies for calls to functions with the StringSyntax attribute. These calls are deemed safe if the argument list is non-empty, if the format string is a string literal, or if the format string is from Properties.Resources; otherwise a test failure is logged.
  • Now our format string parameters are tagged with the StringSyntax attribute
  • Now the 67 existing places that fail the new test are updated to use a format string of "{0}", or to remove a superfluous call to string.Format, or other such solutions

This guard rail will help us to write safer code.

@HebaruSan HebaruSan added Bug Something is not working as intended Enhancement New features or functionality Tests Issues affecting the internal tests labels Sep 25, 2024
Copy link
Member

@techman83 techman83 left a comment

Choose a reason for hiding this comment

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

Safer code and tests! 😍

@HebaruSan HebaruSan merged commit 228c887 into KSP-CKAN:master Sep 25, 2024
3 checks passed
@HebaruSan HebaruSan deleted the feature/str-fmt-tests branch September 25, 2024 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something is not working as intended Enhancement New features or functionality Tests Issues affecting the internal tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants