[tor-commits] [trunnel/master] In generated code, never pass NULL to memcpy even with length 0

nickm at torproject.org nickm at torproject.org
Mon Feb 22 19:13:08 UTC 2016


commit 4dfe3f98c031a05f2da84211ce857dbec514f4ae
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon Feb 22 14:12:00 2016 -0500

    In generated code, never pass NULL to memcpy even with length 0
    
    The C standard says that you can never say memcpy(x, NULL, 0), even
    though most libcs permit it.
    
    Found with asan.
---
 lib/trunnel/CodeGen.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/trunnel/CodeGen.py b/lib/trunnel/CodeGen.py
index 038cdd8..16047b3 100644
--- a/lib/trunnel/CodeGen.py
+++ b/lib/trunnel/CodeGen.py
@@ -2190,7 +2190,8 @@ class EncodeFnGenerator(CodeGenerator):
             self.checkAvail("elt_len", sva)
             self.popIndent(2)
             self.format("""
-                    memcpy(ptr, obj->{c_name}.elts_, elt_len);
+                    if (elt_len)
+                      memcpy(ptr, obj->{c_name}.elts_, elt_len);
                     written += elt_len; ptr += elt_len;
                   }}""", c_name=sva.c_name)
             return
@@ -2597,7 +2598,8 @@ class ParseFnGenerator(CodeGenerator):
                 self.format("""
                     TRUNNEL_DYNARRAY_EXPAND({tp}, &obj->{c_name}, {w}, {{}});
                     obj->{c_name}.n_ = {w};
-                    memcpy({elt}, ptr, {w});
+                    if ({w})
+                      memcpy({elt}, ptr, {w});
                     """, w=w, elt=elt, tp=tp, c_name=sva.c_name)
 
             self.format('ptr += {w}; remaining -= {w};\n', w=w)





More information about the tor-commits mailing list