diff --git a/core/Bitstring.cc b/core/Bitstring.cc
index e1b31642f3c1354fb949f92cd1c302d08342d8ac..0c9d4b13e47ca2ced292547e17b2608b2d0544bc 100644
--- a/core/Bitstring.cc
+++ b/core/Bitstring.cc
@@ -1052,7 +1052,8 @@ int BITSTRING::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
   cp.fieldorder = p_td.raw->fieldorder;
   cp.hexorder = ORDER_LSB;
   buff.get_b((size_t) decode_length, val_ptr->bits_ptr, cp, top_bit_ord);
-  if (p_td.raw->length_restrition != -1) {
+  if (p_td.raw->length_restrition != -1 &&
+      decode_length > p_td.raw->length_restrition) {
     val_ptr->n_bits = p_td.raw->length_restrition;
     if (p_td.raw->endianness == ORDER_LSB) {
       if ((decode_length - val_ptr->n_bits) % 8) {
diff --git a/core/Charstring.cc b/core/Charstring.cc
index 4dfd526d388fc6dd3d1976085563079c85b4c643..ed0a4a1534226bd302cc19df9098c9d954002751 100644
--- a/core/Charstring.cc
+++ b/core/Charstring.cc
@@ -1614,7 +1614,8 @@ int CHARSTRING::RAW_decode(const TTCN_Typedescriptor_t& p_td,
     temp_buff.get_string(*this);
     decode_length=str_len+8;
   }
-  if (p_td.raw->length_restrition != -1) {
+  if (p_td.raw->length_restrition != -1 &&
+      decode_length > p_td.raw->length_restrition) {
     val_ptr->n_chars = p_td.raw->length_restrition;
     if (p_td.raw->endianness == ORDER_MSB) memmove(val_ptr->chars_ptr,
       val_ptr->chars_ptr + (decode_length / 8 - val_ptr->n_chars),
diff --git a/core/Hexstring.cc b/core/Hexstring.cc
index 93f523816cc5ed4c91b8a6c247812362b865637a..f7a406c21e0f43d7b13d0c2b726a79dfed1cb550 100644
--- a/core/Hexstring.cc
+++ b/core/Hexstring.cc
@@ -828,7 +828,8 @@ int HEXSTRING::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
   init_struct(decode_length / 4);
   buff.get_b((size_t) decode_length, val_ptr->nibbles_ptr, cp, top_bit_ord);
 
-  if (p_td.raw->length_restrition != -1) {
+  if (p_td.raw->length_restrition != -1 &&
+      decode_length > p_td.raw->length_restrition) {
     val_ptr->n_nibbles = p_td.raw->length_restrition;
     if (p_td.raw->endianness == ORDER_MSB) {
       if ((decode_length - val_ptr->n_nibbles * 4) % 8) {
diff --git a/core/Octetstring.cc b/core/Octetstring.cc
index 92ede5ff6eff76267befc31a19f28848742f87db..c40cb88a65e4b1c218570444cf51c223cf577ebf 100644
--- a/core/Octetstring.cc
+++ b/core/Octetstring.cc
@@ -1261,7 +1261,8 @@ int OCTETSTRING::RAW_decode(const TTCN_Typedescriptor_t& p_td,
   init_struct(decode_length / 8);
   buff.get_b((size_t) decode_length, val_ptr->octets_ptr, cp, top_bit_ord);
 
-  if (p_td.raw->length_restrition != -1) {
+  if (p_td.raw->length_restrition != -1 &&
+      decode_length > p_td.raw->length_restrition) {
     val_ptr->n_octets = p_td.raw->length_restrition;
     if (p_td.raw->endianness == ORDER_MSB) memmove(val_ptr->octets_ptr,
       val_ptr->octets_ptr + (decode_length / 8 - val_ptr->n_octets),
diff --git a/regression_test/RAW/Bug522656/.gitignore b/regression_test/RAW/Bug522656/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..1327e72386432b0e6d7b4dd0bd94504049ba2b20
--- /dev/null
+++ b/regression_test/RAW/Bug522656/.gitignore
@@ -0,0 +1,7 @@
+Bug522656
+Bug522656.exe
+Bug522656*.cc
+Bug522656*.hh
+compile
+Bug522656*.log
+*.d
diff --git a/regression_test/RAW/Bug522656/Bug522656.ttcn b/regression_test/RAW/Bug522656/Bug522656.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..9d3ff513a79669af4a6d082e11481155782947d2
--- /dev/null
+++ b/regression_test/RAW/Bug522656/Bug522656.ttcn
@@ -0,0 +1,108 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Botond, Baranyi
+ *
+ ******************************************************************************/
+
+// RAW decoding strings with an empty buffer changes the length of the static 'empty
+// string' values to the length restriction, if a length restriction is set.
+module Bug522656 {
+
+type bitstring Bit2 length (2)
+with { variant "FIELDLENGTH(2)" }
+
+type hexstring Hex2 length (2)
+with { variant "FIELDLENGTH(2)" }
+
+type octetstring Oct1 length (1)
+with { variant "FIELDLENGTH(1)" }
+
+type charstring Char3 length (3)
+with { variant "FIELDLENGTH(3)" }
+
+type record Rec {
+  Bit2 b,
+  Hex2 h,
+  Oct1 o,
+  Char3 c
+}
+
+type component CT {}
+
+// This test case decodes the strings. The decoder is set to display only warnings
+// upon failure, and continue the decoding.
+// The changes in the empty strings are checked in other test cases.
+testcase tc_decode() runs on CT {
+  var Rec x;
+  var bitstring buffer := ''B;
+  var integer result := decvalue(buffer, x);
+  if (result != 0) {
+    setverdict(pass);
+  }
+  else {
+    setverdict(fail, "Decoded with empty buffer successful.");
+  }
+}
+
+// Make sure the empty bitstring value has not changed.
+testcase tc_bitstring() runs on CT {
+  var bitstring empty_bit := ''B;
+  if (lengthof(empty_bit) == 0) {
+    setverdict(pass);
+  }
+  else {
+    setverdict(fail, "Empty bitstring length: ", lengthof(empty_bit));
+  }
+}
+
+// Make sure the empty hexstring value has not changed.
+testcase tc_hexstring() runs on CT {
+  var hexstring empty_hex := ''H;
+  if (lengthof(empty_hex) == 0) {
+    setverdict(pass);
+  }
+  else {
+    setverdict(fail, "Empty hexstring length: ", lengthof(empty_hex));
+  }
+}
+
+// Make sure the empty octetstring value has not changed.
+testcase tc_octetstring() runs on CT {
+  var octetstring empty_oct := ''O;
+  if (lengthof(empty_oct) == 0) {
+    setverdict(pass);
+  }
+  else {
+    setverdict(fail, "Empty octetstring length: ", lengthof(empty_oct));
+  }
+}
+
+// Make sure the empty charstring value has not changed.
+testcase tc_charstring() runs on CT {
+  var charstring empty_char := "";
+  if (lengthof(empty_char) == 0) {
+    setverdict(pass);
+  }
+  else {
+    setverdict(fail, "Empty charstring length: ", lengthof(empty_char));
+  }
+}
+
+control {
+  execute(tc_decode()); // must be executed before the other test cases
+  execute(tc_bitstring());
+  execute(tc_hexstring());
+  execute(tc_octetstring());
+  execute(tc_charstring());
+}
+
+}
+with {
+  encode "RAW"
+}
diff --git a/regression_test/RAW/Bug522656/Makefile b/regression_test/RAW/Bug522656/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..4514a22da06115094f047b621bde4bc2c81f04ba
--- /dev/null
+++ b/regression_test/RAW/Bug522656/Makefile
@@ -0,0 +1,147 @@
+##############################################################################
+# Copyright (c) 2000-2017 Ericsson Telecom AB
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#   Botond, Baranyi
+#
+##############################################################################
+TOPDIR := ../..
+include   ../../Makefile.regression
+
+# WARNING! This Makefile can be used with GNU make only.
+# Other versions of make may report syntax errors in it.
+
+#
+# Do NOT touch this line...
+#
+.PHONY: all archive check clean dep objects
+
+.SUFFIXES: .d
+
+#
+# Set these variables...
+#
+
+# Flags for dependency generation
+CXXDEPFLAGS = -MM
+
+ifeq ($(PLATFORM), WIN32)
+# Silence linker warnings.
+# Overridden by Makefile.cfg
+endif
+
+# Flags for the TTCN-3 and ASN.1 compiler:
+COMPILER_FLAGS += 
+
+# Execution mode: (either ttcn3 or ttcn3-parallel)
+TTCN3_LIB = ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX)
+
+
+#
+# You may change these variables. Add your files if necessary...
+#
+
+# TTCN-3 modules of this project:
+TTCN3_MODULES = Bug522656.ttcn
+
+# ASN.1 modules of this project:
+ASN1_MODULES =
+
+# C++ source & header files generated from the TTCN-3 & ASN.1 modules of
+# this project:
+GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.cc) $(ASN1_MODULES:.asn=.cc)
+GENERATED_HEADERS = $(GENERATED_SOURCES:.cc=.hh)
+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
+# C/C++ Source & header files of Test Ports, external functions and
+# other modules:
+USER_SOURCES =
+USER_HEADERS = $(USER_SOURCES:.cc=.hh)
+
+# Object files of this project that are needed for the executable test suite:
+OBJECTS = $(GENERATED_OBJECTS) $(USER_OBJECTS)
+
+GENERATED_OBJECTS = $(GENERATED_SOURCES:.cc=.o)
+
+USER_OBJECTS = $(USER_SOURCES:.cc=.o)
+
+DEPFILES = $(USER_OBJECTS:.o=.d)  $(GENERATED_OBJECTS:.o=.d)
+
+
+# The name of the executable test suite:
+TARGET = Bug522656$(EXESUFFIX)
+
+
+# Rules for building the executable...
+#
+
+all: $(TARGET) ;
+
+objects: $(OBJECTS) ;
+
+$(TARGET): $(OBJECTS)
+	if $(CXX) $(LDFLAGS) -o $@ $^ \
+	-L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) \
+	-L$(OPENSSL_DIR)/lib -lcrypto \
+	-L$(XMLDIR)/lib $($(PLATFORM)_LIBS); \
+	then : ; else $(TTCN3_DIR)/bin/titanver $(OBJECTS); exit 1; fi
+
+.cc.o .c.o:
+	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
+
+.cc.d .c.d:
+	@echo Creating dependency file for '$<'; set -e; \
+	$(CXX) $(CXXDEPFLAGS) $(CPPFLAGS) $(CXXFLAGS) $< \
+	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
+	[ -s $@ ] || rm -f $@
+
+$(GENERATED_SOURCES) $(GENERATED_HEADERS): compile
+	@if [ ! -f $@ ]; then $(RM) compile; $(MAKE) compile; fi
+
+compile: $(TTCN3_MODULES) $(ASN1_MODULES)
+	$(TTCN3_COMPILER) $(COMPILER_FLAGS) $^ - $?
+	touch $@
+
+
+clean:
+	-$(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
+	$(GENERATED_SOURCES) compile $(DEPFILES) \
+	tags *.log
+
+distclean: clean
+	-$(RM) $(DEPFILES)
+
+
+dep: $(GENERATED_SOURCES) $(USER_SOURCES) ;
+
+ifeq ($(findstring n,$(MAKEFLAGS)),)
+ifeq ($(filter clean distclean check compile archive diag,$(MAKECMDGOALS)),)
+-include $(DEPFILES)
+endif
+endif
+
+
+
+diag:
+	$(TTCN3_COMPILER) -v 2>&1
+	$(TTCN3_DIR)/bin/mctr_cli -v 2>&1
+	$(CXX) -v 2>&1
+	@echo TTCN3_DIR=$(TTCN3_DIR)
+	@echo OPENSSL_DIR=$(OPENSSL_DIR)
+	@echo XMLDIR=$(XMLDIR)
+	@echo PLATFORM=$(PLATFORM)
+
+#
+# Add your rules here if necessary...
+#
+
+run: $(TARGET) ./config.cfg
+	./$^
diff --git a/regression_test/RAW/Bug522656/config.cfg b/regression_test/RAW/Bug522656/config.cfg
new file mode 100644
index 0000000000000000000000000000000000000000..b32c3ff6c7551cc8271b2ce34a645272f3d0e2d8
--- /dev/null
+++ b/regression_test/RAW/Bug522656/config.cfg
@@ -0,0 +1,20 @@
+###############################################################################
+# Copyright (c) 2000-2017 Ericsson Telecom AB
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#   Botond, Baranyi
+#
+###############################################################################
+[LOGGING]
+LogFile := "Bug522656.log"
+FileMask := LOG_ALL
+ConsoleMask := TTCN_ERROR | TTCN_TESTCASE | TTCN_STATISTICS
+LogSourceInfo := Yes
+[EXECUTE]
+Bug522656.control
+
+[TESTPORT_PARAMETERS]
diff --git a/regression_test/RAW/Makefile b/regression_test/RAW/Makefile
index 648c6c103bd221deb2c2b2d05304be9429dc6273..53403f6734195ff79f41d78cd05598821f3e5c5f 100644
--- a/regression_test/RAW/Makefile
+++ b/regression_test/RAW/Makefile
@@ -18,7 +18,7 @@
 TOPDIR := ..
 include $(TOPDIR)/Makefile.regression
 
-RDIRS = Examples HN25015 HQ26535 HQ49956 HS16977 ustr Annex_E_variants Bug521125 Lengthto_Offset
+RDIRS = Examples HN25015 HQ26535 HQ49956 HS16977 ustr Annex_E_variants Bug521125 Lengthto_Offset Bug522656
 
 all dep clean run distclean:
 	for dir in $(RDIRS); do (cd $$dir && $(MAKE) $@) || exit; done