aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2018-10-17 13:04:52 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2018-10-17 13:04:52 +0300
commit69f5ba17eef319bc112cadd54f18cccc10495ecb (patch)
treeeca1d1af41d1adef77d55516203b29b89d3824c1
parentec9c6f1bbdfd3d86fba493ea56473c0aaf9acad1 (diff)
Fix undefined behavior for ofdstream::write(nullptr, 0)
-rw-r--r--libbutl/fdstream.cxx25
1 files changed, 22 insertions, 3 deletions
diff --git a/libbutl/fdstream.cxx b/libbutl/fdstream.cxx
index 955b75d..72439ae 100644
--- a/libbutl/fdstream.cxx
+++ b/libbutl/fdstream.cxx
@@ -306,7 +306,14 @@ namespace butl
size_t an (epptr () - pptr ()); // Amount of free space in the buffer.
if (n <= an)
{
- memcpy (pptr (), s, n);
+ assert (s != nullptr || n == 0);
+
+ // Note that the memcpy() function behavior is undefined if either of
+ // pointers is NULL, even if the bytes count is zero.
+ //
+ if (s != nullptr)
+ memcpy (pptr (), s, n);
+
advance (n);
return n;
}
@@ -361,7 +368,13 @@ namespace butl
an = 0;
else
{
- memcpy (pptr (), s, an);
+ assert (s != nullptr || an == 0);
+
+ // The source can not be NULL (see above for details).
+ //
+ if (s != nullptr)
+ memcpy (pptr (), s, an);
+
advance (an);
}
@@ -398,7 +411,13 @@ namespace butl
//
if (n <= static_cast<size_t> (epptr () - pbase ()))
{
- memcpy (pbase (), s, n);
+ assert (s != nullptr || n == 0);
+
+ // The source can not be NULL (see above for details).
+ //
+ if (s != nullptr)
+ memcpy (pbase (), s, n);
+
advance (n);
return sn;
}