diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-17 23:28:28 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-17 23:28:28 +0900 |
| commit | f316c947100f7683f4ca9ba1c71bb1446433dbb4 (patch) | |
| tree | 185ac72d1440f26570b52ba0fb4942deb792b545 /src/BencodeParser.cc | |
| parent | 9b607bbae0b397fffc53339aae36e9f8547cfd12 (diff) | |
Ignore floating number in torrent file
Diffstat (limited to 'src/BencodeParser.cc')
| -rw-r--r-- | src/BencodeParser.cc | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/BencodeParser.cc b/src/BencodeParser.cc index f5423072..08d36b21 100644 --- a/src/BencodeParser.cc +++ b/src/BencodeParser.cc @@ -55,7 +55,8 @@ enum { BENCODE_STRING_LEN, BENCODE_STRING, BENCODE_NUMBER_SIGN, - BENCODE_NUMBER + BENCODE_NUMBER, + BENCODE_FLOAT_NUMBER_IGNORE, }; } // namespace @@ -216,11 +217,32 @@ ssize_t BencodeParser::parseUpdate(const char* data, size_t size) } numConsumed_ += j - i; if (j != size) { - if (data[j] != 'e' || numConsumed_ == 0) { + if (numConsumed_ == 0) { currentState_ = BENCODE_ERROR; return lastError_ = ERR_INVALID_NUMBER; } + + auto c = data[j]; + if (util::isDigit(c) || c == '.' || c == 'E' || c == '+' || c == '-') { + // some torrent generator adds floating point number in + // scientific notation (e.g., -1.134E+3) in integer field. + // In this case, just skip these bytes until we find 'e'. + number_ = 0; + numConsumed_ = 0; + currentState_ = BENCODE_FLOAT_NUMBER_IGNORE; + + i = j; + + break; + } + + if (c != 'e') { + currentState_ = BENCODE_ERROR; + return lastError_ = ERR_INVALID_NUMBER; + } + i = j; + onNumberEnd(); } else { @@ -228,6 +250,20 @@ ssize_t BencodeParser::parseUpdate(const char* data, size_t size) } break; } + case BENCODE_FLOAT_NUMBER_IGNORE: { + auto c = data[i]; + if (util::isDigit(c) || c == '.' || c == 'E' || c == '+' || c == '-') { + continue; + } + + if (c != 'e') { + currentState_ = BENCODE_ERROR; + return lastError_ = ERR_INVALID_FLOAT_NUMBER; + } + + onNumberEnd(); + break; + } } } return i; |
