diff mbox

malloc/tst-valloc.c: Tidy up code.

Message ID 524D625F.4080208@linaro.org
State Accepted
Headers show

Commit Message

Will Newton Oct. 3, 2013, 12:26 p.m. UTC
Add some comments and call free on all potentially allocated pointers.
Also remove duplicate check for NULL pointer.

ChangeLog:

2013-10-03  Will Newton  <will.newton@linaro.org>

	* malloc/tst-valloc.c: Add comments.
	(do_test): Add comments and call free on all potentially
	allocated pointers. Remove duplicate check for NULL pointer.
	Add space after cast.
---
 malloc/tst-valloc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

Comments

Carlos O'Donell Oct. 3, 2013, 8:58 p.m. UTC | #1
On 10/03/2013 08:26 AM, Will Newton wrote:
> 
> Add some comments and call free on all potentially allocated pointers.
> Also remove duplicate check for NULL pointer.
> 
> ChangeLog:
> 
> 2013-10-03  Will Newton  <will.newton@linaro.org>
> 
> 	* malloc/tst-valloc.c: Add comments.
> 	(do_test): Add comments and call free on all potentially
> 	allocated pointers. Remove duplicate check for NULL pointer.
> 	Add space after cast.

Also looks good. Thanks again for fixing the nits and adding comments.

> ---
>  malloc/tst-valloc.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
> 
> diff --git a/malloc/tst-valloc.c b/malloc/tst-valloc.c
> index 3648f2f..7676e66 100644
> --- a/malloc/tst-valloc.c
> +++ b/malloc/tst-valloc.c
> @@ -1,4 +1,5 @@
> -/* Copyright (C) 2013 Free Software Foundation, Inc.
> +/* Test for valloc.
> +   Copyright (C) 2013 Free Software Foundation, Inc.
>     This file is part of the GNU C Library.
> 
>     The GNU C Library is free software; you can redistribute it and/or
> @@ -40,6 +41,8 @@ do_test (void)
> 
>    errno = 0;
> 
> +  /* An attempt to allocate a huge value should return NULL and set
> +     errno to ENOMEM.  */
>    p = valloc (-1);
> 
>    save = errno;
> @@ -50,8 +53,11 @@ do_test (void)
>    if (p == NULL && save != ENOMEM)
>      merror ("valloc (-1) errno is not set correctly");
> 
> +  free (p);
> +
>    errno = 0;
> 
> +  /* Test to expose integer overflow in malloc internals from BZ #15856.  */
>    p = valloc (-pagesize);
> 
>    save = errno;
> @@ -62,6 +68,10 @@ do_test (void)
>    if (p == NULL && save != ENOMEM)
>      merror ("valloc (-pagesize) errno is not set correctly");
> 
> +  free (p);
> +
> +  /* A zero-sized allocation should succeed with glibc, returning a
> +     non-NULL value.  */
>    p = valloc (0);
> 
>    if (p == NULL)
> @@ -69,15 +79,13 @@ do_test (void)
> 
>    free (p);
> 
> +  /* Check the alignment of the returned pointer is correct.  */
>    p = valloc (32);
> 
>    if (p == NULL)
>      merror ("valloc (32) failed.");
> 
> -  ptrval = (unsigned long)p;
> -
> -  if (p == NULL)
> -    merror ("valloc (32) failed.");
> +  ptrval = (unsigned long) p;
> 
>    if ((ptrval & (pagesize - 1)) != 0)
>      merror ("returned pointer is not page aligned.");
>
diff mbox

Patch

diff --git a/malloc/tst-valloc.c b/malloc/tst-valloc.c
index 3648f2f..7676e66 100644
--- a/malloc/tst-valloc.c
+++ b/malloc/tst-valloc.c
@@ -1,4 +1,5 @@ 
-/* Copyright (C) 2013 Free Software Foundation, Inc.
+/* Test for valloc.
+   Copyright (C) 2013 Free Software Foundation, Inc.
    This file is part of the GNU C Library.

    The GNU C Library is free software; you can redistribute it and/or
@@ -40,6 +41,8 @@  do_test (void)

   errno = 0;

+  /* An attempt to allocate a huge value should return NULL and set
+     errno to ENOMEM.  */
   p = valloc (-1);

   save = errno;
@@ -50,8 +53,11 @@  do_test (void)
   if (p == NULL && save != ENOMEM)
     merror ("valloc (-1) errno is not set correctly");

+  free (p);
+
   errno = 0;

+  /* Test to expose integer overflow in malloc internals from BZ #15856.  */
   p = valloc (-pagesize);

   save = errno;
@@ -62,6 +68,10 @@  do_test (void)
   if (p == NULL && save != ENOMEM)
     merror ("valloc (-pagesize) errno is not set correctly");

+  free (p);
+
+  /* A zero-sized allocation should succeed with glibc, returning a
+     non-NULL value.  */
   p = valloc (0);

   if (p == NULL)
@@ -69,15 +79,13 @@  do_test (void)

   free (p);

+  /* Check the alignment of the returned pointer is correct.  */
   p = valloc (32);

   if (p == NULL)
     merror ("valloc (32) failed.");

-  ptrval = (unsigned long)p;
-
-  if (p == NULL)
-    merror ("valloc (32) failed.");
+  ptrval = (unsigned long) p;

   if ((ptrval & (pagesize - 1)) != 0)
     merror ("returned pointer is not page aligned.");