Skip to content

Fix ISSUE-141: edi reader stuck in a dead-loop with unrecognized raw segment at the end of the input. #143

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

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Records": [
"{'IEA':{},'ISA':{}}",
"{'IEA':{},'ISA':{}}"
],
"FinalErr": "EOF"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Records": [
"{}"
],
"FinalErr": "input 'test' at segment no.2 (char[13,13]): segment 'UNKNOWN' is either not declared in schema or appears in an invalid order"
}
12 changes: 12 additions & 0 deletions extensions/omniv21/fileformat/edi/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,20 @@ func (r *ediReader) Read() (*idr.Node, error) {
if err != nil {
return nil, err
}
if len(r.stack) <= 1 {
// we have a raw segment waiting for processing but currently
// the decl stack is effectively empty (with only the artificial
// #root decl on it. We can get into this situation two ways:
// 1.
}
cur := r.stackTop()
if !cur.segDecl.matchSegName(rawSeg.Name) {
if len(r.stack) <= 1 {
return nil, ErrInvalidEDI(r.fmtErrStr2(
r.r.SegCount(), r.r.RuneEnd(), r.r.RuneEnd(),
"segment '%s' is either not declared in schema or appears in an invalid order",
rawSeg.Name))
}
err = r.segNext()
if err != nil {
return nil, err
Expand Down
94 changes: 69 additions & 25 deletions extensions/omniv21/fileformat/edi/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,11 @@ func TestSegDoneSegNext(t *testing.T) {

func TestRead(t *testing.T) {
for _, test := range []struct {
name string
input string
declJSON string
xpath string
err string
name string
input string
declJSON string
xpath string
readerCreationErr string
}{
{
name: "invalid target xpath, failure",
Expand All @@ -603,8 +603,8 @@ func TestRead(t *testing.T) {
{ "name": "ISA", "min": 0 }
]
}`,
xpath: "[",
err: `invalid target xpath '[', err: expression must evaluate to a node-set`,
xpath: "[",
readerCreationErr: `invalid target xpath '[', err: expression must evaluate to a node-set`,
},
{
name: "empty input, success",
Expand All @@ -617,8 +617,8 @@ func TestRead(t *testing.T) {
{ "name": "ISA", "min": 0 }
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "single seg decl, multiple seg instances, success",
Expand All @@ -640,8 +640,8 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "2 seg decls, success",
Expand Down Expand Up @@ -669,8 +669,8 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "2 seg groups, filtered target, success",
Expand Down Expand Up @@ -711,8 +711,8 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: ".[e1 != '6']",
err: "",
xpath: ".[e1 != '6']",
readerCreationErr: "",
},
{
name: "seg min not satisfied before EOF, failure",
Expand Down Expand Up @@ -740,8 +740,8 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "missing raw seg name, failure",
Expand All @@ -762,8 +762,8 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "raw seg processing wrong, failure",
Expand All @@ -784,8 +784,8 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "seg min not satisfied before next seg appearance, failure",
Expand All @@ -810,18 +810,62 @@ func TestRead(t *testing.T) {
}
]
}`,
xpath: "",
err: "",
xpath: "",
readerCreationErr: "",
},
{
name: "unprocessed raw segment, failure",
input: "ISA\nUNKNOWN\n",
declJSON: `
{
"segment_delimiter": "\n",
"element_delimiter": "*",
"segment_declarations": [
{
"name": "ISA",
"is_target": true,
"min": 0
}
]
}`,
xpath: "",
readerCreationErr: "",
},
{
name: "multiple root level segments, success",
input: "ISA\nIEA\nISA\nIEA\n",
declJSON: `
{
"segment_delimiter": "\n",
"element_delimiter": "*",
"segment_declarations": [
{
"name": "group1",
"type": "segment_group",
"is_target": true,
"child_segments": [
{
"name": "ISA"
},
{
"name": "IEA"
}
]
}
]
}`,
xpath: "",
readerCreationErr: "",
},
} {
t.Run(test.name, func(t *testing.T) {
var decl FileDecl
err := json.Unmarshal([]byte(test.declJSON), &decl)
assert.NoError(t, err)
reader, err := NewReader("test", strings.NewReader(test.input), &decl, test.xpath)
if test.err != "" {
if test.readerCreationErr != "" {
assert.Error(t, err)
assert.Equal(t, test.err, err.Error())
assert.Equal(t, test.readerCreationErr, err.Error())
assert.Nil(t, reader)
return
}
Expand Down