Commit 4dfe3f98 authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

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.
parent 0795ce5e
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -2190,6 +2190,7 @@ class EncodeFnGenerator(CodeGenerator):
            self.checkAvail("elt_len", sva)
            self.popIndent(2)
            self.format("""
                    if (elt_len)
                      memcpy(ptr, obj->{c_name}.elts_, elt_len);
                    written += elt_len; ptr += elt_len;
                  }}""", c_name=sva.c_name)
@@ -2597,6 +2598,7 @@ class ParseFnGenerator(CodeGenerator):
                self.format("""
                    TRUNNEL_DYNARRAY_EXPAND({tp}, &obj->{c_name}, {w}, {{}});
                    obj->{c_name}.n_ = {w};
                    if ({w})
                      memcpy({elt}, ptr, {w});
                    """, w=w, elt=elt, tp=tp, c_name=sva.c_name)