From cfdbfa6b2cc27ae5b98c5f27599bbc0fc6a104c1 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 14 Mar 2018 23:53:43 -0400 Subject: [PATCH] Catch more possible instances of passing NULL to memcpy. Actually fixes #1976. --- src/libexpr/primops.cc | 12 ++++++++---- src/libstore/download.cc | 1 + src/libutil/hash.cc | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 6778023f5..c88f677da 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1601,12 +1601,16 @@ static void prim_partition(EvalState & state, const Pos & pos, Value * * args, V state.mkAttrs(v, 2); Value * vRight = state.allocAttr(v, state.sRight); - state.mkList(*vRight, right.size()); - memcpy(vRight->listElems(), right.data(), sizeof(Value *) * right.size()); + auto rsize = right.size(); + state.mkList(*vRight, rsize); + if (rsize) + memcpy(vRight->listElems(), right.data(), sizeof(Value *) * rsize); Value * vWrong = state.allocAttr(v, state.sWrong); - state.mkList(*vWrong, wrong.size()); - memcpy(vWrong->listElems(), wrong.data(), sizeof(Value *) * wrong.size()); + auto wsize = wrong.size(); + state.mkList(*vWrong, wsize); + if (wsize) + memcpy(vWrong->listElems(), wrong.data(), sizeof(Value *) * wsize); v.attrs->sort(); } diff --git a/src/libstore/download.cc b/src/libstore/download.cc index 5ab625f42..9d9f1153f 100644 --- a/src/libstore/download.cc +++ b/src/libstore/download.cc @@ -195,6 +195,7 @@ struct CurlDownloader : public Downloader if (readOffset == request.data->length()) return 0; auto count = std::min(size * nitems, request.data->length() - readOffset); + assert(count); memcpy(buffer, request.data->data() + readOffset, count); readOffset += count; return count; diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 75e476755..150995f55 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -191,6 +191,7 @@ Hash::Hash(const std::string & s, HashType type) auto d = base64Decode(std::string(s, pos)); if (d.size() != hashSize) throw BadHash("invalid base-64 hash '%s'", s); + assert(hashSize); memcpy(hash, d.data(), hashSize); }