Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdec60c8

Browse files
authored
Fix null ref in SyntaxTokenCache (#30978) (#30988)
Fixes#27154
1 parent0fa43be commitdec60c8

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed

‎src/Razor/Microsoft.AspNetCore.Razor.Language/src/Syntax/InternalSyntax/SyntaxFactory.cs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
usingSystem;
5-
64
namespaceMicrosoft.AspNetCore.Razor.Language.Syntax.InternalSyntax
75
{
86
internalstaticpartialclassSyntaxFactory
97
{
108
internalstaticSyntaxTokenToken(SyntaxKindkind,stringcontent,paramsRazorDiagnostic[]diagnostics)
119
{
12-
if(SyntaxTokenCache.CanBeCached(kind,diagnostics))
10+
if(SyntaxTokenCache.Instance.CanBeCached(kind,diagnostics))
1311
{
14-
returnSyntaxTokenCache.GetCachedToken(kind,content);
12+
returnSyntaxTokenCache.Instance.GetCachedToken(kind,content);
1513
}
1614

1715
returnnewSyntaxToken(kind,content,diagnostics);

‎src/Razor/Microsoft.AspNetCore.Razor.Language/src/Syntax/InternalSyntax/SyntaxTokenCache.cs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
usingSystem;
5-
usingMicrosoft.Extensions.Internal;
67

78
namespaceMicrosoft.AspNetCore.Razor.Language.Syntax.InternalSyntax
89
{
910
// Simplified version of Roslyn's SyntaxNodeCache
10-
internalstaticclassSyntaxTokenCache
11+
internalsealedclassSyntaxTokenCache
1112
{
1213
privateconstintCacheSizeBits=16;
1314
privateconstintCacheSize=1<<CacheSizeBits;
1415
privateconstintCacheMask=CacheSize-1;
16+
publicstaticreadonlySyntaxTokenCacheInstance=new();
1517
privatestaticreadonlyEntry[]s_cache=newEntry[CacheSize];
1618

17-
privatestructEntry
19+
internalSyntaxTokenCache(){}
20+
21+
privatereadonlystructEntry
1822
{
1923
publicintHash{get;}
20-
publicSyntaxTokenToken{get;}
24+
publicSyntaxToken?Token{get;}
2125

2226
internalEntry(inthash,SyntaxTokentoken)
2327
{
@@ -26,7 +30,7 @@ internal Entry(int hash, SyntaxToken token)
2630
}
2731
}
2832

29-
publicstaticboolCanBeCached(SyntaxKindkind,paramsRazorDiagnostic[]diagnostics)
33+
publicboolCanBeCached(SyntaxKindkind,paramsRazorDiagnostic[]diagnostics)
3034
{
3135
if(diagnostics.Length==0)
3236
{
@@ -50,7 +54,7 @@ public static bool CanBeCached(SyntaxKind kind, params RazorDiagnostic[] diagnos
5054
returnfalse;
5155
}
5256

53-
publicstaticSyntaxTokenGetCachedToken(SyntaxKindkind,stringcontent)
57+
publicSyntaxTokenGetCachedToken(SyntaxKindkind,stringcontent)
5458
{
5559
varhash=(kind,content).GetHashCode();
5660

@@ -60,7 +64,7 @@ public static SyntaxToken GetCachedToken(SyntaxKind kind, string content)
6064
varidx=indexableHash&CacheMask;
6165
vare=s_cache[idx];
6266

63-
if(e.Hash==hash&&e.Token.Kind==kind&&e.Token.Content==content)
67+
if(e.Hash==hash&&e.Token!=null&&e.Token.Kind==kind&&e.Token.Content==content)
6468
{
6569
returne.Token;
6670
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
usingXunit;
5+
6+
namespaceMicrosoft.AspNetCore.Razor.Language.Syntax.InternalSyntax
7+
{
8+
publicclassSyntaxTokenCacheTest
9+
{
10+
// Regression test for https://github.com/dotnet/aspnetcore/issues/27154
11+
[Fact]
12+
publicvoidGetCachedToken_ReturnsNewEntry()
13+
{
14+
// Arrange
15+
varcache=newSyntaxTokenCache();
16+
17+
// Act
18+
vartoken=cache.GetCachedToken(SyntaxKind.Whitespace,"Hello world");
19+
20+
// Assert
21+
Assert.Equal(SyntaxKind.Whitespace,token.Kind);
22+
Assert.Equal("Hello world",token.Content);
23+
Assert.Empty(token.GetDiagnostics());
24+
}
25+
26+
[Fact]
27+
publicvoidGetCachedToken_ReturnsCachedToken()
28+
{
29+
// Arrange
30+
varcache=newSyntaxTokenCache();
31+
32+
// Act
33+
vartoken1=cache.GetCachedToken(SyntaxKind.Whitespace,"Hello world");
34+
vartoken2=cache.GetCachedToken(SyntaxKind.Whitespace,"Hello world");
35+
36+
// Assert
37+
Assert.Same(token1,token2);
38+
}
39+
40+
[Fact]
41+
publicvoidGetCachedToken_ReturnsDifferentEntries_IfKindsAreDifferent()
42+
{
43+
// Arrange
44+
varcache=newSyntaxTokenCache();
45+
46+
// Act
47+
vartoken1=cache.GetCachedToken(SyntaxKind.Whitespace,"Hello world");
48+
vartoken2=cache.GetCachedToken(SyntaxKind.Keyword,"Hello world");
49+
50+
// Assert
51+
Assert.NotSame(token1,token2);
52+
Assert.Equal(SyntaxKind.Whitespace,token1.Kind);
53+
Assert.Equal("Hello world",token1.Content);
54+
55+
Assert.Equal(SyntaxKind.Keyword,token2.Kind);
56+
Assert.Equal("Hello world",token2.Content);
57+
}
58+
59+
[Fact]
60+
publicvoidGetCachedToken_ReturnsDifferentEntries_IfContentsAreDifferent()
61+
{
62+
// Arrange
63+
varcache=newSyntaxTokenCache();
64+
65+
// Act
66+
vartoken1=cache.GetCachedToken(SyntaxKind.Keyword,"Text1");
67+
vartoken2=cache.GetCachedToken(SyntaxKind.Keyword,"Text2");
68+
69+
// Assert
70+
Assert.NotSame(token1,token2);
71+
Assert.Equal(SyntaxKind.Keyword,token1.Kind);
72+
Assert.Equal("Text1",token1.Content);
73+
74+
Assert.Equal(SyntaxKind.Keyword,token2.Kind);
75+
Assert.Equal("Text2",token2.Content);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp