From be63ee81ca09b038653b597079cd0e76465aadee Mon Sep 17 00:00:00 2001 From: BenceJanosSzabo <bence.janos.szabo@ericsson.com> Date: Thu, 7 Sep 2017 09:38:38 +0200 Subject: [PATCH] XER: fixed nfinite recursion in generated code (Bug 521803) Change-Id: Ic7440e44ba1f20d0995eb4d13256d81ab06c5245 Signed-off-by: BenceJanosSzabo <bence.janos.szabo@ericsson.com> --- compiler2/union.c | 12 +++-- function_test/XER_EncDec/XER_EncDec_TD.script | 50 +++++++++++++++++++ .../XML/EXER-whitepaper/Untagged.ttcnpp | 33 ++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/compiler2/union.c b/compiler2/union.c index 881c642cc..bbdb18360 100644 --- a/compiler2/union.c +++ b/compiler2/union.c @@ -1342,10 +1342,14 @@ void defUnionClass(struct_def const *sdef, output_struct *output) /* An untagged union can start with the start tag of any alternative */ for (i = 0; i < sdef->nElements; i++) { - src=mputprintf(src, - " if (%s::can_start(name, uri, %s_xer_, flavor, flavor2)) return TRUE;\n" - , sdef->elements[i].type, sdef->elements[i].typegen - ); + // An untagged union cannot start with itself. It would create an infinite + // recursion. + if (strcmp(sdef->elements[i].type, sdef->name) != 0) { + src=mputprintf(src, + " if (%s::can_start(name, uri, %s_xer_, flavor, flavor2)) return TRUE;\n" + , sdef->elements[i].type, sdef->elements[i].typegen + ); + } } src = mputstr(src, " return FALSE;\n}\n\n"); diff --git a/function_test/XER_EncDec/XER_EncDec_TD.script b/function_test/XER_EncDec/XER_EncDec_TD.script index 716f8c81f..091db3ca9 100644 --- a/function_test/XER_EncDec/XER_EncDec_TD.script +++ b/function_test/XER_EncDec/XER_EncDec_TD.script @@ -1461,6 +1461,56 @@ Dynamic test case error: Performing a valueof or send operation on a non-specifi :exmp. +.*---------------------------------------------------------------------* +:h4. Decoding record with untagged self recursive union field from incorrect XML +.*---------------------------------------------------------------------* +:xmp tab=0. + +<TC - Decoding record with untagged self recursive union field from incorrect XML> + +<STATIC> + +type component Test_CT{}; + +<TTCN_TC:PURE_EXEC> + +type record MyRecord { + UntaggedUnion u +} with { + encode "XML"; +} + +type union UntaggedUnion { + integer i, + UntaggedUnion u +} with { + encode "XML"; + variant "untagged"; +} + +external function ef_xer_dec(in octetstring par) return MyRecord + with { extension "prototype(convert) decode (XER:XER_EXTENDED) errorbehavior(ALL:ERROR)" } + +testcase tc_dec_self_recursive_untagged_field() runs on Test_CT { + var universal charstring bad_xml := "<MyRecord>\n\t<bad>44</bad>\n</MyRecord>\n\n"; + + var MyRecord v_res := ef_xer_dec(unichar2oct(bad_xml)); + + setverdict(pass); +} + +control { + execute(tc_dec_self_recursive_untagged_field()); +} + +<RESULT> + +While XER-decoding type '@Temp.MyRecord': Component 'u': 'bad' does not match any alternative + +<END_TC> + +:exmp. + .*---------------------------------------------------------------------* :h1.REFERENCES .*---------------------------------------------------------------------* diff --git a/regression_test/XML/EXER-whitepaper/Untagged.ttcnpp b/regression_test/XML/EXER-whitepaper/Untagged.ttcnpp index ba4930d3d..60482a45b 100644 --- a/regression_test/XML/EXER-whitepaper/Untagged.ttcnpp +++ b/regression_test/XML/EXER-whitepaper/Untagged.ttcnpp @@ -786,6 +786,36 @@ testcase decode_ut_union2() runs on UTA CHECK_DECODE(exer_dec_untunion2, s_untunion2, UntaggedUnion2, c_untunion2); } + +// ------- untagged union with a recursive field + +type record MyRecord { + UntaggedUnion3 u +} + +type union UntaggedUnion3 { + integer i, + UntaggedUnion3 u +} with { + variant "untagged"; +} + +DECLARE_EXER_ENCODERS(MyRecord, untunion3); + +const MyRecord c_untunion3 := { u := { i := 44 } }; + +const universal charstring s_untunion3 := "<MyRecord>\n\t<i>44</i>\n</MyRecord>\n\n"; + +testcase encode_ut_union3() runs on UTA +{ + CHECK_METHOD(exer_enc_untunion3, c_untunion3, s_untunion3); +} + +testcase decode_ut_union3() runs on UTA +{ + CHECK_DECODE(exer_dec_untunion3, s_untunion3, MyRecord, c_untunion3); +} + /* * * * * * * * * * * Run it! * * * * * * * * * * */ control { @@ -831,6 +861,9 @@ control { execute(encode_ut_union2()); execute(decode_ut_union2()); + execute(encode_ut_union3()); + execute(decode_ut_union3()); + execute(encode_ut_union_recof()); execute(decode_ut_union_recof()); } -- GitLab