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
@@ -213,25 +205,80 @@ __CPROVER_HIDE:;
213
205
#define __CPROVER_STDLIB_H_INCLUDED
214
206
#endif
215
207
208
+ #ifndef __CPROVER_ERRNO_H_INCLUDED
209
+ # include <errno.h>
210
+ # define __CPROVER_ERRNO_H_INCLUDED
211
+ #endif
212
+
213
+ #ifndef _WIN32
214
+ FILE * stdin ;
215
+ FILE * stdout ;
216
+ FILE * stderr ;
217
+ #endif
218
+
216
219
FILE * fdopen (int handle , const char * mode )
217
220
{
218
221
__CPROVER_HIDE :;
219
- (void )handle ;
222
+ if (handle < 0 )
223
+ {
224
+ errno = EBADF ;
225
+ return NULL ;
226
+ }
220
227
(void )* mode ;
221
228
#ifdef __CPROVER_STRING_ABSTRACTION
222
229
__CPROVER_assert (__CPROVER_is_zero_string (mode ),
223
230
"fdopen zero-termination of 2nd argument" );
224
231
#endif
225
232
226
- #if !defined(__linux__ ) || defined(__GLIBC__ )
233
+ #ifndef _WIN32
234
+ FILE * * f_alias = NULL ;
235
+ switch (handle )
236
+ {
237
+ case 0 :
238
+ f_alias = & stdin ;
239
+ break ;
240
+ case 1 :
241
+ f_alias = & stdout ;
242
+ break ;
243
+ case 2 :
244
+ f_alias = & stderr ;
245
+ break ;
246
+ }
247
+
248
+ if (f_alias != NULL && * f_alias != NULL )
249
+ {
250
+ __CPROVER_assume (fileno (* f_alias ) == handle );
251
+ return * f_alias ;
252
+ }
253
+ # if !defined(__linux__ ) || defined(__GLIBC__ )
227
254
FILE * f = malloc (sizeof (FILE ));
228
255
#else
229
- // libraries need to expose the definition of FILE; this is the
256
+ // libraries need not expose the definition of FILE; this is the
230
257
// case for musl
231
258
FILE * f = malloc (sizeof (int ));
232
259
#endif
260
+ if (f_alias != NULL )
261
+ * f_alias = f ;
262
+ __CPROVER_assume (fileno (f ) == handle );
233
263
234
264
return f ;
265
+ #else
266
+ switch (handle )
267
+ {
268
+ case 0 :
269
+ return stdin ;
270
+ case 1 :
271
+ return stdout ;
272
+ case 2 :
273
+ return stderr ;
274
+ default :
275
+ {
276
+ FILE * f = malloc (sizeof (FILE ));
277
+ __CPROVER_assume (fileno (f ) == handle );
278
+ return f ;
279
+ }
280
+ }
281
+ #endif
235
282
}
236
283
237
284
/* FUNCTION: _fdopen */
@@ -251,18 +298,56 @@ FILE *fdopen(int handle, const char *mode)
251
298
#define __CPROVER_STDLIB_H_INCLUDED
252
299
#endif
253
300
301
+ #ifndef __CPROVER_ERRNO_H_INCLUDED
302
+ # include <errno.h>
303
+ # define __CPROVER_ERRNO_H_INCLUDED
304
+ #endif
305
+
254
306
#ifdef __APPLE__
307
+
308
+ # ifndef LIBRARY_CHECK
309
+ FILE * stdin ;
310
+ FILE * stdout ;
311
+ FILE * stderr ;
312
+ # endif
313
+
255
314
FILE * _fdopen (int handle , const char * mode )
256
315
{
257
316
__CPROVER_HIDE :;
258
- (void )handle ;
317
+ if (handle < 0 )
318
+ {
319
+ errno = EBADF ;
320
+ return NULL ;
321
+ }
259
322
(void )* mode ;
260
323
#ifdef __CPROVER_STRING_ABSTRACTION
261
324
__CPROVER_assert (__CPROVER_is_zero_string (mode ),
262
325
"fdopen zero-termination of 2nd argument" );
263
326
#endif
264
327
328
+ FILE * * f_alias = NULL ;
329
+ switch (handle )
330
+ {
331
+ case 0 :
332
+ f_alias = & stdin ;
333
+ break ;
334
+ case 1 :
335
+ f_alias = & stdout ;
336
+ break ;
337
+ case 2 :
338
+ f_alias = & stderr ;
339
+ break ;
340
+ }
341
+
342
+ if (f_alias != NULL && * f_alias != NULL )
343
+ {
344
+ __CPROVER_assume (fileno (* f_alias ) == handle );
345
+ return * f_alias ;
346
+ }
265
347
FILE * f = malloc (sizeof (FILE ));
348
+ if (f_alias != NULL )
349
+ * f_alias = f ;
350
+ __CPROVER_assume (fileno (f ) == handle );
266
351
267
352
return f ;
268
353
}
@@ -466,6 +551,8 @@ __CPROVER_HIDE:;
466
551
#define __CPROVER_STDIO_H_INCLUDED
467
552
#endif
468
553
554
+ #undef feof
555
+
469
556
int __VERIFIER_nondet_int (void );
470
557
471
558
int feof (FILE * stream )
@@ -498,6 +585,8 @@ int feof(FILE *stream)
498
585
#define __CPROVER_STDIO_H_INCLUDED
499
586
#endif
500
587
588
+ #undef ferror
589
+
501
590
int __VERIFIER_nondet_int (void );
502
591
503
592
int ferror (FILE * stream )
@@ -530,6 +619,8 @@ int ferror(FILE *stream)
530
619
#define __CPROVER_STDIO_H_INCLUDED
531
620
#endif
532
621
622
+ #undef fileno
623
+
533
624
int __VERIFIER_nondet_int (void );
534
625
535
626
int fileno (FILE * stream )
@@ -695,6 +786,8 @@ int fgetc(FILE *stream)
695
786
#define __CPROVER_STDIO_H_INCLUDED
696
787
#endif
697
788
789
+ #undef getc
790
+
698
791
int __VERIFIER_nondet_int (void );
699
792
700
793
int getc (FILE * stream )
@@ -731,6 +824,8 @@ int getc(FILE *stream)
731
824
#define __CPROVER_STDIO_H_INCLUDED
732
825
#endif
733
826
827
+ #undef getchar
828
+
734
829
int __VERIFIER_nondet_int (void );
735
830
736
831
int getchar (void )
@@ -1593,20 +1688,31 @@ int vasprintf(char **ptr, const char *fmt, va_list ap)
1593
1688
# define __CPROVER_STDIO_H_INCLUDED
1594
1689
# endif
1595
1690
1691
+ # ifndef __CPROVER_STDLIB_H_INCLUDED
1692
+ # include <stdlib.h>
1693
+ # define __CPROVER_STDLIB_H_INCLUDED
1694
+ # endif
1695
+
1596
1696
FILE * __acrt_iob_func (unsigned fd )
1597
1697
{
1598
- static FILE stdin_file ;
1599
- static FILE stdout_file ;
1600
- static FILE stderr_file ;
1698
+ static FILE * stdin_file ;
1699
+ static FILE * stdout_file ;
1700
+ static FILE * stderr_file ;
1601
1701
1602
1702
switch (fd )
1603
1703
{
1604
1704
case 0 :
1605
- return & stdin_file ;
1705
+ if (!stdin_file )
1706
+ stdin_file = malloc (sizeof (FILE ));
1707
+ return stdin_file ;
1606
1708
case 1 :
1607
- return & stdout_file ;
1709
+ if (!stdout_file )
1710
+ stdout_file = malloc (sizeof (FILE ));
1711
+ return stdout_file ;
1608
1712
case 2 :
1609
- return & stderr_file ;
1713
+ if (!stderr_file )
1714
+ stderr_file = malloc (sizeof (FILE ));
1715
+ return stderr_file ;
1610
1716
default :
1611
1717
return (FILE * )0 ;
1612
1718
}
0 commit comments