From a17215e9849e395af17c5ee7eecc36c90e2a181f Mon Sep 17 00:00:00 2001 From: Omar Sandoval Date: Mon, 15 Jul 2019 11:03:14 -0700 Subject: [PATCH] libdrgn: dwarf_index: fix memcpy() undefined behavior Apparently, it's undefined behavior to pass NULL as the source to memcpy(), even if the length is zero. It's an easy fix, so let's appease UBSan. --- libdrgn/dwarf_index.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/libdrgn/dwarf_index.c b/libdrgn/dwarf_index.c index 8b11182b3..cbdbdaa5b 100644 --- a/libdrgn/dwarf_index.c +++ b/libdrgn/dwarf_index.c @@ -602,15 +602,18 @@ static struct drgn_error *read_cus(struct drgn_dwarf_index *dindex, } } - #pragma omp critical(drgn_read_cus) - if (!err) { - if (compilation_unit_vector_reserve(all_cus, - all_cus->size + cus.size)) { - memcpy(all_cus->data + all_cus->size, cus.data, - cus.size * sizeof(*cus.data)); - all_cus->size += cus.size; - } else { - err = &drgn_enomem; + if (cus.size) { + #pragma omp critical(drgn_read_cus) + if (!err) { + if (compilation_unit_vector_reserve(all_cus, + all_cus->size + cus.size)) { + memcpy(all_cus->data + all_cus->size, + cus.data, + cus.size * sizeof(*cus.data)); + all_cus->size += cus.size; + } else { + err = &drgn_enomem; + } } }