Fix ISSUE-141: edi reader stuck in a dead-loop with unrecognized raw segment at the end of the input. #143
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes #141:
The triggering raw segment in the input is at line 36: "DTP":
(added line number at beginning of each line so we can reference them)
The issue is with the schema, where inside
EB
seg group, we have, in this particular order, the following child segments:EB
,DTP
,LS
,HSD
. You can see theDTP
seg is declared in front ofHSD
, but in the input it appears after. Now bothDTP
andHSD
seg decl hasmin = 0
. When edi reader sees the 3HSD
, it thinks it's okay, sinceDTP
seg is optional. Then edi reader sees theDTP
. Unfortunately this time it can't find a proper placement match for it, thus it considers theEB
seg group is done. Cascadingly, it considers the wrappingtransaction_set_id
seg group as well as theGS
andISA
are all done. Thus it moves on. Then it seesSE
andIEA
both are optional, so seems like to it everything is done. EDI reader now rewinds the seg processing stacking all the way to the #root. At this moment, essentially we have a dangling raw segmentDTP
unprocessed and unaccounted for. EDI reader should've gracefully failed, but it didn't. Due a bug, it will try to mark the current instance of #root seg decl done, and move onto the next instance of #root decl. But again,DTP
is still not matched in the next instance of #root (the first segment in this schema should always beISA
), it will move on to the next instance of #root, yet again and again and again - infinite dead-loop.The fix is:
If we can't match a raw seg name to the current decl stack, and if the current decl stack is nothing but the virtual #root decl, then we have a dangling unmated seg and return a failure immediately.