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
@@ -197,7 +189,8 @@ __CPROVER_HIDE:;
197
189
__CPROVER_set_must (stream , "closed" );
198
190
#endif
199
191
int return_value = __VERIFIER_nondet_int ();
200
- free (stream );
192
+ if (stream != stdin && stream != stdout && stream != stderr )
193
+ free (stream );
201
194
return return_value ;
202
195
}
203
196
@@ -213,25 +206,83 @@ __CPROVER_HIDE:;
213
206
#define __CPROVER_STDLIB_H_INCLUDED
214
207
#endif
215
208
209
+ #ifndef __CPROVER_ERRNO_H_INCLUDED
210
+ # include <errno.h>
211
+ # define __CPROVER_ERRNO_H_INCLUDED
212
+ #endif
213
+
216
214
FILE * fdopen (int handle , const char * mode )
217
215
{
218
216
__CPROVER_HIDE :;
219
- (void )handle ;
217
+ if (handle < 0 )
218
+ {
219
+ errno = EBADF ;
220
+ return NULL ;
221
+ }
220
222
(void )* mode ;
221
223
#ifdef __CPROVER_STRING_ABSTRACTION
222
224
__CPROVER_assert (__CPROVER_is_zero_string (mode ),
223
225
"fdopen zero-termination of 2nd argument" );
224
226
#endif
225
227
226
- #if !defined(__linux__ ) || defined(__GLIBC__ )
227
- FILE * f = malloc (sizeof (FILE ));
228
+ #if defined(_WIN32 ) || defined(__OpenBSD__ ) || defined(__NetBSD__ )
229
+ switch (handle )
230
+ {
231
+ case 0 :
232
+ return stdin ;
233
+ case 1 :
234
+ return stdout ;
235
+ case 2 :
236
+ return stderr ;
237
+ default :
238
+ {
239
+ FILE * f = malloc (sizeof (FILE ));
240
+ __CPROVER_assume (fileno (f ) == handle );
241
+ return f ;
242
+ }
243
+ }
228
244
#else
229
- // libraries need to expose the definition of FILE; this is the
245
+ # if !defined(__linux__ ) || defined(__GLIBC__ )
246
+ static FILE stdin_file ;
247
+ static FILE stdout_file ;
248
+ static FILE stderr_file ;
249
+ # else
250
+ // libraries need not expose the definition of FILE; this is the
230
251
// case for musl
231
- FILE * f = malloc (sizeof (int ));
232
- #endif
252
+ static int stdin_file ;
253
+ static int stdout_file ;
254
+ static int stderr_file ;
255
+ # endif
256
+
257
+ FILE * f = NULL ;
258
+ switch (handle )
259
+ {
260
+ case 0 :
261
+ stdin = & stdin_file ;
262
+ __CPROVER_havoc_object (& stdin_file );
263
+ f = & stdin_file ;
264
+ break ;
265
+ case 1 :
266
+ stdout = & stdout_file ;
267
+ __CPROVER_havoc_object (& stdout_file );
268
+ f = & stdout_file ;
269
+ break ;
270
+ case 2 :
271
+ stderr = & stderr_file ;
272
+ __CPROVER_havoc_object (& stderr_file );
273
+ f = & stderr_file ;
274
+ break ;
275
+ default :
276
+ # if !defined(__linux__ ) || defined(__GLIBC__ )
277
+ f = malloc (sizeof (FILE ));
278
+ # else
279
+ f = malloc (sizeof (int ));
280
+ # endif
281
+ }
233
282
283
+ __CPROVER_assume (fileno (f ) == handle );
234
284
return f ;
285
+ #endif
235
286
}
236
287
237
288
/* FUNCTION: _fdopen */
@@ -251,19 +302,60 @@ FILE *fdopen(int handle, const char *mode)
251
302
#define __CPROVER_STDLIB_H_INCLUDED
252
303
#endif
253
304
305
+ #ifndef __CPROVER_ERRNO_H_INCLUDED
306
+ # include <errno.h>
307
+ # define __CPROVER_ERRNO_H_INCLUDED
308
+ #endif
309
+
254
310
#ifdef __APPLE__
311
+
312
+ # ifndef LIBRARY_CHECK
313
+ FILE * stdin ;
314
+ FILE * stdout ;
315
+ FILE * stderr ;
316
+ # endif
317
+
255
318
FILE * _fdopen (int handle , const char * mode )
256
319
{
257
320
__CPROVER_HIDE :;
258
- (void )handle ;
321
+ if (handle < 0 )
322
+ {
323
+ errno = EBADF ;
324
+ return NULL ;
325
+ }
259
326
(void )* mode ;
260
327
#ifdef __CPROVER_STRING_ABSTRACTION
261
328
__CPROVER_assert (__CPROVER_is_zero_string (mode ),
262
329
"fdopen zero-termination of 2nd argument" );
263
330
#endif
264
331
265
- FILE * f = malloc (sizeof (FILE ));
332
+ static FILE stdin_file ;
333
+ static FILE stdout_file ;
334
+ static FILE stderr_file ;
266
335
336
+ FILE * f = NULL ;
337
+ switch (handle )
338
+ {
339
+ case 0 :
340
+ stdin = & stdin_file ;
341
+ __CPROVER_havoc_object (& stdin_file );
342
+ f = & stdin_file ;
343
+ break ;
344
+ case 1 :
345
+ stdout = & stdout_file ;
346
+ __CPROVER_havoc_object (& stdout_file );
347
+ f = & stdout_file ;
348
+ break ;
349
+ case 2 :
350
+ stderr = & stderr_file ;
351
+ __CPROVER_havoc_object (& stderr_file );
352
+ f = & stderr_file ;
353
+ break ;
354
+ default :
355
+ f = malloc (sizeof (FILE ));
356
+ }
357
+
358
+ __CPROVER_assume (fileno (f ) == handle );
267
359
return f ;
268
360
}
269
361
#endif
@@ -466,6 +558,8 @@ __CPROVER_HIDE:;
466
558
#define __CPROVER_STDIO_H_INCLUDED
467
559
#endif
468
560
561
+ #undef feof
562
+
469
563
int __VERIFIER_nondet_int (void );
470
564
471
565
int feof (FILE * stream )
@@ -498,6 +592,8 @@ int feof(FILE *stream)
498
592
#define __CPROVER_STDIO_H_INCLUDED
499
593
#endif
500
594
595
+ #undef ferror
596
+
501
597
int __VERIFIER_nondet_int (void );
502
598
503
599
int ferror (FILE * stream )
@@ -530,6 +626,8 @@ int ferror(FILE *stream)
530
626
#define __CPROVER_STDIO_H_INCLUDED
531
627
#endif
532
628
629
+ #undef fileno
630
+
533
631
int __VERIFIER_nondet_int (void );
534
632
535
633
int fileno (FILE * stream )
@@ -695,6 +793,8 @@ int fgetc(FILE *stream)
695
793
#define __CPROVER_STDIO_H_INCLUDED
696
794
#endif
697
795
796
+ #undef getc
797
+
698
798
int __VERIFIER_nondet_int (void );
699
799
700
800
int getc (FILE * stream )
@@ -731,6 +831,8 @@ int getc(FILE *stream)
731
831
#define __CPROVER_STDIO_H_INCLUDED
732
832
#endif
733
833
834
+ #undef getchar
835
+
734
836
int __VERIFIER_nondet_int (void );
735
837
736
838
int getchar (void )
@@ -1602,10 +1704,13 @@ FILE *__acrt_iob_func(unsigned fd)
1602
1704
switch (fd )
1603
1705
{
1604
1706
case 0 :
1707
+ __CPROVER_havoc_object (& stdin_file );
1605
1708
return & stdin_file ;
1606
1709
case 1 :
1710
+ __CPROVER_havoc_object (& stdout_file );
1607
1711
return & stdout_file ;
1608
1712
case 2 :
1713
+ __CPROVER_havoc_object (& stderr_file );
1609
1714
return & stderr_file ;
1610
1715
default :
1611
1716
return (FILE * )0 ;
0 commit comments