From 9e5b6756c815a99f2061096d11dc2236db760e79 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 30 May 2017 17:30:47 -0400 Subject: [PATCH 06/33] FIXME: fix selftest::read_file for empty file --- gcc/selftest.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/gcc/selftest.c b/gcc/selftest.c index d940ba1..364160a 100644 --- a/gcc/selftest.c +++ b/gcc/selftest.c @@ -202,7 +202,14 @@ read_file (const location &loc, FILE *f_in, const char *desc) xstrerror (errno)); /* 0-terminate the buffer. */ + if (total_sz == 0) + { + size_t new_alloc_sz = alloc_sz ? alloc_sz * 2: total_sz + 1; + result = (char *)xrealloc (result, new_alloc_sz); + alloc_sz = new_alloc_sz; + } gcc_assert (total_sz < alloc_sz); + gcc_assert (result); result[total_sz] = '\0'; return result; @@ -332,6 +339,17 @@ test_read_file () free (buf); } +/* Verify that read_file can cope with an empty file. */ + +static void +test_read_empty_file () +{ + temp_source_file t (SELFTEST_LOCATION, "empty.txt", ""); + char *buf = read_file (SELFTEST_LOCATION, t.get_filename ()); + ASSERT_STREQ ("", buf); + free (buf); +} + /* Verify locate_file (and read_file). */ static void @@ -353,6 +371,7 @@ selftest_c_tests () test_assertions (); test_named_temp_file (); test_read_file (); + test_read_empty_file (); test_locate_file (); } -- 1.8.5.3