diff mbox

Fix selftest::read_file for empty file

Message ID 1500475773-32599-1-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm July 19, 2017, 2:49 p.m. UTC
selftest::read_file currently assumes it has a non-empty file;
when loading an empty file it dies with an assertion failure,
or a write through NULL if assertions are disabled.

This patch fixes this case, removing this limitation.

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu;
takes -fself-test from 39458 passes to 39460.

OK for trunk?

gcc/ChangeLog:
	* selftest.c (selftest::read_file): Handle 0-length files.
	(selftest::test_read_empty_file): New function.
	(selftest::selftest_c_tests): Call it.
---
 gcc/selftest.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Jeff Law July 19, 2017, 2:49 p.m. UTC | #1
On 07/19/2017 08:49 AM, David Malcolm wrote:
> selftest::read_file currently assumes it has a non-empty file;
> when loading an empty file it dies with an assertion failure,
> or a write through NULL if assertions are disabled.
> 
> This patch fixes this case, removing this limitation.
> 
> Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu;
> takes -fself-test from 39458 passes to 39460.
> 
> OK for trunk?
> 
> gcc/ChangeLog:
> 	* selftest.c (selftest::read_file): Handle 0-length files.
> 	(selftest::test_read_empty_file): New function.
> 	(selftest::selftest_c_tests): Call it.
OK.
jeff
Marek Polacek July 19, 2017, 2:54 p.m. UTC | #2
On Wed, Jul 19, 2017 at 10:49:33AM -0400, David Malcolm wrote:
> selftest::read_file currently assumes it has a non-empty file;
> when loading an empty file it dies with an assertion failure,
> or a write through NULL if assertions are disabled.
> 
> This patch fixes this case, removing this limitation.
> 
> Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu;
> takes -fself-test from 39458 passes to 39460.
> 
> OK for trunk?
> 
> gcc/ChangeLog:
> 	* selftest.c (selftest::read_file): Handle 0-length files.
> 	(selftest::test_read_empty_file): New function.
> 	(selftest::selftest_c_tests): Call it.
> ---
>  gcc/selftest.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/gcc/selftest.c b/gcc/selftest.c
> index b41b9f5..026a35f 100644
> --- a/gcc/selftest.c
> +++ b/gcc/selftest.c
> @@ -192,7 +192,14 @@ read_file (const location &loc, const char *path)
>    fclose (f_in);
>  
>    /* 0-terminate the buffer.  */
> +  if (total_sz == 0)
> +    {
> +      size_t new_alloc_sz = alloc_sz ? alloc_sz * 2: total_sz + 1;

There should be a space before ":".  Also, if you know total_sz is 0, why
"total_sz + 1" and not just "1"?

> +      result = (char *)xrealloc (result, new_alloc_sz);

A space after ).

> +      alloc_sz = new_alloc_sz;
> +    }
>    gcc_assert (total_sz < alloc_sz);
> +  gcc_assert (result);
>    result[total_sz] = '\0';
>  
>    return result;
> @@ -296,6 +303,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
> @@ -317,6 +335,7 @@ selftest_c_tests ()
>    test_assertions ();
>    test_named_temp_file ();
>    test_read_file ();
> +  test_read_empty_file ();
>    test_locate_file ();
>  }
>  
> -- 
> 1.8.5.3
> 

	Marek
Jakub Jelinek July 19, 2017, 2:59 p.m. UTC | #3
On Wed, Jul 19, 2017 at 04:54:31PM +0200, Marek Polacek wrote:
> > --- a/gcc/selftest.c
> > +++ b/gcc/selftest.c
> > @@ -192,7 +192,14 @@ read_file (const location &loc, const char *path)
> >    fclose (f_in);
> >  
> >    /* 0-terminate the buffer.  */
> > +  if (total_sz == 0)
> > +    {
> > +      size_t new_alloc_sz = alloc_sz ? alloc_sz * 2: total_sz + 1;
> 
> There should be a space before ":".  Also, if you know total_sz is 0, why
> "total_sz + 1" and not just "1"?
> 
> > +      result = (char *)xrealloc (result, new_alloc_sz);
> 
> A space after ).

Or better
  result = XRESIZEVEC (char, result, new_alloc_sz); 
?

	Jakub
diff mbox

Patch

diff --git a/gcc/selftest.c b/gcc/selftest.c
index b41b9f5..026a35f 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -192,7 +192,14 @@  read_file (const location &loc, const char *path)
   fclose (f_in);
 
   /* 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;
@@ -296,6 +303,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
@@ -317,6 +335,7 @@  selftest_c_tests ()
   test_assertions ();
   test_named_temp_file ();
   test_read_file ();
+  test_read_empty_file ();
   test_locate_file ();
 }