From: Simon Marchi Date: Fri, 5 Oct 2018 02:43:27 +0000 (-0400) Subject: Fix undefined behavior, don't pass NULL to fwrite X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1f88d0c87c37d3a15fa6376335e8b0d1c79d85aa;p=binutils-gdb.git Fix undefined behavior, don't pass NULL to fwrite If a vector that we try to write using file_write is empty, we may end up passing NULL to fwrite, which triggers UBSan: .../gdb/dwarf-index-write.c:73:14: runtime error: null pointer passed as argument 1, which is declared to never be null Avoid it by skipping the write if the vector is empty. gdb/ChangeLog: * dwarf-index-write.c (file_write): Don't write if the vector is empty. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 126deb7893c..0e5a3f482c5 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2018-10-04 Simon Marchi + + * dwarf-index-write.c (file_write): Don't write if the vector is + empty. + 2018-10-05 Tom de Vries * python/py-progspace.c (pspy_solib_name): Fix type mismatch in diff --git a/gdb/dwarf-index-write.c b/gdb/dwarf-index-write.c index 252032161fb..d4585af837c 100644 --- a/gdb/dwarf-index-write.c +++ b/gdb/dwarf-index-write.c @@ -80,7 +80,8 @@ template static void file_write (FILE *file, const std::vector &vec) { - file_write (file, vec.data (), vec.size () * sizeof (vec[0])); + if (!vec.empty ()) + file_write (file, vec.data (), vec.size () * sizeof (vec[0])); } /* In-memory buffer to prepare data to be written later to a file. */