严格检查String::IndexOf参数和返回值

This commit is contained in:
Stone 2016-06-02 12:26:27 +00:00
parent 921f0553e1
commit 1b53d1bcf4
2 changed files with 14 additions and 0 deletions

View File

@ -750,6 +750,7 @@ String& String::Format(cstring format, ...)
int String::IndexOf(const char ch, int startIndex) const int String::IndexOf(const char ch, int startIndex) const
{ {
if(startIndex < 0) return -1;
if(startIndex >= _Length) return -1; if(startIndex >= _Length) return -1;
auto p = strchr(_Arr + startIndex, ch); auto p = strchr(_Arr + startIndex, ch);
@ -761,6 +762,7 @@ int String::IndexOf(const char ch, int startIndex) const
int String::IndexOf(const String& str, int startIndex) const int String::IndexOf(const String& str, int startIndex) const
{ {
if(str._Length == 0) return -1; if(str._Length == 0) return -1;
if(startIndex < 0) return -1;
if(startIndex + str._Length > _Length) return -1; if(startIndex + str._Length > _Length) return -1;
auto p = strstr(_Arr + startIndex, str._Arr); auto p = strstr(_Arr + startIndex, str._Arr);
@ -772,6 +774,7 @@ int String::IndexOf(const String& str, int startIndex) const
int String::IndexOf(cstring str, int startIndex) const int String::IndexOf(cstring str, int startIndex) const
{ {
if(!str) return -1; if(!str) return -1;
if(startIndex < 0) return -1;
if(startIndex + strlen(str) > _Length) return -1; if(startIndex + strlen(str) > _Length) return -1;
auto p = strstr(_Arr + startIndex, str); auto p = strstr(_Arr + startIndex, str);

View File

@ -442,6 +442,8 @@ port>]:<data>
*/ */
int Esp8266::ParseReceive(const Buffer& bs) const int Esp8266::ParseReceive(const Buffer& bs) const
{ {
TS("Esp8266::ParseReceive");
auto str = bs.AsString(); auto str = bs.AsString();
// +IPD开头的是收到网络数据 // +IPD开头的是收到网络数据
@ -450,21 +452,28 @@ int Esp8266::ParseReceive(const Buffer& bs) const
int s = str.IndexOf(",", p) + 1; int s = str.IndexOf(",", p) + 1;
int e = str.IndexOf(",", s); int e = str.IndexOf(",", s);
if(s == 0 || e < 0) return -1;
int idx = str.Substring(s, e - s).ToInt(); int idx = str.Substring(s, e - s).ToInt();
s = e + 1; s = e + 1;
e = str.IndexOf(",", s); e = str.IndexOf(",", s);
if(e < 0) return -1;
int len = str.Substring(s, e - s).ToInt(); int len = str.Substring(s, e - s).ToInt();
IPEndPoint ep; IPEndPoint ep;
s = e + 1; s = e + 1;
e = str.IndexOf(",", s); e = str.IndexOf(",", s);
if(e < 0) return -1;
ep.Address = IPAddress::Parse(str.Substring(s, e - s)); ep.Address = IPAddress::Parse(str.Substring(s, e - s));
s = e + 1; s = e + 1;
e = str.IndexOf(":", s); e = str.IndexOf(":", s);
if(e < 0) return -1;
ep.Port = str.Substring(s, e - s).ToInt(); ep.Port = str.Substring(s, e - s).ToInt();
// 后面是数据 // 后面是数据
@ -484,6 +493,8 @@ bool Esp8266::ParseExpect(const Buffer& bs)
{ {
if(!_Response) return false; if(!_Response) return false;
TS("Esp8266::ParseExpect");
// 适配任意关键字后,也就是收到了成功或失败,通知业务层已结束 // 适配任意关键字后,也就是收到了成功或失败,通知业务层已结束
auto str = bs.AsString(); auto str = bs.AsString();
// 适配第一关键字 // 适配第一关键字