From 94f56c1c1540d40ea5620f6ee50682530f144e7e Mon Sep 17 00:00:00 2001 From: Botond Baranyi <botond.baranyi@ericsson.com> Date: Wed, 28 Oct 2020 16:44:28 +0100 Subject: [PATCH] Fixed charstring pattern subtyping for empty strings (bug 568340) Signed-off-by: Botond Baranyi <botond.baranyi@ericsson.com> Change-Id: I8dcf52db6b24dc91f302452cc1c3c89bd73a5176 --- compiler2/PredefFunc.cc | 12 ++++- compiler2/PredefFunc.hh | 5 ++ compiler2/subtypestuff.cc | 4 +- .../compileonly/Bug568340/.gitignore | 4 ++ .../compileonly/Bug568340/Bug568340.ttcn | 20 ++++++++ .../compileonly/Bug568340/Makefile | 50 +++++++++++++++++++ regression_test/compileonly/Makefile | 2 +- 7 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 regression_test/compileonly/Bug568340/.gitignore create mode 100644 regression_test/compileonly/Bug568340/Bug568340.ttcn create mode 100644 regression_test/compileonly/Bug568340/Makefile diff --git a/compiler2/PredefFunc.cc b/compiler2/PredefFunc.cc index 3e23e39f8..4f80aedfc 100644 --- a/compiler2/PredefFunc.cc +++ b/compiler2/PredefFunc.cc @@ -620,6 +620,16 @@ namespace Common { string* regexp(const string& instr, const string& expression, const Int& groupno, bool nocase) + { + string* retval = regexp_internal(instr, expression, groupno, nocase); + if (retval != NULL) { + return retval; + } + return new string(); + } + + string* regexp_internal(const string& instr, const string& expression, + const Int& groupno, bool nocase) { string *retval=0; @@ -664,13 +674,11 @@ namespace Common { if(pmatch[nmatch].rm_so != -1 && pmatch[nmatch].rm_eo != -1) retval = new string(instr.substr(pmatch[nmatch].rm_so, pmatch[nmatch].rm_eo - pmatch[nmatch].rm_so)); - else retval=new string(); } Free(pmatch); if(ret_val!=0) { if(ret_val==REG_NOMATCH) { regfree(&posix_regexp); - retval=new string(); } else { /* regexp error */ diff --git a/compiler2/PredefFunc.hh b/compiler2/PredefFunc.hh index 026ded84c..0be5e57b5 100644 --- a/compiler2/PredefFunc.hh +++ b/compiler2/PredefFunc.hh @@ -61,8 +61,13 @@ namespace Common { extern Real int2float(const int_val_t& value); extern int_val_t* float2int(const Real& value, const Location& loc); extern string* float2str(const Real& value); + // returns a pointer to an empty string if the input doesn't match the expression (as in TTCN-3) extern string* regexp(const string& instr, const string& expression, const Int& groupno, bool nocase); + // returns a null pointer if the input doesn't match the expression + // (to distinguish between a matching empty string and non-matching) + extern string* regexp_internal(const string& instr, const string& expression, + const Int& groupno, bool nocase); extern ustring* regexp(const ustring& instr, const ustring& expression, const Int& groupno, bool nocase); extern string* remove_bom(const string& encoded_value); diff --git a/compiler2/subtypestuff.cc b/compiler2/subtypestuff.cc index 4e1211aa3..5e77bffb0 100644 --- a/compiler2/subtypestuff.cc +++ b/compiler2/subtypestuff.cc @@ -508,8 +508,8 @@ tribool StringPatternConstraint::match(const string& str) const { string patt = pattern->get_full_str(); if (patt.size()==0) return TRIBOOL(str.size()==0); - string *result = regexp(str, string('(')+patt+string(')'), 0, pattern->get_nocase()); - bool rv = (result->size()!=0); + string *result = regexp_internal(str, string('(')+patt+string(')'), 0, pattern->get_nocase()); + bool rv = (result!=0); delete result; return TRIBOOL(rv); } diff --git a/regression_test/compileonly/Bug568340/.gitignore b/regression_test/compileonly/Bug568340/.gitignore new file mode 100644 index 000000000..67fd87c0c --- /dev/null +++ b/regression_test/compileonly/Bug568340/.gitignore @@ -0,0 +1,4 @@ +Bug568340 +Bug568340.exe +Bug568340*.cc +Bug568340*.hh diff --git a/regression_test/compileonly/Bug568340/Bug568340.ttcn b/regression_test/compileonly/Bug568340/Bug568340.ttcn new file mode 100644 index 000000000..d46b687fc --- /dev/null +++ b/regression_test/compileonly/Bug568340/Bug568340.ttcn @@ -0,0 +1,20 @@ +/****************************************************************************** + * Copyright (c) 2000-2020 Ericsson Telecom AB + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html + * + * Contributors: + * Baranyi, Botond + * + ******************************************************************************/ + +// pattern subtyping for empty strings (bug 568340) +module Bug568340 { + +type charstring T (pattern "(/?+)#(0,1)"); + +const T val := ""; + +} diff --git a/regression_test/compileonly/Bug568340/Makefile b/regression_test/compileonly/Bug568340/Makefile new file mode 100644 index 000000000..9552e0432 --- /dev/null +++ b/regression_test/compileonly/Bug568340/Makefile @@ -0,0 +1,50 @@ +############################################################################## +# Copyright (c) 2000-2020 Ericsson Telecom AB +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# which accompanies this distribution, and is available at +# https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html +# +# Contributors: +# Botond, Baranyi +# +############################################################################## +TOPDIR := ../.. +include $(TOPDIR)/Makefile.regression + +.PHONY: all clean dep + +TTCN3_LIB = ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX) + +TTCN3_MODULES = Bug568340.ttcn + +GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.cc) +GENERATED_HEADERS = $(GENERATED_SOURCES:.cc=.hh) +ifdef CODE_SPLIT +GENERATED_SOURCES := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), .cc _seq.cc _set.cc _seqof.cc _setof.cc _union.cc)) +else ifdef SPLIT_TO_SLICES +POSTFIXES := $(foreach file, $(SPLIT_TO_SLICES), $(addsuffix $(file), _part_)) +POSTFIXES := $(foreach file, $(POSTFIXES), $(addprefix $(file), .cc)) +GENERATED_SOURCES2 := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), $(POSTFIXES))) +GENERATED_SOURCES += $(GENERATED_SOURCES2) +endif + +OBJECTS = $(GENERATED_SOURCES:.cc=.o) + +TARGET = Bug568340$(EXESUFFIX) + +all: $(TARGET) + +$(TARGET): $(GENERATED_SOURCES) $(USER_SOURCES) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ -L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) \ + -L$(OPENSSL_DIR)/lib -lcrypto $($(PLATFORM)_LIBS) + +$(GENERATED_SOURCES) $(GENERATED_HEADERS): $(TTCN3_MODULES) + $(TTCN3_COMPILER) $(COMPILER_FLAGS) $^ + +clean distclean: + $(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \ + $(GENERATED_SOURCES) compile *.log + +dep: $(GENERATED_SOURCES) + makedepend $(CPPFLAGS) $(GENERATED_SOURCES) diff --git a/regression_test/compileonly/Makefile b/regression_test/compileonly/Makefile index b37605bee..cf0922753 100644 --- a/regression_test/compileonly/Makefile +++ b/regression_test/compileonly/Makefile @@ -31,7 +31,7 @@ CODIRS := dynamicTemplate styleGuide topLevelPdu \ asn1_hyphen nameClash portTranslation UntaggedOnTopLevelUnionSwitch \ typeDescrGenWIthNoXER languageSpec asn1_opentype openTypeXER \ asnStringSubtypes componentParameter realtimeKeywords Bug550526 \ - Bug550623 Bug552232 + Bug550623 Bug552232 Bug568340 ifdef DYN CODIRS += mfgen-xsd -- GitLab