Skip to content

Commit bb09e54

Browse files
Fix CodeQL warnings and suggestions
1 parent d71f42d commit bb09e54

File tree

26 files changed

+159
-208
lines changed

26 files changed

+159
-208
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersion.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public partial class ApiVersion : IEquatable<ApiVersion>, IComparable<ApiVersion
1818

1919
private ApiVersion()
2020
{
21-
const int MajorVersion = int.MaxValue;
22-
const int MinorVersion = int.MaxValue;
23-
var groupVersion = DateOnly.MaxValue;
24-
hashCode = HashCode.Combine( groupVersion, MajorVersion, MinorVersion );
21+
const int Major = int.MaxValue;
22+
const int Minor = int.MaxValue;
23+
var group = DateOnly.MaxValue;
24+
hashCode = HashCode.Combine( group, Major, Minor );
2525
}
2626

2727
/// <summary>

src/Abstractions/src/Asp.Versioning.Abstractions/ApiVersionFormatProvider.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,9 @@ private static string GetDefaultFormat( string? format, object? arg, IFormatProv
412412
return format ?? string.Empty;
413413
}
414414

415-
if ( !string.IsNullOrEmpty( format ) )
415+
if ( !string.IsNullOrEmpty( format ) && arg is IFormattable formattable )
416416
{
417-
if ( arg is IFormattable formattable )
418-
{
419-
return formattable.ToString( format, formatProvider );
420-
}
417+
return formattable.ToString( format, formatProvider );
421418
}
422419

423420
return arg.ToString() ?? string.Empty;
@@ -488,17 +485,12 @@ private static void SplitFormatSpecifierWithNumber(
488485
}
489486
}
490487

491-
if ( end > start )
492-
{
493-
count = int.Parse(
488+
count = end > start
489+
? int.Parse(
494490
Str.StringOrSpan( Str.Slice( format, start, end ) ),
495491
default,
496-
formatProvider );
497-
}
498-
else
499-
{
500-
count = 2;
501-
}
492+
formatProvider )
493+
: 2;
502494
}
503495

504496
private static void AppendStatus( StringBuilder text, string? status )

src/Abstractions/src/Asp.Versioning.Abstractions/FormatTokenizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private static void ConsumeCustomFormat(
170170

171171
var start = i;
172172
#if NETSTANDARD1_0
173-
var ch = format[i];
173+
char ch;
174174
var previous = format[i];
175175
#else
176176
ref readonly var ch = ref format[i];

src/Abstractions/src/Asp.Versioning.Abstractions/NamespaceParser.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,12 @@ protected virtual bool TryParse( Text identifier, out ApiVersion? apiVersion )
190190
return false;
191191
}
192192

193-
if ( group is null && identifier.Length >= 10 )
193+
if ( group is null &&
194+
identifier.Length >= 10 &&
195+
!TryConsumeGroup( ref identifier, ReadableDateFormat, length: 10, out group ) )
194196
{
195-
if ( !TryConsumeGroup( ref identifier, ReadableDateFormat, length: 10, out group ) )
196-
{
197-
apiVersion = default;
198-
return false;
199-
}
197+
apiVersion = default;
198+
return false;
200199
}
201200
}
202201

