[CMD] Fix substring-substitute regression from commit cdc8e45b (use signed offsets).

This commit is contained in:
Hermès Bélusca-Maïto 2020-09-28 00:30:18 +02:00
parent f2e3e8a1eb
commit f5ba9de2ee
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -1421,21 +1421,19 @@ do { \
} }
else if (*Src == _T('~')) else if (*Src == _T('~'))
{ {
/* %VAR:~[start][,length]% - substring /* %VAR:~[start][,length]% - Substring.
* Negative values are offsets from the end. * Negative values are offsets from the end.
*/ */
size_t Start = _tcstol(Src + 1, (PTSTR*)&Src, 0); SSIZE_T Start = _tcstol(Src + 1, (PTSTR*)&Src, 0);
size_t End = VarLength; SSIZE_T End = (SSIZE_T)VarLength;
if (Start < 0) if (Start < 0)
Start += VarLength; Start += VarLength;
Start = max(Start, 0); Start = min(max(Start, 0), VarLength);
Start = min(Start, VarLength);
if (*Src == _T(',')) if (*Src == _T(','))
{ {
End = _tcstol(Src + 1, (PTSTR*)&Src, 0); End = _tcstol(Src + 1, (PTSTR*)&Src, 0);
End += (End < 0) ? VarLength : Start; End += (End < 0) ? VarLength : Start;
End = max(End, Start); End = min(max(End, Start), VarLength);
End = min(End, VarLength);
} }
if (*Src++ != Delim) if (*Src++ != Delim)
goto bad_subst; goto bad_subst;