diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-12-23 13:08:24 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-12-23 13:08:24 -0800 |
commit | 781ac0929c76862c9988867a731b4699b459f5fd (patch) | |
tree | 79cb023794ca30f01a1d3361a2fd19552acc1c31 /src | |
parent | e06c85acfe7332640aadfea2c4f62ab71d159a88 (diff) | |
download | binaryen-781ac0929c76862c9988867a731b4699b459f5fd.tar.gz binaryen-781ac0929c76862c9988867a731b4699b459f5fd.tar.bz2 binaryen-781ac0929c76862c9988867a731b4699b459f5fd.zip |
fix s-parser handling of memory segments with 0s
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-s-parser.h | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 55b4a20aa..4817fdf48 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -187,34 +187,16 @@ private: } char *start = input; if (input[0] == '"') { - // parse escaping \", and \a7 into 0xa7 the character code + // parse escaping \", but leave code escaped - we'll handle escaping in memory segments specifically input++; std::string str; while (1) { if (input[0] == '"') break; if (input[0] == '\\') { - if (input[1] == '"') { - str += '"'; - input += 2; - continue; - } else if (input[1] == '\'') { - str += '\''; - input += 2; - continue; - } else if (input[1] == '\\') { - str += '\\'; - input += 2; - } else if (input[1] == 'n') { - str += '\n'; - input += 2; - } else if (input[1] == 't') { - str += '\t'; - input += 2; - } else { - str += (char)(unhex(input[1])*16 + unhex(input[2])); - input += 3; - continue; - } + str += input[0]; + str += input[1]; + input += 2; + continue; } str += input[0]; input++; @@ -965,8 +947,39 @@ private: while (i < s.size()) { Element& curr = *s[i]; assert(curr[0]->str() == SEGMENT); - char *data = strdup(curr[2]->c_str()); // TODO: handle non-null-terminated? - wasm.memory.segments.emplace_back(atoi(curr[1]->c_str()), data, strlen(data)); + const char *input = curr[2]->c_str(); + char *data = (char*)malloc(strlen(input)); // over-allocated, since escaping collapses, but whatever + char *write = data; + while (1) { + if (input[0] == 0) break; + if (input[0] == '\\') { + if (input[1] == '"') { + *write++ = '"'; + input += 2; + continue; + } else if (input[1] == '\'') { + *write++ = '\''; + input += 2; + continue; + } else if (input[1] == '\\') { + *write++ = '\\'; + input += 2; + } else if (input[1] == 'n') { + *write++ = '\n'; + input += 2; + } else if (input[1] == 't') { + *write++ = '\t'; + input += 2; + } else { + *write++ = (char)(unhex(input[1])*16 + unhex(input[2])); + input += 3; + continue; + } + } + *write++ = input[0]; + input++; + } + wasm.memory.segments.emplace_back(atoi(curr[1]->c_str()), data, write - data); i++; } } |