diff --git a/compiler2/record_of.c b/compiler2/record_of.c
index dfe66158ef276b102c427860513d73cfc1f8dd7c..7cee6d8072dbed7ba59f34cf20c36deec0b94667 100644
--- a/compiler2/record_of.c
+++ b/compiler2/record_of.c
@@ -1404,7 +1404,7 @@ void defRecordOfClass1(const struct_of_def *sdef, output_struct *output)
        * then use that to decode the value. */
       "    for(char * str = strtok(x_val, \" \\t\\x0A\\x0D\"); str != 0; str = strtok(x_val + x_pos, \" \\t\\x0A\\x0D\")) {\n"
       // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell
-      "      x_pos += strlen(str) + 1;\n"
+      "      x_pos = (str - x_val) + strlen(str) + 1;\n"
       "      TTCN_Buffer buf_2;\n"
       "      buf_2.put_c('<');\n"
       "      write_ns_prefix(*p_td.oftype_descr, buf_2);\n"
diff --git a/core2/Basetype2.cc b/core2/Basetype2.cc
index fc4b0996c97908f76a919632450d9af2ae00486a..f41a9fa43557cb83055e0c99b6fcb239af535bb4 100644
--- a/core2/Basetype2.cc
+++ b/core2/Basetype2.cc
@@ -2480,7 +2480,7 @@ int Record_Of_Type::XER_decode(const XERdescriptor_t& p_td,
      * to decode the value. */
     for(char * str = strtok(val, " \t\x0A\x0D"); str != 0; str = strtok(val + pos, " \t\x0A\x0D")) {
       // Calling strtok with NULL won't work here, since the decoded element can have strtok calls aswell
-      pos += strlen(str) + 1;
+      pos = (str - val) + strlen(str) + 1;
       // Construct a new XML Reader with the current token.
       TTCN_Buffer buf2;
       const XERdescriptor_t& sub_xer = *p_td.oftype_descr;
diff --git a/regression_test/XML/EXER-whitepaper/List.ttcnpp b/regression_test/XML/EXER-whitepaper/List.ttcnpp
index de187c55b8be652efa7420dc0b08db4d60d32926..f2938031faad14baaf04fef6e19f7715a4d3793f 100644
--- a/regression_test/XML/EXER-whitepaper/List.ttcnpp
+++ b/regression_test/XML/EXER-whitepaper/List.ttcnpp
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *   Balasko, Jeno
+ *   Baranyi, Botond
  *   Raduly, Csaba
  *
  ******************************************************************************/
@@ -92,11 +93,38 @@ testcase decode_floats() runs on L
 
 /* * * * * * * * * * * * * * * * * * * * */
 
+// decoding a list with leading whitespaces (bug 540607)
+type record of universal charstring Strings
+with {
+  variant "list"
+}
+
+DECLARE_EXER_ENCODERS(Strings, strings);
+
+const Strings str := { "first", "second", "third", "fourth" }
+
+const universal charstring str_dec :=
+"<Strings> 
+   first
+     second
+       third
+         fourth
+ </Strings>";
+
+
+testcase decode_strings() runs on L
+{
+  CHECK_DECODE(exer_dec_strings, str_dec, Strings, str);
+}
+
+/* * * * * * * * * * * * * * * * * * * * */
+
 control {
   execute(encode_ints());
   execute(decode_ints());
   execute(encode_floats());
   execute(decode_floats());
+  execute(decode_strings());
 }
 
 }