doFuncReplace(): add tail of src if tail is partially similar to 'replaceWith'

if the tail of message similar to replacement string. Example:
msg: 1234567890
replacement: 8906
the last N (amount of similar symbols in tail of message and the begin of replacement,
in current example = 3) will never be assigned in dst message. And will contain a garbage.
As longer replacement string as more garbage will be in dst message instead of a required
src tail.

The length of dst message is right. The tail of dst message filled with garbage.
Because of memory for dst message is allocated with right size, but didn't filled if
the tail of message partially equals to a beginning of a replacement string.
This commit is contained in:
baya 2018-01-03 01:06:10 +02:00
parent 57bde72767
commit 9c7413beb7

View File

@ -1636,35 +1636,34 @@ doFuncReplace(struct svar *__restrict__ const operandVal, struct svar *__restric
uchar *replaceWith = es_getBufAddr(replaceWithStr);
uint lfind = es_strlen(findStr);
uint lReplaceWith = es_strlen(replaceWithStr);
uint size = 0;
uint lSrc = es_strlen(str);
uint lDst = 0;
uchar* src_buff = es_getBufAddr(str);
uint i, j;
for(i = j = 0; i <= es_strlen(str); i++, size++) {
for(i = j = 0; i <= lSrc; i++, lDst++) {
if (j == lfind) {
size = size - lfind + lReplaceWith;
lDst = lDst - lfind + lReplaceWith;
j = 0;
}
if (i == es_strlen(str)) break;
if (i == lSrc) break;
if (src_buff[i] == find[j]) {
j++;
} else if (j > 0) {
i -= (j - 1);
size -= (j - 1);
lDst -= (j - 1);
j = 0;
}
}
es_str_t *res = es_newStr(size);
es_str_t *res = es_newStr(lDst);
unsigned char* dest = es_getBufAddr(res);
uint k, s;
for(i = j = k = s = 0; i <= es_strlen(str); i++, s++) {
for(i = j = s = 0; i <= lSrc; i++, s++) {
if (j == lfind) {
for (k = 0; k < lReplaceWith; k++) {
dest[s - j + k] = replaceWith[k];
}
s = s - j + lReplaceWith;
s -= j;
for (k = 0; k < lReplaceWith; k++, s++) dest[s] = replaceWith[k];
j = 0;
}
if (i == es_strlen(str)) break;
if (i == lSrc) break;
if (src_buff[i] == find[j]) {
j++;
} else {
@ -1676,7 +1675,10 @@ doFuncReplace(struct svar *__restrict__ const operandVal, struct svar *__restric
dest[s] = src_buff[i];
}
}
res->lenStr = size;
if (j > 0) {
for (k = 1; k <= j; k++) dest[s - k] = src_buff[i - k];
}
res->lenStr = lDst;
if(freeOperand) es_deleteStr(str);
if(freeFind) es_deleteStr(findStr);
if(freeReplacement) es_deleteStr(replaceWithStr);