Skip to content

Commit

Permalink
codeql issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vb2ae committed Nov 8, 2024
1 parent 01dd0c0 commit 395dc3d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 62 deletions.
33 changes: 4 additions & 29 deletions src/Caliburn.Micro.Avalonia.Tests/ViewModelLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewModels
{
DefaultSubNamespaceForViews = "not empty",
DefaultSubNamespaceForViewModels = string.Empty,
NameFormat = "not Empty"
NameFormat = "{0}{1}not Empty"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
Expand All @@ -22,7 +22,7 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewModels
{
DefaultSubNamespaceForViews = "not null",
DefaultSubNamespaceForViewModels = null,
NameFormat = "not null"
NameFormat = "{1}{0}not null"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
Expand All @@ -35,7 +35,7 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewsIsEmp
{
DefaultSubNamespaceForViews = string.Empty,
DefaultSubNamespaceForViewModels = "not Empty",
NameFormat = "not Empty"
NameFormat = "{0}{1}not Empty"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
Expand All @@ -48,37 +48,12 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewsIsNul
{
DefaultSubNamespaceForViews = null,
DefaultSubNamespaceForViewModels = "not null",
NameFormat = "not null"
NameFormat = "{0}{1}not null"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
}

[Fact]
public void ConfigureTypeMappingsShouldThrowWhenNameFormatIsEmpty()
{
var config = new TypeMappingConfiguration
{
DefaultSubNamespaceForViews = "not Empty",
DefaultSubNamespaceForViewModels = "not Empty",
NameFormat = string.Empty
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
}

[Fact]
public void ConfigureTypeMappingsShouldThrowWhenNameFormatIsNull()
{
var config = new TypeMappingConfiguration
{
DefaultSubNamespaceForViews = "not null",
DefaultSubNamespaceForViewModels = "not null",
NameFormat = null
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
}

[Fact]
public void COnfigureTypeMappingsWithDefaultValuesShouldNotThrow()
Expand Down
33 changes: 4 additions & 29 deletions src/Caliburn.Micro.Platform.Tests/ViewModelLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewModels
{
DefaultSubNamespaceForViews = "not empty",
DefaultSubNamespaceForViewModels = string.Empty,
NameFormat = "not Empty"
NameFormat = "not Empty{1}{0}"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
Expand All @@ -25,7 +25,7 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewModels
{
DefaultSubNamespaceForViews = "not null",
DefaultSubNamespaceForViewModels = null,
NameFormat = "not null"
NameFormat = "not null{1}{0}"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
Expand All @@ -38,7 +38,7 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewsIsEmp
{
DefaultSubNamespaceForViews = string.Empty,
DefaultSubNamespaceForViewModels = "not Empty",
NameFormat = "not Empty"
NameFormat = "{0}{1}Empty"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
Expand All @@ -51,37 +51,12 @@ public void ConfigureTypeMappingsShouldThrowWhenDefaultSubNamespaceForViewsIsNul
{
DefaultSubNamespaceForViews = null,
DefaultSubNamespaceForViewModels = "not null",
NameFormat = "not null"
NameFormat = "{1}not {0}null"
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
}

[Fact]
public void ConfigureTypeMappingsShouldThrowWhenNameFormatIsEmpty()
{
var config = new TypeMappingConfiguration
{
DefaultSubNamespaceForViews = "not Empty",
DefaultSubNamespaceForViewModels = "not Empty",
NameFormat = string.Empty
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
}

[Fact]
public void ConfigureTypeMappingsShouldThrowWhenNameFormatIsNull()
{
var config = new TypeMappingConfiguration
{
DefaultSubNamespaceForViews = "not null",
DefaultSubNamespaceForViewModels = "not null",
NameFormat = null
};

Assert.Throws<ArgumentException>(() => ViewModelLocator.ConfigureTypeMappings(config));
}

[Fact]
public void COnfigureTypeMappingsWithDefaultValuesShouldNotThrow()
Expand Down
47 changes: 43 additions & 4 deletions src/Caliburn.Micro.Platform/MessageBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Caliburn.Micro
/// </summary>
public static class MessageBinder
{
static readonly ILog Log = LogManager.GetLog(typeof(MessageBinder));

/// <summary>
/// The special parameter values recognized by the message binder along with their resolvers.
/// Parameter names are case insensitive so the specified names are unique and can be used with different case variations
Expand Down Expand Up @@ -122,7 +124,6 @@ public static object CoerceValue(Type destinationType, object providedValue, obj
{
return CustomConverters[destinationType](providedValue, context);
}

try
{
#if !WINDOWS_UWP && !XFORMS && !MAUI
Expand All @@ -141,7 +142,7 @@ public static object CoerceValue(Type destinationType, object providedValue, obj
}
#endif
#if WINDOWS_UWP || XFORMS || MAUI
if (destinationType.GetTypeInfo().IsEnum) {
if (destinationType.GetTypeInfo().IsEnum) {
#else
if (destinationType.IsEnum)
{
Expand All @@ -164,19 +165,57 @@ public static object CoerceValue(Type destinationType, object providedValue, obj
}
}
}
catch
catch (InvalidCastException ex)
{
Log.Error(ex);
return GetDefaultValue(destinationType);
}
catch (FormatException ex)
{
Log.Error(ex);
return GetDefaultValue(destinationType);
}
catch (OverflowException ex)
{
Log.Error(ex);
return GetDefaultValue(destinationType);
}
catch (Exception ex)
{
Log.Error(ex);
return GetDefaultValue(destinationType);
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.


try
{
return Convert.ChangeType(providedValue, destinationType, CultureInfo.CurrentCulture);
}
catch
catch (InvalidCastException ex)
{
// Log the exception details if necessary
Log.Error(ex);
return GetDefaultValue(destinationType);
}
catch (FormatException ex)
{
// Log the exception details if necessary
Log.Error(ex);
return GetDefaultValue(destinationType);
}
catch (OverflowException ex)
{
// Log the exception details if necessary
Log.Error(ex);
return GetDefaultValue(destinationType);
}
catch (Exception ex)
{
// Log the exception details if necessary
Log.Error(ex);
return GetDefaultValue(destinationType);
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.

}

/// <summary>
Expand Down

0 comments on commit 395dc3d

Please sign in to comment.