@@ -255,11 +254,6 @@ private static bool IsDateLike( Text value )
255254
{
256255
for ( var i = 0; i < 8; i++ )
257256
{
258-
#if NETSTANDARD
259-
var ch = value[i];
260-
#else
261-
ref readonly var ch = ref value[i];
262-
#endif
263257
if ( !char.IsDigit( value[i] ) )
264258
{
265259
return false;

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/Routing/ODataRouteBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ static int IndexOfToken( StringBuilder builder, string token )
479479

480480
var matched = true;
481481

482-
for ( var j = 0; j < token.Length; i++, j++ )
482+
for ( var j = 0; i < builder.Length && j < token.Length; i++, j++ )
483483
{
484484
if ( builder[i] != token[j] )
485485
{
@@ -493,7 +493,7 @@ static int IndexOfToken( StringBuilder builder, string token )
493493
break;
494494
}
495495

496-
while ( builder[i] != '}' )
496+
while ( i < builder.Length && builder[i] != '}' )
497497
{
498498
++i;
499499
}

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/System.Web.Http/HttpConfigurationExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ namespace System.Web.Http;
1515
/// </summary>
1616
public static class HttpConfigurationExtensions
1717
{
18-
private const string RootContainerMappingsKey = "Microsoft.AspNet.OData.RootContainerMappingsKey";
19-
private const string NonODataRootContainerKey = "Microsoft.AspNet.OData.NonODataRootContainerKey";
20-
private const string UrlKeyDelimiterKey = "Microsoft.AspNet.OData.UrlKeyDelimiterKey";
21-
2218
/// <summary>
2319
/// Adds or replaces the configured <see cref="IApiExplorer">API explorer</see> with an implementation that supports OData and API versioning.
2420
/// </summary>
@@ -71,6 +67,8 @@ private static ODataApiExplorer AddODataApiExplorer( this HttpConfiguration conf
7167

7268
internal static IServiceProvider GetODataRootContainer( this HttpConfiguration configuration, IHttpRoute route )
7369
{
70+
const string RootContainerMappingsKey = "Microsoft.AspNet.OData.RootContainerMappingsKey";
71+
const string NonODataRootContainerKey = "Microsoft.AspNet.OData.NonODataRootContainerKey";
7472
var properties = configuration.Properties;
7573
var containers = (ConcurrentDictionary<string, IServiceProvider>) properties.GetOrAdd( RootContainerMappingsKey, key => new ConcurrentDictionary<string, IServiceProvider>() );
7674
var routeName = configuration.Routes.GetRouteName( route );
@@ -92,6 +90,8 @@ internal static IServiceProvider GetODataRootContainer( this HttpConfiguration c
9290

9391
internal static ODataUrlKeyDelimiter? GetUrlKeyDelimiter( this HttpConfiguration configuration )
9492
{
93+
const string UrlKeyDelimiterKey = "Microsoft.AspNet.OData.UrlKeyDelimiterKey";
94+
9595
if ( configuration.Properties.TryGetValue( UrlKeyDelimiterKey, out var value ) )
9696
{
9797
return value as ODataUrlKeyDelimiter;

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/ApiExplorer/VersionedApiExplorer.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class VersionedApiExplorer : IApiExplorer
3030
private static readonly Regex actionVariableRegex = new( $"{{{RouteValueKeys.Action}}}", Compiled | IgnoreCase | CultureInvariant );
3131
private static readonly Regex controllerVariableRegex = new( $"{{{RouteValueKeys.Controller}}}", Compiled | IgnoreCase | CultureInvariant );
3232
private readonly ApiExplorerOptions options;
33-
private readonly Lazy<ApiDescriptionGroupCollection> apiDescriptions;
33+
private readonly Lazy<ApiDescriptionGroupCollection> apiDescriptionsHolder;
3434
private IDocumentationProvider? documentationProvider;
3535
private ISunsetPolicyManager? sunsetPolicyManager;
3636

@@ -50,14 +50,14 @@ public VersionedApiExplorer( HttpConfiguration configuration, ApiExplorerOptions
5050
{
5151
Configuration = configuration;
5252
this.options = options;
53-
apiDescriptions = new( Initialize );
53+
apiDescriptionsHolder = new( Initialize );
5454
}
5555

5656
/// <summary>
5757
/// Gets a collection of descriptions grouped by API version.
5858
/// </summary>
5959
/// <value>An <see cref="ApiDescriptionGroupCollection">API description group collection</see>.</value>
60-
public virtual ApiDescriptionGroupCollection ApiDescriptions => apiDescriptions.Value;
60+
public virtual ApiDescriptionGroupCollection ApiDescriptions => apiDescriptionsHolder.Value;
6161

6262
/// <summary>
6363
/// Gets or sets the documentation provider. The provider will be responsible for documenting the API.
@@ -733,17 +733,15 @@ protected virtual Collection<VersionedApiDescription> ExploreRouteControllers(
733733
}
734734
}
735735
}
736-
else if ( route.Defaults.TryGetValue( RouteValueKeys.Controller, out controllerVariableValue ) )
736+
else if ( route.Defaults.TryGetValue( RouteValueKeys.Controller, out controllerVariableValue ) &&
737+
controllerMappings.TryGetValue( controllerVariableValue, out var controllerDescriptor ) )
737738
{
738739
// bound controller variable {controller = "controllerName"}
739-
if ( controllerMappings.TryGetValue( controllerVariableValue, out var controllerDescriptor ) )
740+
foreach ( var nestedControllerDescriptor in controllerDescriptor.AsEnumerable() )
740741
{
741-
foreach ( var nestedControllerDescriptor in controllerDescriptor.AsEnumerable() )
742+
if ( ShouldExploreController( controllerVariableValue, nestedControllerDescriptor, route, apiVersion ) )
742743
{
743-
if ( ShouldExploreController( controllerVariableValue, nestedControllerDescriptor, route, apiVersion ) )
744-
{
745-
ExploreRouteActions( route, routeTemplate, nestedControllerDescriptor, apiDescriptions, apiVersion );
746-
}
744+
ExploreRouteActions( route, routeTemplate, nestedControllerDescriptor, apiDescriptions, apiVersion );
747745
}
748746
}
749747
}

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/Routing/ParsedRouteAdapter{T}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ internal sealed class ParsedRouteAdapter<T> : IParsedRoute where T : notnull
1313
private static readonly Lazy<Func<T, IEnumerable<object>>> pathSegmentsAccessor = new( NewPathSegmentsAccessor );
1414
private static readonly Lazy<Func<T, IDictionary<string, object>?, IDictionary<string, object>, HttpRouteValueDictionary, HttpRouteValueDictionary, object>> bindFunc = new( NewBindFunc );
1515
private readonly T adapted;
16-
private readonly Lazy<IReadOnlyList<IPathSegment>> pathSegments;
16+
private readonly Lazy<IReadOnlyList<IPathSegment>> pathSegmentsHolder;
1717

1818
public ParsedRouteAdapter( T adapted )
1919
{
2020
this.adapted = adapted;
21-
pathSegments = new Lazy<IReadOnlyList<IPathSegment>>( AdaptToPathSegments );
21+
pathSegmentsHolder = new Lazy<IReadOnlyList<IPathSegment>>( AdaptToPathSegments );
2222
}
2323

2424
public IBoundRouteTemplate? Bind( IDictionary<string, object>? currentValues, IDictionary<string, object> values, HttpRouteValueDictionary defaultValues, HttpRouteValueDictionary constraints )
@@ -36,7 +36,7 @@ public ParsedRouteAdapter( T adapted )
3636
return adapter;
3737
}
3838

39-
public IReadOnlyList<IPathSegment> PathSegments => pathSegments.Value;
39+
public IReadOnlyList<IPathSegment> PathSegments => pathSegmentsHolder.Value;
4040

4141
private IReadOnlyList<IPathSegment> AdaptToPathSegments()
4242
{

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/Routing/PathContentSegmentAdapter{T}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ internal sealed class PathContentSegmentAdapter<T> : IPathContentSegment where T
1111
private static readonly Lazy<Func<T, bool>> catchAllAccessor = new( NewCatchAllAccessor );
1212
private static readonly Lazy<Func<T, IEnumerable<object>>> subsegmentsAccessor = new( NewSubsegmentsAccessor );
1313
private readonly T adapted;
14-
private readonly Lazy<IReadOnlyList<IPathSubsegment>> subsegments;
14+
private readonly Lazy<IReadOnlyList<IPathSubsegment>> subsegmentsHolder;
1515

1616
public PathContentSegmentAdapter( T adapted )
1717
{
1818
this.adapted = adapted;
19-
subsegments = new Lazy<IReadOnlyList<IPathSubsegment>>( AdaptToPathSubsegments );
19+
subsegmentsHolder = new Lazy<IReadOnlyList<IPathSubsegment>>( AdaptToPathSubsegments );
2020
}
2121

2222
public bool IsCatchAll => catchAllAccessor.Value( adapted );
2323

24-
public IReadOnlyList<IPathSubsegment> Subsegments => subsegments.Value;
24+
public IReadOnlyList<IPathSubsegment> Subsegments => subsegmentsHolder.Value;
2525

2626
public override string ToString() => adapted.ToString();
2727

src/AspNet/WebApi/src/Asp.Versioning.WebApi/Controllers/ActionSelectorCacheItem.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,10 @@ private static List<CandidateActionWithParams> GetInitialCandidateWithParameterL
313313
{
314314
var candidate = candidates[i];
315315

316-
if ( actionName == null || candidate.MatchName( actionName ) )
316+
if ( ( actionName == null || candidate.MatchName( actionName ) ) &&
317+
( ignoreMethods || candidate.MatchVerb( incomingMethod ) ) )
317318
{
318-
if ( ignoreMethods || candidate.MatchVerb( incomingMethod ) )
319-
{
320-
candidateActionWithParams.Add( new( candidate, combinedParameterNames, subRouteData ) );
321-
}
319+
candidateActionWithParams.Add( new( candidate, combinedParameterNames, subRouteData ) );
322320
}
323321
}
324322
}
@@ -360,25 +358,13 @@ private CandidateAction[] GetInitialCandidateList( HttpControllerContext control
360358
candidatesFoundByName[i] = new( actionsFoundByName[i] );
361359
}
362360

363-
if ( ignoreMethods )
364-
{
365-
candidates = candidatesFoundByName;
366-
}
367-
else
368-
{
369-
candidates = FilterIncompatibleMethods( incomingMethod, candidatesFoundByName );
370-
}
361+
candidates = ignoreMethods ? candidatesFoundByName : FilterIncompatibleMethods( incomingMethod, candidatesFoundByName );
371362
}
372363
else
373364
{
374-
if ( ignoreMethods )
375-
{
376-
candidates = standardActions!.StandardCandidateActions!;
377-
}
378-
else
379-
{
380-
candidates = FindActionsForMethod( incomingMethod, standardActions!.CacheListMethods!, standardActions!.StandardCandidateActions! );
381-
}
365+
candidates = ignoreMethods
366+
? standardActions!.StandardCandidateActions!
367+
: FindActionsForMethod( incomingMethod, standardActions!.CacheListMethods!, standardActions!.StandardCandidateActions! );
382368
}
383369

384370
return candidates;
@@ -526,16 +512,9 @@ internal static string CreateAmbiguousMatchList( IEnumerable<HttpActionDescripto
526512
foreach ( var descriptor in ambiguousCandidates )
527513
{
528514
var controllerDescriptor = descriptor.ControllerDescriptor;
529-
string controllerTypeName;
530-
531-
if ( controllerDescriptor != null && controllerDescriptor.ControllerType != null )
532-
{
533-
controllerTypeName = controllerDescriptor.ControllerType.FullName;
534-
}
535-
else
536-
{
537-
controllerTypeName = string.Empty;
538-
}
515+
var controllerTypeName = controllerDescriptor != null && controllerDescriptor.ControllerType != null
516+
? controllerDescriptor.ControllerType.FullName
517+
: string.Empty;
539518

540519
exceptionMessageBuilder.AppendLine();
541520
exceptionMessageBuilder.AppendFormat(

0 commit comments

Comments
 (0)