Explorar o código

Fixed media description m-line parsing

Paul-Louis Ageneau %!s(int64=2) %!d(string=hai) anos
pai
achega
08499940bd
Modificáronse 1 ficheiros con 16 adicións e 4 borrados
  1. 16 4
      src/description.cpp

+ 16 - 4
src/description.cpp

@@ -44,6 +44,13 @@ inline void trim_end(string &str) {
 	    str.end());
 	    str.end());
 }
 }
 
 
+inline string get_first_line(const string &str) {
+	string line;
+	std::istringstream ss(str);
+	std::getline(ss, line);
+	return line;
+}
+
 inline std::pair<string_view, string_view> parse_pair(string_view attr) {
 inline std::pair<string_view, string_view> parse_pair(string_view attr) {
 	string_view key, value;
 	string_view key, value;
 	if (size_t separator = attr.find(':'); separator != string::npos) {
 	if (size_t separator = attr.find(':'); separator != string::npos) {
@@ -523,12 +530,15 @@ unsigned int Description::mediaCount() const { return unsigned(mEntries.size());
 Description::Entry::Entry(const string &mline, string mid, Direction dir)
 Description::Entry::Entry(const string &mline, string mid, Direction dir)
     : mMid(std::move(mid)), mDirection(dir) {
     : mMid(std::move(mid)), mDirection(dir) {
 
 
-	uint16_t port;
-	std::istringstream ss(mline);
+	uint16_t port = 0;
+	std::istringstream ss(match_prefix(mline, "m=") ? mline.substr(2) : mline);
 	ss >> mType;
 	ss >> mType;
 	ss >> port;
 	ss >> port;
 	ss >> mDescription;
 	ss >> mDescription;
 
 
+	if (mType.empty() || mDescription.empty())
+		throw std::invalid_argument("Invalid media description line");
+
 	// RFC 3264: Existing media streams are removed by creating a new SDP with the port number for
 	// RFC 3264: Existing media streams are removed by creating a new SDP with the port number for
 	// that stream set to zero.
 	// that stream set to zero.
 	// RFC 8843: If the offerer assigns a zero port value to a bundled "m=" section, but does not
 	// RFC 8843: If the offerer assigns a zero port value to a bundled "m=" section, but does not
@@ -866,10 +876,12 @@ void Description::Application::parseSdpLine(string_view line) {
 	}
 	}
 }
 }
 
 
-Description::Media::Media(const string &sdp) : Entry(sdp, "", Direction::Unknown) {
+Description::Media::Media(const string &sdp)
+    : Entry(get_first_line(sdp), "", Direction::Unknown) {
+	string line;
 	std::istringstream ss(sdp);
 	std::istringstream ss(sdp);
+	std::getline(ss, line); // discard first line
 	while (ss) {
 	while (ss) {
-		string line;
 		std::getline(ss, line);
 		std::getline(ss, line);
 		trim_end(line);
 		trim_end(line);
 		if (line.empty())
 		if (line.empty())