fix tars proto, overflow bug

This commit is contained in:
ruanshudong 2020-06-02 17:08:48 +08:00
parent b7e9be5a5b
commit ee17821a2e
4 changed files with 1762 additions and 1756 deletions

View File

@ -37,9 +37,9 @@
//支持iphone
#ifdef __APPLE__
#include "TarsType.h"
#include "TarsType.h"
#elif defined ANDROID // android
#include "TarsType.h"
#include "TarsType.h"
#else
#include "tup/TarsType.h"
#endif
@ -355,58 +355,58 @@ do { \
namespace tars
{
//////////////////////////////////////////////////////////////////
struct TarsStructBase
{
protected:
struct TarsStructBase
{
protected:
TarsStructBase() {}
~TarsStructBase() {}
};
};
struct TarsProtoException : public std::runtime_error
{
struct TarsProtoException : public std::runtime_error
{
TarsProtoException(const std::string& s) : std::runtime_error(s) {}
};
};
struct TarsEncodeException : public TarsProtoException
{
struct TarsEncodeException : public TarsProtoException
{
TarsEncodeException(const std::string& s) : TarsProtoException(s) {}
};
};
struct TarsDecodeException : public TarsProtoException
{
struct TarsDecodeException : public TarsProtoException
{
TarsDecodeException(const std::string& s) : TarsProtoException(s) {}
};
};
struct TarsDecodeMismatch : public TarsDecodeException
{
struct TarsDecodeMismatch : public TarsDecodeException
{
TarsDecodeMismatch(const std::string & s) : TarsDecodeException(s) {}
};
};
struct TarsDecodeRequireNotExist : public TarsDecodeException
{
struct TarsDecodeRequireNotExist : public TarsDecodeException
{
TarsDecodeRequireNotExist(const std::string & s) : TarsDecodeException(s) {}
};
};
struct TarsDecodeInvalidValue : public TarsDecodeException
{
struct TarsDecodeInvalidValue : public TarsDecodeException
{
TarsDecodeInvalidValue(const std::string & s) : TarsDecodeException(s) {}
};
};
struct TarsNotEnoughBuff : public TarsProtoException
{
struct TarsNotEnoughBuff : public TarsProtoException
{
TarsNotEnoughBuff(const std::string & s) : TarsProtoException(s) {}
};
};
//////////////////////////////////////////////////////////////////
namespace
{
/// 数据头信息的封装包括类型和tag
class DataHead
{
namespace
{
/// 数据头信息的封装包括类型和tag
class DataHead
{
uint8_t _type;
uint8_t _tag;
public:
public:
enum
{
eChar = 0,
@ -425,14 +425,14 @@ namespace tars
eSimpleList = 13,
};
#pragma pack(1)
#pragma pack(1)
struct helper
{
uint8_t type : 4;
uint8_t tag : 4;
};
#pragma pack()
public:
#pragma pack()
public:
DataHead() : _type(0), _tag(0) {}
DataHead(uint8_t type, uint8_t tag) : _type(type), _tag(tag) {}
@ -506,25 +506,25 @@ namespace tars
os.writeBuf((const char *)&tag, sizeof(tag));
}
}
};
}
};
}
//////////////////////////////////////////////////////////////////
/// 缓冲区读取器封装
class BufferReader
{
private:
class BufferReader
{
private:
BufferReader(const BufferReader&);
BufferReader& operator=(const BufferReader&);
public:
public:
const char * _buf; ///< 缓冲区
size_t _buf_len; ///< 缓冲区长度
size_t _cur; ///< 当前位置
public:
public:
BufferReader() : _buf(NULL),_buf_len(0),_cur(0) {}
@ -627,6 +627,12 @@ namespace tars
*/
bool hasEnd()
{
if(_cur > _buf_len)
{
char s[64];
snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len);
throw TarsDecodeException(s);
}
return _cur >= _buf_len;
}
size_t tellp() const
@ -641,7 +647,7 @@ namespace tars
{
return _buf_len;
}
};
};
//当tars文件中含有指针型类型的数据用MapBufferReader读取
//在读数据时利用MapBufferReader提前分配的内存 减少运行过程中频繁内存分配
@ -649,10 +655,10 @@ namespace tars
//byte *m;
//指针类型使用时需要MapBufferReader提前设定预分配内存块setMapBuffer()
//指针需要内存时通过偏移指向预分配内存块,减少解码过程中的内存申请
class MapBufferReader : public BufferReader
{
class MapBufferReader : public BufferReader
{
public:
public:
MapBufferReader() : _buf_m(NULL),_buf_len_m(0),_cur_m(0) {}
void reset() { _cur_m = 0; BufferReader::reset();}
@ -698,17 +704,17 @@ namespace tars
_buf_len_m = buf.size();
_cur_m = 0;
}
public:
public:
char * _buf_m; ///< 缓冲区
size_t _buf_len_m; ///< 缓冲区长度
size_t _cur_m; ///< 当前位置
};
};
//////////////////////////////////////////////////////////////////
/// 缓冲区写入器封装
class BufferWriter
{
public:
class BufferWriter
{
public:
char * _buf;
size_t _len;
size_t _buf_len;
@ -722,11 +728,11 @@ namespace tars
return p;
}
private:
private:
BufferWriter(const BufferWriter & bw);
BufferWriter& operator=(const BufferWriter& buf);
public:
public:
BufferWriter()
: _buf(NULL)
, _len(0)
@ -755,26 +761,26 @@ namespace tars
std::swap(_buf_len, buf._buf_len);
std::swap(_len, buf._len);
}
};
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// 实际buffer是std::string
/// 可以swap, 把buffer交换出来, 避免一次内存copy
class BufferWriterString
{
protected:
class BufferWriterString
{
protected:
mutable std::string _buffer;
char * _buf;
size_t _len;
size_t _buf_len;
std::function<char*(BufferWriterString &, size_t)> _reserve;
private:
private:
//不让copy 复制
BufferWriterString(const BufferWriterString&);
BufferWriterString& operator=(const BufferWriterString& buf);
public:
public:
BufferWriterString()
: _buf(NULL)
, _len(0)
@ -829,25 +835,25 @@ namespace tars
std::swap(_buf_len, buf._buf_len);
std::swap(_len, buf._len);
}
};
};
/// 实际buffer是std::vector<char>
/// 可以swap, 把buffer交换出来, 避免一次内存copy
class BufferWriterVector
{
protected:
class BufferWriterVector
{
protected:
mutable std::vector<char> _buffer;
char * _buf;
size_t _len;
size_t _buf_len;
std::function<char*(BufferWriterVector &, size_t)> _reserve;
private:
private:
//不让copy 复制
BufferWriterVector(const BufferWriterVector&);
BufferWriterVector& operator=(const BufferWriterVector& buf);
public:
public:
BufferWriterVector()
: _buf(NULL)
, _len(0)
@ -902,13 +908,13 @@ namespace tars
std::swap(_buf_len, buf._buf_len);
std::swap(_len, buf._len);
}
};
};
//////////////////////////////////////////////////////////////////
template<typename ReaderT = BufferReader>
class TarsInputStream : public ReaderT
{
public:
template<typename ReaderT = BufferReader>
class TarsInputStream : public ReaderT
{
public:
/// 跳到指定标签的元素前
bool skipToTag(uint8_t tag)
@ -991,7 +997,7 @@ namespace tars
break;
case TarsHeadeMap:
{
Int32 size = 0;
UInt32 size = 0;
read(size, 0);
for (Int32 i = 0; i < size * 2; ++i)
skipField();
@ -999,7 +1005,7 @@ namespace tars
break;
case TarsHeadeList:
{
Int32 size = 0;
UInt32 size = 0;
read(size, 0);
for (Int32 i = 0; i < size; ++i)
skipField();
@ -1015,7 +1021,7 @@ namespace tars
snprintf(s, sizeof(s), "skipField with invalid type, type value: %d, %d, %d.", type, headType, headTag);
throw TarsDecodeMismatch(s);
}
Int32 size = 0;
UInt32 size = 0;
read(size, 0);
TarsReadHeadSkip(*this, size);
}
@ -1711,13 +1717,13 @@ namespace tars
throw TarsDecodeRequireNotExist(s);
}
}
};
};
//////////////////////////////////////////////////////////////////
template<typename WriterT = BufferWriter>
class TarsOutputStream : public WriterT
{
public:
template<typename WriterT = BufferWriter>
class TarsOutputStream : public WriterT
{
public:
void writeUnknown(const std::string& s)
{
this->writeBuf(s.data(), s.size());
@ -1954,15 +1960,15 @@ namespace tars
h.writeTo(*this);
*/
}
};
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
//支持iphone
#ifdef __APPLE__
#include "TarsDisplayer.h"
#include "TarsDisplayer.h"
#else
#include "tup/TarsDisplayer.h"
#include "tup/TarsDisplayer.h"
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@ -513,7 +513,7 @@ int yy_flex_debug = 0;
#define YY_MORE_ADJ 0
#define YY_RESTORE_YY_MORE_OFFSET
char *yytext;
#line 1 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 1 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
/**
* Tencent is pleased to support the open source community by making Tars available.
*
@ -529,7 +529,7 @@ char *yytext;
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#line 20 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 20 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#include <map>
#include <string>
#include <sstream>
@ -742,7 +742,7 @@ YY_DECL
register char *yy_cp, *yy_bp;
register int yy_act;
#line 67 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 67 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 749 "tars.lex.cpp"
@ -840,12 +840,12 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
#line 69 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 69 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{ BEGIN(INCL); }
YY_BREAK
case 2:
YY_RULE_SETUP
#line 71 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 71 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
if ( include_file_stack_ptr >= MAX_INCLUDE_DEPTH )
{
@ -878,7 +878,7 @@ YY_RULE_SETUP
YY_BREAK
case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(INCL):
#line 101 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 101 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
--include_file_stack_ptr;
if ( include_file_stack_ptr < 0 )
@ -897,14 +897,14 @@ case YY_STATE_EOF(INCL):
YY_BREAK
case 3:
YY_RULE_SETUP
#line 117 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 117 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
return TARS_SCOPE_DELIMITER;
}
YY_BREAK
case 4:
YY_RULE_SETUP
#line 121 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 121 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
// C++ comment
bool e = false;
@ -925,7 +925,7 @@ YY_RULE_SETUP
YY_BREAK
case 5:
YY_RULE_SETUP
#line 139 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 139 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
// C comment
bool e = false;
@ -976,7 +976,7 @@ YY_RULE_SETUP
YY_BREAK
case 6:
YY_RULE_SETUP
#line 187 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 187 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
StringGrammarPtr ident = new StringGrammar;
ident->v = yytext;
@ -987,7 +987,7 @@ YY_RULE_SETUP
case 7:
/* rule 7 can match eol */
YY_RULE_SETUP
#line 194 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 194 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
StringGrammarPtr ident = new StringGrammar;
ident->v = yytext;
@ -1000,7 +1000,7 @@ YY_RULE_SETUP
YY_BREAK
case 8:
YY_RULE_SETUP
#line 204 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 204 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
StringGrammarPtr str = new StringGrammar;
bool e = false;
@ -1115,7 +1115,7 @@ YY_RULE_SETUP
YY_BREAK
case 9:
YY_RULE_SETUP
#line 316 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 316 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
errno = 0;
IntergerGrammarPtr ptr = new IntergerGrammar;
@ -1140,7 +1140,7 @@ YY_RULE_SETUP
YY_BREAK
case 10:
YY_RULE_SETUP
#line 338 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 338 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
errno = 0;
FloatGrammarPtr ptr = new FloatGrammar;
@ -1175,7 +1175,7 @@ YY_RULE_SETUP
case 11:
/* rule 11 can match eol */
YY_RULE_SETUP
#line 369 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 369 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
if(yytext[0] == '\n')
{
@ -1185,7 +1185,7 @@ YY_RULE_SETUP
YY_BREAK
case 12:
YY_RULE_SETUP
#line 376 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 376 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
if(yytext[0] < 32 || yytext[0] > 126)
{
@ -1204,7 +1204,7 @@ YY_RULE_SETUP
YY_BREAK
case 13:
YY_RULE_SETUP
#line 392 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 392 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
ECHO;
YY_BREAK
#line 1211 "tars.lex.cpp"
@ -2214,7 +2214,7 @@ void yyfree (void * ptr )
#define YYTABLES_NAME "yytables"
#line 392 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 392 "/Users/jarod/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"

File diff suppressed because it is too large Load Diff