Replaced some exceptions in MTProto code with t_assert()s.

This commit is contained in:
John Preston 2016-10-01 23:29:32 +03:00
parent 4a5f467560
commit 4bdb2c48c7
4 changed files with 824 additions and 1386 deletions

View File

@ -44,17 +44,17 @@ public:
}
uint32 getDC() const {
if (!_isset) throw mtpErrorKeyNotReady("getDC()");
t_assert(_isset);
return _dc;
}
uint64 keyId() const {
if (!_isset) throw mtpErrorKeyNotReady("keyId()");
t_assert(_isset);
return _keyId;
}
void prepareAES(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send = true) const {
if (!_isset) throw mtpErrorKeyNotReady(QString("prepareAES(..., %1)").arg(Logs::b(send)));
t_assert(_isset);
uint32 x = send ? 0 : 8;
@ -90,7 +90,7 @@ public:
}
void write(QDataStream &to) const {
if (!_isset) throw mtpErrorKeyNotReady("write(...)");
t_assert(_isset);
to.writeRawData(_key, 256);
}

View File

@ -199,30 +199,12 @@ public:
}
};
class mtpErrorUninitialized : public Exception {
public:
mtpErrorUninitialized() : Exception("MTP Uninitialized variable write attempt") {
}
};
class mtpErrorBadTypeId : public Exception {
public:
mtpErrorBadTypeId(mtpTypeId typeId, const QString &type) : Exception(QString("MTP Bad type id %1 passed to constructor of %2").arg(typeId).arg(type)) {
}
};
class mtpErrorWrongTypeId : public Exception {
public:
mtpErrorWrongTypeId(mtpTypeId typeId, mtpTypeId required) : Exception(QString("MTP Wrong type id %1 for this data conversion, must be %2").arg(typeId).arg(required)) {
}
};
class mtpErrorKeyNotReady : public Exception {
public:
mtpErrorKeyNotReady(const QString &method) : Exception(QString("MTP Auth key is used in %1 without being created").arg(method)) {
}
};
class mtpData {
public:
mtpData() : cnt(1) {
@ -686,12 +668,12 @@ public:
}
MTPDstring &_string() {
if (!data) throw mtpErrorUninitialized();
t_assert(data != nullptr);
split();
return *(MTPDstring*)data;
}
const MTPDstring &c_string() const {
if (!data) throw mtpErrorUninitialized();
t_assert(data != nullptr);
return *(const MTPDstring*)data;
}
@ -823,12 +805,12 @@ public:
}
MTPDvector<T> &_vector() {
if (!data) throw mtpErrorUninitialized();
t_assert(data != nullptr);
split();
return *(MTPDvector<T>*)data;
}
const MTPDvector<T> &c_vector() const {
if (!data) throw mtpErrorUninitialized();
t_assert(data != nullptr);
return *(const MTPDvector<T>*)data;
}

View File

@ -22,7 +22,7 @@ import glob
import re
import binascii
# define some checked flag convertions
# define some checked flag conversions
# the key flag type should be a subset of the value flag type
# with exact the same names, then the key flag can be implicitly
# casted to the value flag type
@ -612,17 +612,19 @@ for restype in typesList:
withData = 1;
getters += '\n\tMTPD' + name + ' &_' + name + '() {\n'; # splitting getter
getters += '\t\tif (!data) throw mtpErrorUninitialized();\n';
if (withType):
getters += '\t\tif (_type != mtpc_' + name + ') throw mtpErrorWrongTypeId(_type, mtpc_' + name + ');\n';
getters += '\t\tt_assert(data != nullptr && _type == mtpc_' + name + ');\n';
else:
getters += '\t\tt_assert(data != nullptr);\n';
getters += '\t\tsplit();\n';
getters += '\t\treturn *(MTPD' + name + '*)data;\n';
getters += '\t}\n';
getters += '\tconst MTPD' + name + ' &c_' + name + '() const {\n'; # const getter
getters += '\t\tif (!data) throw mtpErrorUninitialized();\n';
if (withType):
getters += '\t\tif (_type != mtpc_' + name + ') throw mtpErrorWrongTypeId(_type, mtpc_' + name + ');\n';
getters += '\t\tt_assert(data != nullptr && _type == mtpc_' + name + ');\n';
else:
getters += '\t\tt_assert(data != nullptr);\n';
getters += '\t\treturn *(const MTPD' + name + '*)data;\n';
getters += '\t}\n';
@ -783,7 +785,7 @@ for restype in typesList:
typesText += '\tmtpTypeId type() const;\n'; # type id method
inlineMethods += 'inline mtpTypeId MTP' + restype + '::type() const {\n';
if (withType):
inlineMethods += '\tif (!_type) throw mtpErrorUninitialized();\n';
inlineMethods += '\tt_assert(_type != 0);\n';
inlineMethods += '\treturn _type;\n';
else:
inlineMethods += '\treturn mtpc_' + v[0][0] + ';\n';

File diff suppressed because it is too large Load Diff