Skip to content

Commit

Permalink
Fix possible segfault when deleting QNetworkReply instance. Make some…
Browse files Browse the repository at this point in the history
… local variables const.
  • Loading branch information
marxoft committed Nov 8, 2015
1 parent e76a88d commit 44b1103
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/authenticationrequest.cpp
Expand Up @@ -63,8 +63,8 @@ class AuthenticationRequestPrivate : public RequestPrivate
bool ok;
setResult(QtJson::Json::parse(reply->readAll(), ok));

QNetworkReply::NetworkError e = reply->error();
QString es = reply->errorString();
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
reply->deleteLater();
reply = 0;

Expand Down
10 changes: 5 additions & 5 deletions src/request.cpp
Expand Up @@ -896,8 +896,8 @@ void RequestPrivate::_q_onAccessTokenRefreshed() {
bool ok;
setResult(QtJson::Json::parse(reply->readAll(), ok));

QNetworkReply::NetworkError e = reply->error();
QString es = reply->errorString();
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
reply->deleteLater();
reply = 0;

Expand Down Expand Up @@ -979,11 +979,11 @@ void RequestPrivate::_q_onReplyFinished() {
}

bool ok = true;
QString response = QString::fromUtf8(reply->readAll());
const QString response = QString::fromUtf8(reply->readAll());
setResult(response.isEmpty() ? response : QtJson::Json::parse(response, ok));

QNetworkReply::NetworkError e = reply->error();
QString es = reply->errorString();
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
reply->deleteLater();
reply = 0;

Expand Down
51 changes: 34 additions & 17 deletions src/streamsrequest.cpp
Expand Up @@ -292,6 +292,9 @@ static QString unescape(const QString &s) {
}

void extractVideoStreams(QScriptValue decryptionFunction) {
#ifdef QYOUTUBE_DEBUG
qDebug() << "QYouTube::StreamsRequestPrivate::extractVideoStreams: Extracting video streams.";
#endif
Q_Q(StreamsRequest);

QVariantList formats;
Expand Down Expand Up @@ -355,7 +358,13 @@ static QString unescape(const QString &s) {

Q_Q(StreamsRequest);

switch (reply->error()) {
response = reply->readAll();
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
reply->deleteLater();
reply = 0;

switch (e) {
case QNetworkReply::NoError:
break;
case QNetworkReply::OperationCanceledError:
Expand All @@ -366,14 +375,12 @@ static QString unescape(const QString &s) {
return;
default:
setStatus(StreamsRequest::Failed);
setError(StreamsRequest::Error(reply->error()));
setErrorString(reply->errorString());
setError(StreamsRequest::Error(e));
setErrorString(es);
emit q->finished();
return;
}

response = reply->readAll();


if (!response.contains("url_encoded_fmt_stream_map=")) {
#ifdef QYOUTUBE_DEBUG
qDebug() << "QYouTube::StreamsRequestPrivate::_q_onVideoInfoLoaded: No format map in video info page. \
Expand Down Expand Up @@ -409,7 +416,13 @@ static QString unescape(const QString &s) {

Q_Q(StreamsRequest);

switch (reply->error()) {
response = reply->readAll();
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
reply->deleteLater();
reply = 0;

switch (e) {
case QNetworkReply::NoError:
break;
case QNetworkReply::OperationCanceledError:
Expand All @@ -420,14 +433,12 @@ static QString unescape(const QString &s) {
return;
default:
setStatus(StreamsRequest::Failed);
setError(StreamsRequest::Error(reply->error()));
setErrorString(reply->errorString());
setError(StreamsRequest::Error(e));
setErrorString(es);
emit q->finished();
return;
}

response = reply->readAll();


if (response.contains("url_encoded_fmt_stream_map\":")) {
QString js = response.section("\"assets\":", 1, 1).section('}', 0, 0) + "}";
response = response.section("url_encoded_fmt_stream_map\":\"", 1, 1).section(",\"", 0, 0)
Expand Down Expand Up @@ -474,7 +485,14 @@ static QString unescape(const QString &s) {

Q_Q(StreamsRequest);

switch (reply->error()) {
const QString jsresponse(reply->readAll());
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
const QUrl playerUrl = reply->request().url();
reply->deleteLater();
reply = 0;

switch (e) {
case QNetworkReply::NoError:
break;
case QNetworkReply::OperationCanceledError:
Expand All @@ -485,13 +503,12 @@ static QString unescape(const QString &s) {
return;
default:
setStatus(StreamsRequest::Failed);
setError(StreamsRequest::Error(reply->error()));
setErrorString(reply->errorString());
setError(StreamsRequest::Error(e));
setErrorString(es);
emit q->finished();
return;
}

QString jsresponse(reply->readAll());
QRegExp re("\\.sig\\|\\|[\\w\\$]+(?=\\()");

if (re.indexIn(jsresponse) != -1) {
Expand All @@ -509,7 +526,7 @@ static QString unescape(const QString &s) {
QScriptValue decryptionFunction = global.property(funcName);

if (decryptionFunction.isFunction()) {
decryptionCache[reply->request().url()] = decryptionFunction;
decryptionCache[playerUrl] = decryptionFunction;
extractVideoStreams(decryptionFunction);
return;
}
Expand Down
23 changes: 16 additions & 7 deletions src/subtitlesrequest.cpp
Expand Up @@ -47,11 +47,20 @@ class SubtitlesRequestPrivate : public RequestPrivate
}

if (!redirect.isEmpty()) {
reply->deleteLater();
reply = 0;
followRedirect(redirect);
}
}

switch (reply->error()) {

const QByteArray response = reply->readAll();
const QNetworkReply::NetworkError e = reply->error();
const QString es = reply->errorString();
const QUrl subtitlesUrl = reply->request().url();
reply->deleteLater();
reply = 0;

switch (e) {
case QNetworkReply::NoError:
break;
case QNetworkReply::OperationCanceledError:
Expand All @@ -62,14 +71,14 @@ class SubtitlesRequestPrivate : public RequestPrivate
return;
default:
setStatus(Request::Failed);
setError(Request::Error(reply->error()));
setErrorString(reply->errorString());
setError(Request::Error(e));
setErrorString(es);
emit q->finished();
return;
}

QDomDocument doc;
doc.setContent(reply->readAll());
doc.setContent(response);
QDomElement transcriptEl = doc.documentElement();
QDomNodeList trackNodes = transcriptEl.elementsByTagName("track");
QVariantList subs;
Expand All @@ -81,11 +90,11 @@ class SubtitlesRequestPrivate : public RequestPrivate
QString code = trackEl.attribute("lang_code");
#if QT_VERSION >= 0x050000
QUrlQuery query(u);
query.addQueryItem("v", QUrlQuery(reply->request().url()).queryItemValue("v"));
query.addQueryItem("v", QUrlQuery(subtitlesUrl).queryItemValue("v"));
query.addQueryItem("lang", code);
u.setQuery(query);
#else
u.addQueryItem("v", reply->request().url().queryItemValue("v"));
u.addQueryItem("v", subtitlesUrl.queryItemValue("v"));
u.addQueryItem("lang", code);
#endif
sub["id"] = trackEl.attribute("id");
Expand Down

0 comments on commit 44b1103

Please sign in to comment.