fix an undefined behavior in uint::SetHex
Decrementing psz beyond the beginning of the string is UB, even though the out-of-bounds pointer is never dereferenced.
This commit is contained in:
parent
e74649e951
commit
0f459d868d
1 changed files with 7 additions and 8 deletions
|
@ -37,16 +37,15 @@ void base_blob<BITS>::SetHex(const char* psz)
|
||||||
psz += 2;
|
psz += 2;
|
||||||
|
|
||||||
// hex string to uint
|
// hex string to uint
|
||||||
const char* pbegin = psz;
|
size_t digits = 0;
|
||||||
while (::HexDigit(*psz) != -1)
|
while (::HexDigit(psz[digits]) != -1)
|
||||||
psz++;
|
digits++;
|
||||||
psz--;
|
|
||||||
unsigned char* p1 = (unsigned char*)data;
|
unsigned char* p1 = (unsigned char*)data;
|
||||||
unsigned char* pend = p1 + WIDTH;
|
unsigned char* pend = p1 + WIDTH;
|
||||||
while (psz >= pbegin && p1 < pend) {
|
while (digits > 0 && p1 < pend) {
|
||||||
*p1 = ::HexDigit(*psz--);
|
*p1 = ::HexDigit(psz[--digits]);
|
||||||
if (psz >= pbegin) {
|
if (digits > 0) {
|
||||||
*p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
|
*p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4);
|
||||||
p1++;
|
p1++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue