Skip to content

Commit

Permalink
EbmlString::ReadFully: use automatic memory management/fewer allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbunkus authored and robUx4 committed Dec 26, 2023
1 parent d09a750 commit ae9bb25
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/EbmlString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,20 @@ filepos_t EbmlString::ReadData(IOCallback & input, ScopeMode ReadFully)
return GetSize();

if (GetSize() == 0) {
Value = "";
SetValueIsSet();
Value.clear();

} else {
auto Buffer = (GetSize() + 1 < std::numeric_limits<std::size_t>::max()) ? new (std::nothrow) char[GetSize() + 1] : nullptr;
if (Buffer == nullptr) {
// unable to store the data, skip it
input.setFilePointer(GetSize(), seek_current);
} else {
input.readFully(Buffer, GetSize());
if (Buffer[GetSize()-1] != '\0') {
Buffer[GetSize()] = '\0';
}
Value = Buffer;
delete [] Buffer;
SetValueIsSet();
}
Value.resize(GetSize());
std::memset(&Value[0], 0, GetSize());
input.readFully(&Value[0], GetSize());

auto PosNull = Value.find('\0');
if (PosNull != std::string::npos)
Value.resize(PosNull);
}

SetValueIsSet();

return GetSize();
}

Expand Down

0 comments on commit ae9bb25

Please sign in to comment.