inline char* findSubStr(char* targetData, int dataLen, const char* subStr, int subStrLen){
if (dataLen < subStrLen || dataLen <= 0 || subStrLen <= 0){
return NULL;
}
char lastChar = targetData[dataLen - 1];
targetData[dataLen - 1] = '\0';
char* pFind = NULL;
pFind = strstr(targetData, subStr);
targetData[dataLen - 1] = lastChar;
// 纯二进制下,尾部是否一致
if (!pFind){
bool hasNotSameChar = false;
for (int i = 0; i < subStrLen; i++){
if (subStr[i] != targetData[dataLen - subStrLen + i]){
hasNotSameChar = true;
break;
}
}
if (!hasNotSameChar){
pFind = &targetData[dataLen - subStrLen];
}
}
return pFind;
}
由于strstr查找的目标地址可能不是以'\0'结束,这样就可能有越界的风险,上面代码则不会越界。