Skip to content

Commit

Permalink
Merge pull request antlr#3322 from kaby76/issue-3319
Browse files Browse the repository at this point in the history
Fix for Issue 3319
  • Loading branch information
parrt committed Oct 26, 2021
2 parents a686ffb + 7773a5e commit 6af4c77
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
73 changes: 73 additions & 0 deletions runtime/Go/antlr/atnconfigset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package antlr

import (
"testing"
)

// Test for Issue # 3319
// To run, "cd antlr4/runtime/Go/antlr/", then "go test".
// In the old runtime code, the test would crash because it would try
// to compare a *LexerActionExecutor with nil, causing a nil pointer dereference.
// It only happens if there were different states that had equal stateNumber mod 16,
// and you created that ATNConfig with a nil LexerActionExecutor. (That's why this
// code has a hardwired constant of 16.

func TestCompare(t *testing.T) {
var set = NewOrderedATNConfigSet()
var s0 = NewBaseATNState()
var s1 = NewBaseATNState()
var s2 = NewBaseATNState()
var s3 = NewBaseATNState()
var s16 = NewBaseATNState()
s16.SetStateNumber(16)
var s17 = NewBaseATNState()
s17.SetStateNumber(17)
var s18 = NewBaseATNState()
s18.SetStateNumber(18)
var s19 = NewBaseATNState()
s19.SetStateNumber(19)
var la0 = NewBaseLexerAction(1)
var la1 = NewBaseLexerAction(2)
var laa = make([]LexerAction, 2)
laa[0] = la0
laa[1] = la1
var ae = NewLexerActionExecutor(laa)
set.Add(NewLexerATNConfig5(s0, 0, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s0, 1, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s0, 2, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s1, 0, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s1, 1, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s1, 2, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s2, 0, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s2, 1, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s2, 2, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s3, 0, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s3, 1, BasePredictionContextEMPTY, ae), nil)
set.Add(NewLexerATNConfig5(s3, 2, BasePredictionContextEMPTY, ae), nil)

set.Add(NewLexerATNConfig5(s0, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s0, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s0, 2, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s1, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s1, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s1, 2, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s2, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s2, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s2, 2, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s3, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s3, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s3, 2, BasePredictionContextEMPTY, nil), nil)

set.Add(NewLexerATNConfig5(s16, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s16, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s16, 2, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s17, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s17, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s17, 2, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s18, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s18, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s18, 2, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s19, 0, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s19, 1, BasePredictionContextEMPTY, nil), nil)
set.Add(NewLexerATNConfig5(s19, 2, BasePredictionContextEMPTY, nil), nil)
}
11 changes: 7 additions & 4 deletions runtime/Go/antlr/lexer_action_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ func (l *LexerActionExecutor) hash() int {
func (l *LexerActionExecutor) equals(other interface{}) bool {
if l == other {
return true
} else if _, ok := other.(*LexerActionExecutor); !ok {
}
othert, ok := other.(*LexerActionExecutor)
if !ok {
return false
}
if othert == nil {
return false
} else {
return l.cachedHash == other.(*LexerActionExecutor).cachedHash &&
&l.lexerActions == &other.(*LexerActionExecutor).lexerActions
}
return l.cachedHash == othert.cachedHash && &l.lexerActions == &othert.lexerActions
}

0 comments on commit 6af4c77

Please sign in to comment.