6
6
#define __CPROVER_STDIO_H_INCLUDED
7
7
#endif
8
8
9
- /* undefine macros in OpenBSD's stdio.h that are problematic to the checker. */
10
- #if defined(__OpenBSD__ )
11
- #undef getchar
12
9
#undef putchar
13
- #undef getc
14
- #undef feof
15
- #undef ferror
16
- #undef fileno
17
- #endif
18
10
19
11
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool (void );
20
12
@@ -237,7 +229,8 @@ __CPROVER_HIDE:;
237
229
__CPROVER_set_must (stream , "closed" );
238
230
#endif
239
231
int return_value = __VERIFIER_nondet_int ();
240
- free (stream );
232
+ if (stream != stdin && stream != stdout && stream != stderr )
233
+ free (stream );
241
234
return return_value ;
242
235
}
243
236
@@ -253,25 +246,83 @@ __CPROVER_HIDE:;
253
246
#define __CPROVER_STDLIB_H_INCLUDED
254
247
#endif
255
248
249
+ #ifndef __CPROVER_ERRNO_H_INCLUDED
250
+ # include <errno.h>
251
+ # define __CPROVER_ERRNO_H_INCLUDED
252
+ #endif
253
+
256
254
FILE * fdopen (int handle , const char * mode )
257
255
{
258
256
__CPROVER_HIDE :;
259
- (void )handle ;
257
+ if (handle < 0 )
258
+ {
259
+ errno = EBADF ;
260
+ return NULL ;
261
+ }
260
262
(void )* mode ;
261
263
#ifdef __CPROVER_STRING_ABSTRACTION
262
264
__CPROVER_assert (__CPROVER_is_zero_string (mode ),
263
265
"fdopen zero-termination of 2nd argument" );
264
266
#endif
265
267
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
+ }
268
284
#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
270
291
// 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
+ }
273
322
323
+ __CPROVER_assume (fileno (f ) == handle );
274
324
return f ;
325
+ #endif
275
326
}
276
327
277
328
/* FUNCTION: _fdopen */
@@ -291,19 +342,60 @@ FILE *fdopen(int handle, const char *mode)
291
342
#define __CPROVER_STDLIB_H_INCLUDED
292
343
#endif
293
344
345
+ #ifndef __CPROVER_ERRNO_H_INCLUDED
346
+ # include <errno.h>
347
+ # define __CPROVER_ERRNO_H_INCLUDED
348
+ #endif
349
+
294
350
#ifdef __APPLE__
351
+
352
+ # ifndef LIBRARY_CHECK
353
+ FILE * stdin ;
354
+ FILE * stdout ;
355
+ FILE * stderr ;
356
+ # endif
357
+
295
358
FILE * _fdopen (int handle , const char * mode )
296
359
{
297
360
__CPROVER_HIDE :;
298
- (void )handle ;
361
+ if (handle < 0 )
362
+ {
363
+ errno = EBADF ;
364
+ return NULL ;
365
+ }
299
366
(void )* mode ;
300
367
#ifdef __CPROVER_STRING_ABSTRACTION
301
368
__CPROVER_assert (__CPROVER_is_zero_string (mode ),
302
369
"fdopen zero-termination of 2nd argument" );
303
370
#endif
304
371
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
+ }
306
397
398
+ __CPROVER_assume (fileno (f ) == handle );
307
399
return f ;
308
400
}
309
401
#endif
@@ -506,6 +598,8 @@ __CPROVER_HIDE:;
506
598
#define __CPROVER_STDIO_H_INCLUDED
507
599
#endif
508
600
601
+ #undef feof
602
+
509
603
int __VERIFIER_nondet_int (void );
510
604
511
605
int feof (FILE * stream )
@@ -538,6 +632,8 @@ int feof(FILE *stream)
538
632
#define __CPROVER_STDIO_H_INCLUDED
539
633
#endif
540
634
635
+ #undef ferror
636
+
541
637
int __VERIFIER_nondet_int (void );
542
638
543
639
int ferror (FILE * stream )
@@ -570,6 +666,8 @@ int ferror(FILE *stream)
570
666
#define __CPROVER_STDIO_H_INCLUDED
571
667
#endif
572
668
669
+ #undef fileno
670
+
573
671
int __VERIFIER_nondet_int (void );
574
672
575
673
int fileno (FILE * stream )
@@ -735,6 +833,8 @@ int fgetc(FILE *stream)
735
833
#define __CPROVER_STDIO_H_INCLUDED
736
834
#endif
737
835
836
+ #undef getc
837
+
738
838
int __VERIFIER_nondet_int (void );
739
839
740
840
int getc (FILE * stream )
@@ -771,6 +871,8 @@ int getc(FILE *stream)
771
871
#define __CPROVER_STDIO_H_INCLUDED
772
872
#endif
773
873
874
+ #undef getchar
875
+
774
876
int __VERIFIER_nondet_int (void );
775
877
776
878
int getchar (void )
@@ -1939,10 +2041,13 @@ FILE *__acrt_iob_func(unsigned fd)
1939
2041
switch (fd )
1940
2042
{
1941
2043
case 0 :
2044
+ __CPROVER_havoc_object (& stdin_file );
1942
2045
return & stdin_file ;
1943
2046
case 1 :
2047
+ __CPROVER_havoc_object (& stdout_file );
1944
2048
return & stdout_file ;
1945
2049
case 2 :
2050
+ __CPROVER_havoc_object (& stderr_file );
1946
2051
return & stderr_file ;
1947
2052
default :
1948
2053
return (FILE * )0 ;
0 commit comments