testsuite, analyzer: Fix up pipe-glibc.c testcase [PR107396]

Message ID ZCVCzOdvlQG2Lke7@tucnak
State Committed
Headers
Series testsuite, analyzer: Fix up pipe-glibc.c testcase [PR107396] |

Commit Message

Jakub Jelinek March 30, 2023, 8:05 a.m. UTC
  Hi!

The gcc.dg/analyzer/pipe-glibc.c test FAILs when using recent glibc headers
and succeeds with older headers.
The important change is that
https://sourceware.org/git/?p=glibc.git;a=commit;h=c1760eaf3b575ad174fd88b252fd16bd525fa818
in 2021 added __attribute__ ((__malloc__ (fclose, 1))) attribute to fdopen,
so in write_to_pipe there is an excess warning:
.../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c: In function 'write_to_pipe':
.../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c:28:3: warning: use of possibly-NULL 'stream' where non-null expected [CWE-690] [-Wanalyzer-possible-null-argument]
.../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c:27:12: note: (1) this call could return NULL
.../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c:28:3: note: (2) argument 4 ('stream') from (1) could be NULL where non-null expected
<built-in>: note: argument 4 of '__builtin_fwrite' must be non-null
Strangely, nothing is reported on the read_from_pipe function, seems
fwrite/fprintf/fputc etc. are builtins in GCC and we mark the FILE *
arguments as nonnull there on the builtin declarations, while fgetc/fread
etc. aren't builtins and glibc doesn't mark any of those using nonnull.
Shall we change that on the glibc side?

Anyway, because this differs based on glibc version and I think the
above warning is not the primary intention of the test, I think it is
best to tweak it so that this warning isn't reported.
Another option would be avoid using glibc headers and use our own
declarations, or make sure we add the malloc with fclose attribute ourselves
(but fdopen in the libc headers could be a macro, so not sure
__typeof (fdopen) fdopen __attribute__ ((__malloc__, __malloc__ (fclose, 1)));
would work).  Or use -Wno-analyzer-possible-null-arguments in
dg-additional-options?

Bootstrapped/regtested on x86_64-linux and  i686-linux, ok for trunk?

2023-03-30  Jakub Jelinek  <jakub@redhat.com>

	PR analyzer/107396
	* gcc.dg/analyzer/pipe-glibc.c (read_from_pie, write_to_pipe): Exit
	if fdopen returns NULL.


	Jakub
  

Comments

David Malcolm March 31, 2023, 8:38 p.m. UTC | #1
On Thu, 2023-03-30 at 10:05 +0200, Jakub Jelinek wrote:
> Hi!
> 
> The gcc.dg/analyzer/pipe-glibc.c test FAILs when using recent glibc
> headers
> and succeeds with older headers.
> The important change is that
> https://sourceware.org/git/?p=glibc.git;a=commit;h=c1760eaf3b575ad174fd88b252fd16bd525fa818
> in 2021 added __attribute__ ((__malloc__ (fclose, 1))) attribute to
> fdopen,
> so in write_to_pipe there is an excess warning:
> .../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c: In function
> 'write_to_pipe':
> .../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c:28:3: warning: use of
> possibly-NULL 'stream' where non-null expected [CWE-690] [-Wanalyzer-
> possible-null-argument]
> .../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c:27:12: note: (1) this
> call could return NULL
> .../gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c:28:3: note: (2)
> argument 4 ('stream') from (1) could be NULL where non-null expected
> <built-in>: note: argument 4 of '__builtin_fwrite' must be non-null
> Strangely, nothing is reported on the read_from_pipe function, seems
> fwrite/fprintf/fputc etc. are builtins in GCC and we mark the FILE *
> arguments as nonnull there on the builtin declarations, while
> fgetc/fread
> etc. aren't builtins and glibc doesn't mark any of those using
> nonnull.
> Shall we change that on the glibc side?
> 
> Anyway, because this differs based on glibc version and I think the
> above warning is not the primary intention of the test, I think it is
> best to tweak it so that this warning isn't reported.

[...snip...]

Thanks: the patch LGTM.

Dave
  

Patch

--- gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c.jj	2022-10-25 10:37:28.106531709 +0200
+++ gcc/testsuite/gcc.dg/analyzer/pipe-glibc.c	2023-03-29 19:14:48.789766475 +0200
@@ -13,6 +13,8 @@  read_from_pipe (int file)
   FILE *stream;
   int c;
   stream = fdopen (file, "r");
+  if (stream == NULL)
+    exit (EXIT_FAILURE);
   while ((c = fgetc (stream)) != EOF)
     putchar (c);
   fclose (stream);
@@ -25,6 +27,8 @@  write_to_pipe (int file)
 {
   FILE *stream;
   stream = fdopen (file, "w");
+  if (stream == NULL)
+    exit (EXIT_FAILURE);
   fprintf (stream, "hello, world!\n");
   fprintf (stream, "goodbye, world!\n");
   fclose (stream);