Skip to content

Commit

Permalink
Make NAR header check more robust
Browse files Browse the repository at this point in the history
Changes

  std::bad_alloc

into

  bad archive: input doesn't look like a Nix archive
  • Loading branch information
edolstra committed Sep 26, 2018
1 parent 7ccdcc7 commit 44e8630
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/libutil/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void parseDump(ParseSink & sink, Source & source)
{
string version;
try {
version = readString(source);
version = readString(source, narVersionMagic1.size());
} catch (SerialisationError & e) {
/* This generally means the integer at the start couldn't be
decoded. Ignore and throw the exception below. */
Expand Down
5 changes: 3 additions & 2 deletions src/libutil/serialise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,17 @@ void readPadding(size_t len, Source & source)
size_t readString(unsigned char * buf, size_t max, Source & source)
{
auto len = readNum<size_t>(source);
if (len > max) throw Error("string is too long");
if (len > max) throw SerialisationError("string is too long");
source(buf, len);
readPadding(len, source);
return len;
}


string readString(Source & source)
string readString(Source & source, size_t max)
{
auto len = readNum<size_t>(source);
if (len > max) throw SerialisationError("string is too long");
std::string res(len, 0);
source((unsigned char*) res.data(), len);
readPadding(len, source);
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/serialise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ inline uint64_t readLongLong(Source & source)

void readPadding(size_t len, Source & source);
size_t readString(unsigned char * buf, size_t max, Source & source);
string readString(Source & source);
string readString(Source & source, size_t max = std::numeric_limits<size_t>::max());
template<class T> T readStrings(Source & source);

Source & operator >> (Source & in, string & s);
Expand Down

0 comments on commit 44e8630

Please sign in to comment.