sip: both framers read a Content-Length as a number
DatagramMessage read the first nine characters of a Content-Length, the zeros before its number among them: a length written 0000000005 came out as 0, and the body after it was cut off as bytes past the message. FrameBuffer read the value into a long, and one too long for that counted as no Content-Length: the body was framed as the start of the next message. RFC 3261 25.1 writes the value as 1*DIGIT. LengthOf reads it for both: the number its leading digits spell, zeros before it aside; of a number of more than nine digits the first nine, more than a datagram holds and enough that a stream waits. A value that begins with no digit is no length, as before. Tested by tests/Sip through both framers: a length of one digit written with ten zeros before it frames that many bytes; a twenty-digit length that wraps to zero in 64 bits is a datagram cut short and a stream's body waited for; a value without a leading digit leaves a datagram's body the rest of it and a stream's empty.
This commit is contained in:
parent
78c702546a
commit
c81dd3d267
3 changed files with 40 additions and 10 deletions
|
|
@ -137,6 +137,20 @@ namespace imsd::sip {
|
|||
return v;
|
||||
}
|
||||
|
||||
// The length a Content-Length value names: the number its leading
|
||||
// digits spell (RFC 3261 25.1: 1*DIGIT), zeros before it aside; nullopt
|
||||
// where the value begins with no digit. Of a number of more than nine
|
||||
// digits the first nine are read — more than a datagram holds, and
|
||||
// enough that a stream waits for a body that does not come.
|
||||
std::optional<std::size_t> LengthOf(std::string_view v) {
|
||||
std::string_view digits = v.substr(0, v.find_first_not_of("0123456789"));
|
||||
if (digits.empty()) return std::nullopt;
|
||||
while (digits.starts_with('0')) digits.remove_prefix(1);
|
||||
std::size_t n = 0;
|
||||
for (char c : digits.substr(0, 9)) n = n * 10 + static_cast<std::size_t>(c - '0');
|
||||
return n;
|
||||
}
|
||||
|
||||
// A CSeq value as its sequence number and its method (RFC 3261 25.1:
|
||||
// 1*DIGIT LWS Method): the digits the trimmed value begins with, less
|
||||
// the zeros before the number, the spaces or tabs after them, then the
|
||||
|
|
@ -432,7 +446,7 @@ export namespace imsd::sip {
|
|||
if (idx == std::string::npos) return std::nullopt;
|
||||
std::string_view head = std::string_view(buf_).substr(0, idx);
|
||||
long clen = 0;
|
||||
if (auto v = Header(head, "Content-Length")) clen = LeadingDigits(*v).value_or(0);
|
||||
if (auto v = Header(head, "Content-Length")) clen = static_cast<long>(LengthOf(*v).value_or(0));
|
||||
std::size_t total = idx + 4 + static_cast<std::size_t>(clen);
|
||||
if (buf_.size() < total) return std::nullopt;
|
||||
std::string msg = buf_.substr(0, total);
|
||||
|
|
@ -465,9 +479,8 @@ export namespace imsd::sip {
|
|||
std::size_t bodyStart = idx + 4;
|
||||
std::size_t body = d.size() - bodyStart;
|
||||
if (auto v = Header(d.substr(0, idx), "Content-Length")) {
|
||||
std::size_t clen = 0; int digits = 0;
|
||||
for (char c : *v) { if (c < '0' || c > '9' || digits == 9) break; clen = clen * 10 + static_cast<std::size_t>(c - '0'); digits++; }
|
||||
if (digits) {
|
||||
if (const auto length = LengthOf(*v)) {
|
||||
const std::size_t clen = *length;
|
||||
if (clen > body) {
|
||||
if (problem) *problem = std::format("datagram truncated (Content-Length {} > {} body bytes)", clen, body);
|
||||
return std::nullopt;
|
||||
|
|
|
|||
Loading…
Reference in a new issue