66#define __CPROVER_STDIO_H_INCLUDED
77#endif
88
9- /* undefine macros in OpenBSD's stdio.h that are problematic to the checker. */
10- #if defined(__OpenBSD__ )
11- #undef getchar
129#undef putchar
13- #undef getc
14- #undef feof
15- #undef ferror
16- #undef fileno
17- #endif
1810
1911__CPROVER_bool __VERIFIER_nondet___CPROVER_bool (void );
2012
@@ -237,7 +229,8 @@ __CPROVER_HIDE:;
237229 __CPROVER_set_must (stream , "closed" );
238230#endif
239231 int return_value = __VERIFIER_nondet_int ();
240- free (stream );
232+ if (stream != stdin && stream != stdout && stream != stderr )
233+ free (stream );
241234 return return_value ;
242235}
243236
@@ -253,25 +246,83 @@ __CPROVER_HIDE:;
253246#define __CPROVER_STDLIB_H_INCLUDED
254247#endif
255248
249+ #ifndef __CPROVER_ERRNO_H_INCLUDED
250+ # include <errno.h>
251+ # define __CPROVER_ERRNO_H_INCLUDED
252+ #endif
253+
256254FILE * fdopen (int handle , const char * mode )
257255{
258256 __CPROVER_HIDE :;
259- (void )handle ;
257+ if (handle < 0 )
258+ {
259+ errno = EBADF ;
260+ return NULL ;
261+ }
260262 (void )* mode ;
261263#ifdef __CPROVER_STRING_ABSTRACTION
262264 __CPROVER_assert (__CPROVER_is_zero_string (mode ),
263265 "fdopen zero-termination of 2nd argument" );
264266#endif
265267
266- #if !defined(__linux__ ) || defined(__GLIBC__ )
267- FILE * f = malloc (sizeof (FILE ));
268+ #if defined(_WIN32 ) || defined(__OpenBSD__ ) || defined(__NetBSD__ )
269+ switch (handle )
270+ {
271+ case 0 :
272+ return stdin ;
273+ case 1 :
274+ return stdout ;
275+ case 2 :
276+ return stderr ;
277+ default :
278+ {
279+ FILE * f = malloc (sizeof (FILE ));
280+ __CPROVER_assume (fileno (f ) == handle );
281+ return f ;
282+ }
283+ }
268284#else
269- // libraries need to expose the definition of FILE; this is the
285+ # if !defined(__linux__ ) || defined(__GLIBC__ )
286+ static FILE stdin_file ;
287+ static FILE stdout_file ;
288+ static FILE stderr_file ;
289+ # else
290+ // libraries need not expose the definition of FILE; this is the
270291 // case for musl
271- FILE * f = malloc (sizeof (int ));
272- #endif
292+ static int stdin_file ;
293+ static int stdout_file ;
294+ static int stderr_file ;
295+ # endif
296+
297+ FILE * f = NULL ;
298+ switch (handle )
299+ {
300+ case 0 :
301+ stdin = & stdin_file ;
302+ __CPROVER_havoc_object (& stdin_file );
303+ f = & stdin_file ;
304+ break ;
305+ case 1 :
306+ stdout = & stdout_file ;
307+ __CPROVER_havoc_object (& stdout_file );
308+ f = & stdout_file ;
309+ break ;
310+ case 2 :
311+ stderr = & stderr_file ;
312+ __CPROVER_havoc_object (& stderr_file );
313+ f = & stderr_file ;
314+ break ;
315+ default :
316+ # if !defined(__linux__ ) || defined(__GLIBC__ )
317+ f = malloc (sizeof (FILE ));
318+ # else
319+ f = malloc (sizeof (int ));
320+ # endif
321+ }
273322
323+ __CPROVER_assume (fileno (f ) == handle );
274324 return f ;
325+ #endif
275326}
276327
277328/* FUNCTION: _fdopen */
@@ -291,19 +342,60 @@ FILE *fdopen(int handle, const char *mode)
291342#define __CPROVER_STDLIB_H_INCLUDED
292343#endif
293344
345+ #ifndef __CPROVER_ERRNO_H_INCLUDED
346+ # include <errno.h>
347+ # define __CPROVER_ERRNO_H_INCLUDED
348+ #endif
349+
294350#ifdef __APPLE__
351+
352+ # ifndef LIBRARY_CHECK
353+ FILE * stdin ;
354+ FILE * stdout ;
355+ FILE * stderr ;
356+ # endif
357+
295358FILE * _fdopen (int handle , const char * mode )
296359{
297360 __CPROVER_HIDE :;
298- (void )handle ;
361+ if (handle < 0 )
362+ {
363+ errno = EBADF ;
364+ return NULL ;
365+ }
299366 (void )* mode ;
300367#ifdef __CPROVER_STRING_ABSTRACTION
301368 __CPROVER_assert (__CPROVER_is_zero_string (mode ),
302369 "fdopen zero-termination of 2nd argument" );
303370#endif
304371
305- FILE * f = malloc (sizeof (FILE ));
372+ static FILE stdin_file ;
373+ static FILE stdout_file ;
374+ static FILE stderr_file ;
375+
376+ FILE * f = NULL ;
377+ switch (handle )
378+ {
379+ case 0 :
380+ stdin = & stdin_file ;
381+ __CPROVER_havoc_object (& stdin_file );
382+ f = & stdin_file ;
383+ break ;
384+ case 1 :
385+ stdout = & stdout_file ;
386+ __CPROVER_havoc_object (& stdout_file );
387+ f = & stdout_file ;
388+ break ;
389+ case 2 :
390+ stderr = & stderr_file ;
391+ __CPROVER_havoc_object (& stderr_file );
392+ f = & stderr_file ;
393+ break ;
394+ default :
395+ f = malloc (sizeof (FILE ));
396+ }
306397
398+ __CPROVER_assume (fileno (f ) == handle );
307399 return f ;
308400}
309401#endif
@@ -506,6 +598,8 @@ __CPROVER_HIDE:;
506598#define __CPROVER_STDIO_H_INCLUDED
507599#endif
508600
601+ #undef feof
602+
509603int __VERIFIER_nondet_int (void );
510604
511605int feof (FILE * stream )
@@ -538,6 +632,8 @@ int feof(FILE *stream)
538632#define __CPROVER_STDIO_H_INCLUDED
539633#endif
540634
635+ #undef ferror
636+
541637int __VERIFIER_nondet_int (void );
542638
543639int ferror (FILE * stream )
@@ -570,6 +666,8 @@ int ferror(FILE *stream)
570666#define __CPROVER_STDIO_H_INCLUDED
571667#endif
572668
669+ #undef fileno
670+
573671int __VERIFIER_nondet_int (void );
574672
575673int fileno (FILE * stream )
@@ -735,6 +833,8 @@ int fgetc(FILE *stream)
735833#define __CPROVER_STDIO_H_INCLUDED
736834#endif
737835
836+ #undef getc
837+
738838int __VERIFIER_nondet_int (void );
739839
740840int getc (FILE * stream )
@@ -771,6 +871,8 @@ int getc(FILE *stream)
771871#define __CPROVER_STDIO_H_INCLUDED
772872#endif
773873
874+ #undef getchar
875+
774876int __VERIFIER_nondet_int (void );
775877
776878int getchar (void )
@@ -1939,10 +2041,13 @@ FILE *__acrt_iob_func(unsigned fd)
19392041 switch (fd )
19402042 {
19412043 case 0 :
2044+ __CPROVER_havoc_object (& stdin_file );
19422045 return & stdin_file ;
19432046 case 1 :
2047+ __CPROVER_havoc_object (& stdout_file );
19442048 return & stdout_file ;
19452049 case 2 :
2050+ __CPROVER_havoc_object (& stderr_file );
19462051 return & stderr_file ;
19472052 default :
19482053 return (FILE * )0 ;
0 commit comments