File: test_fprintf_s.c

package info (click to toggle)
safeclib 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: ansic: 52,639; makefile: 1,271; perl: 528; sh: 518
file content (175 lines) | stat: -rw-r--r-- 3,916 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*------------------------------------------------------------------
 * test_fprintf_s
 * File 'io/fprintf_s.c'
 * Lines executed:85.71% of 21
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_str_lib.h"
#include <unistd.h>

#ifdef HAVE_FPRINTF_S
#define HAVE_NATIVE 1
#else
#define HAVE_NATIVE 0
#endif
#include "test_msvcrt.h"

#define TMP "tmpfp"
#define LEN (128)

int errs = 0;
int win_wplus = 0; // if EINVAL with fopen w+
static FILE *out, *f;
static char str[LEN];
int test_fprintf_s(void);

// a variant for debugging
static size_t cmp_and_reset(FILE *out, const char *s) {
    size_t nread;
    size_t pos = ftell(out);
    fflush(out);
    *str = '\0';
    if (win_wplus) {
        fclose(out);
        out = fopen(TMP, "r");
        nread = fread(str, 1, pos, out);
        fclose(out);
        out = fopen(TMP, "w");
    } else {
        rewind(out);
        nread = fread(str, 1, pos, out);
    }
    EXPSTR(str, s)
    if (!win_wplus) {
        rewind(out);
#if defined(HAVE_FTRUNCATE) && defined(HAVE_FILENO)
        nread = ftruncate(fileno(out), 0L);
#endif
    }
    (void) nread;
    return pos;
}

int test_fprintf_s(void) {
    size_t nread;
    errno_t rc;
    int ind;

    // mingw (with some versions) disallows w+ (EINVAL)
    out = fopen(TMP, "w+");
    if (errno) {
        perror("fopen w+");
        fclose(out);
        out = fopen(TMP, "w");
        win_wplus = 1;
    }

    /*--------------------------------------------------*/
    print_msvcrt(use_msvcrt);

    rc = fprintf_s(NULL, "%s", (char*)NULL);
    init_msvcrt(rc == -ESNULLP, &use_msvcrt);
    NEGERR_MSVC(ESNULLP, EOF);

    /*--------------------------------------------------*/

    /* wine msvcrt doesn't check fmt==NULL */
#if !(defined(_WINE_MSVCRT) && defined(TEST_MSVCRT) && defined(HAVE_FPRINTF_S))
#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty fmt")
    rc = fprintf_s(out, NULL);
    NEGERR_MSVC(ESNULLP, EOF);
#endif
#endif

    /*--------------------------------------------------*/

    str[0] = '\0';
    rc = fprintf_s(out, "%s %n", str, &ind);
    NEGERR_MSVC(EINVAL, EOF);

    if (!out) {
        printf("Failed to open file %s for write: %s\n", TMP, strerror(errno));
        return errs;
    }

    /*--------------------------------------------------*/

    rc = fprintf_s(out, "%s %%n", str);
    ERR(3)

    rc = fprintf_s(out, "%%n");
    ERR(2);

    /*--------------------------------------------------*/

    rc = fprintf_s(out, "%s", (char*)NULL);
    NEGERR(ESNULLP)

    /*--------------------------------------------------*/

    strcpy(str, "keep it simple");

    rc = fprintf_s(out, "%s", str);
    NOERR()

    rc = fprintf_s(out, "%d", -10000);
    ERR(6)

    rc = fprintf_s(out, "%ld", -10000L);
    NOERR()

    rewind(out);
    nread = ftruncate(fileno(out), 0L);
    (void)nread;

    /*--------------------------------------------------*/

    str[0] = '\0';

    rc = fprintf_s(out, "%s", str);
    ERR(0)
    cmp_and_reset(out, "");

    /*--------------------------------------------------*/

    strcpy(str, "keep it simple");

    rc = fprintf_s(out, "%s", str);
    ERR(strlen(str))
    cmp_and_reset(out, "keep it simple");

    /*--------------------------------------------------*/

    fclose(out);

    /* print to closed stream: unportable, and not valgrind-safe */
#ifndef __GLIBC__
    rc = fprintf_s(out, "%s", str);
#if defined(__GLIBC__)
    if (rc < 0) {
        ERR(-1);
        ERRNO(EBADF);
    } else {
        ERR(14); /* older glibc upstream bug */
        ERRNO(0);
    }
#else
    /* musl returns 14, all other -1 */
    if (rc != 14 && rc != -1) {
        debug_printf("%s %u  Error rc=%d \n", __FUNCTION__, __LINE__, (int)rc);
        errs++;
    }
#endif
    /* musl throws no error */
#endif

    unlink(TMP);

    return (errs);
}

int main(void) { return (test_fprintf_s()); }