Fix string init with 0 sized literal crash

This commit is contained in:
Doyle Thai 2018-01-25 18:55:05 +11:00
parent b8aee7cef9
commit 47bf005dbf

14
dqn.h
View File

@ -5474,13 +5474,19 @@ bool DqnString::InitSize(i32 size, DqnMemAPI *const api)
} }
else else
#endif #endif
if (size == 0)
{
this->str = nullptr;
}
else
{ {
size_t allocSize = sizeof(*(this->str)) * (size + 1); size_t allocSize = sizeof(*(this->str)) * (size + 1);
this->str = (char *)api->Alloc(allocSize, /*zeroClear*/false); this->str = (char *)api->Alloc(allocSize, /*zeroClear*/false);
if (!this->str) return false; if (!this->str) return false;
}
this->str[0] = 0; this->str[0] = 0;
}
this->max = size; this->max = size;
this->len = 0; this->len = 0;
this->memAPI = api; this->memAPI = api;
@ -5518,7 +5524,11 @@ bool DqnString::InitLiteral(char const *const cstr, i32 const lenInBytes, DqnMem
return false; return false;
} }
if (lenInBytes > 0)
{
this->str[lenInBytes] = 0; this->str[lenInBytes] = 0;
}
this->len = lenInBytes; this->len = lenInBytes;
this->max = this->len; this->max = this->len;
DqnMem_Copy(this->str, cstr, lenInBytes); DqnMem_Copy(this->str, cstr, lenInBytes);
@ -5530,7 +5540,9 @@ bool DqnString::InitLiteral(char const *const cstr, DqnMemAPI *const api)
{ {
i32 utf8LenInBytes = 0; i32 utf8LenInBytes = 0;
DqnStr_LenUTF8((u32 *)cstr, &utf8LenInBytes); DqnStr_LenUTF8((u32 *)cstr, &utf8LenInBytes);
bool result = this->InitLiteral(cstr, utf8LenInBytes, api); bool result = this->InitLiteral(cstr, utf8LenInBytes, api);
return result; return result;
} }