forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_freopen_s.c
90 lines (71 loc) · 1.88 KB
/
test_freopen_s.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*------------------------------------------------------------------
* test_freopen_s
* File 'io/freopen_s.c'
* Lines executed:100.00% of 18
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "safe_lib.h"
#include <unistd.h>
#define TMP "tmpfreopen"
#ifdef HAVE_FREOPEN_S
# define HAVE_NATIVE 1
#else
# define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"
int test_freopen_s (void);
int test_freopen_s (void)
{
errno_t rc;
int errs = 0;
FILE *tmp, *newf;
FILE *file = stdin;
/*--------------------------------------------------*/
print_msvcrt(use_msvcrt);
rc = freopen_s(NULL, TMP, "r", file);
init_msvcrt(rc == ESNULLP, &use_msvcrt);
ERR_MSVC(ESNULLP, EINVAL);
rc = freopen_s(&tmp, TMP, NULL, file);
ERR_MSVC(ESNULLP, EINVAL);
rc = freopen_s(&tmp, TMP, "r", NULL);
ERR_MSVC(ESNULLP, EINVAL);
/*--------------------------------------------------*/
rc = freopen_s(&tmp, TMP, "r", file);
ERR(ENOENT)
if (errno)
ERRNO(ENOENT);
/*--------------------------------------------------*/
/* TODO: fails with asan and valgrind on some glibc systems (not repro)
in strlen or __open_nocancel. glibc bug. */
#ifndef __GLIBC__
# ifndef HAVE_ASAN
file = stdin;
rc = freopen_s(&tmp, NULL, "rb", file);
if (rc == 0) {
ERR(0); /* EINVAL or EFAULT */
if (errno)
ERRNO(EINVAL);
}
# endif
#endif
/*--------------------------------------------------*/
tmp = fopen(TMP, "w");
if (tmp == NULL) {
puts ("fopen failed: %m");
return errs+1;
}
fputs("Hello 1\n", tmp);
rc = freopen_s(&newf, TMP, "a+", tmp);
ERR(0);
fputs("Hello 2\n", newf);
fclose(newf);
/*--------------------------------------------------*/
unlink(TMP);
return (errs);
}
int main (void)
{
return (test_freopen_s());
}