llohse / libnpy

C++ library for reading and writing of numpy's .npy files
MIT License
373 stars 71 forks source link

fix asan container overflow #28

Closed MattIsInIt closed 2 years ago

MattIsInIt commented 2 years ago

Hi,

Using the lib I spotted an ASAN issue du to: l.476

  auto buf_v = std::vector<char>();
  buf_v.reserve(header_length);
  istream.read(buf_v.data(), header_length);

it seems that reserve not changing the size of the vector and using istream read on the buffer (which does not change the size again) lead to a container overflow later on.

The proper way would be to use a resize or simply declare the vector with the correct size:

  auto buf_v = std::vector<char>(header_length);
  istream.read(buf_v.data(), header_length);

This get rid of the issue.

Cheers, Matt

llohse commented 2 years ago

Thanks a lot!