diff mbox series

[03/15] debug: Add fortify dprintf tests

Message ID 20231221185929.1307116-4-adhemerval.zanella@linaro.org
State New
Headers show
Series Improve fortify support with clang | expand

Commit Message

Adhemerval Zanella Netto Dec. 21, 2023, 6:59 p.m. UTC
Similar to other printf-like ones.

Checked on aarch64, armhf, x86_64, and i686.
---
 debug/tst-fortify.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

Comments

Siddhesh Poyarekar Dec. 21, 2023, 8:03 p.m. UTC | #1
On 2023-12-21 13:59, Adhemerval Zanella wrote:
> Similar to other printf-like ones.
> 
> Checked on aarch64, armhf, x86_64, and i686.
> ---

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

>   debug/tst-fortify.c | 22 ++++++++++++++++++++++
>   1 file changed, 22 insertions(+)
> 
> diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
> index 5cd9d22feb..888eae25ad 100644
> --- a/debug/tst-fortify.c
> +++ b/debug/tst-fortify.c
> @@ -59,6 +59,8 @@
>   
>   static char *temp_filename;
>   
> +static int temp_fd_dprintf;
> +
>   static void
>   do_prepare (int argc, char *argv[])
>   {
> @@ -76,6 +78,13 @@ do_prepare (int argc, char *argv[])
>         unlink (temp_filename);
>         exit (1);
>       }
> +
> +  temp_fd_dprintf = create_temp_file ("tst-chk2.", NULL);
> +  if (temp_fd_dprintf == -1)
> +    {
> +      printf ("cannot create temporary file: %m\n");
> +      exit (1);
> +    }
>   }
>   #define PREPARE do_prepare
>   
> @@ -901,6 +910,10 @@ do_test (void)
>         || n1 != 1 || n2 != 2)
>       FAIL ();
>   
> +  if (dprintf (temp_fd_dprintf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
> +      || n1 != 1 || n2 != 2)
> +    FAIL ();
> +
>     strcpy (buf2 + 2, "%n%s%n");
>     /* When the format string is writable and contains %n,
>        with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
> @@ -914,6 +927,11 @@ do_test (void)
>       FAIL ();
>     CHK_FAIL2_END
>   
> +  CHK_FAIL2_START
> +  if (dprintf (temp_fd_dprintf, buf2, str2, &n1, str2, &n1) != 2)
> +    FAIL ();
> +  CHK_FAIL2_END
> +
>     /* But if there is no %n, even writable format string
>        should work.  */
>     buf2[6] = '\0';
> @@ -1263,6 +1281,10 @@ do_test (void)
>     snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
>     CHK_FAIL2_END
>   
> +  CHK_FAIL2_START
> +  dprintf (temp_fd_dprintf, "%3$d\n", 1, 2, 3, 4);
> +  CHK_FAIL2_END
> +
>     int sp[2];
>     if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
>       FAIL ();
diff mbox series

Patch

diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c
index 5cd9d22feb..888eae25ad 100644
--- a/debug/tst-fortify.c
+++ b/debug/tst-fortify.c
@@ -59,6 +59,8 @@ 
 
 static char *temp_filename;
 
+static int temp_fd_dprintf;
+
 static void
 do_prepare (int argc, char *argv[])
 {
@@ -76,6 +78,13 @@  do_prepare (int argc, char *argv[])
       unlink (temp_filename);
       exit (1);
     }
+
+  temp_fd_dprintf = create_temp_file ("tst-chk2.", NULL);
+  if (temp_fd_dprintf == -1)
+    {
+      printf ("cannot create temporary file: %m\n");
+      exit (1);
+    }
 }
 #define PREPARE do_prepare
 
@@ -901,6 +910,10 @@  do_test (void)
       || n1 != 1 || n2 != 2)
     FAIL ();
 
+  if (dprintf (temp_fd_dprintf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
+      || n1 != 1 || n2 != 2)
+    FAIL ();
+
   strcpy (buf2 + 2, "%n%s%n");
   /* When the format string is writable and contains %n,
      with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
@@ -914,6 +927,11 @@  do_test (void)
     FAIL ();
   CHK_FAIL2_END
 
+  CHK_FAIL2_START
+  if (dprintf (temp_fd_dprintf, buf2, str2, &n1, str2, &n1) != 2)
+    FAIL ();
+  CHK_FAIL2_END
+
   /* But if there is no %n, even writable format string
      should work.  */
   buf2[6] = '\0';
@@ -1263,6 +1281,10 @@  do_test (void)
   snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
   CHK_FAIL2_END
 
+  CHK_FAIL2_START
+  dprintf (temp_fd_dprintf, "%3$d\n", 1, 2, 3, 4);
+  CHK_FAIL2_END
+
   int sp[2];
   if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
     FAIL ();