File: test_fprintf_s.c

package info (click to toggle)
safeclib 3.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,648 kB
  • sloc: ansic: 51,375; makefile: 1,259; perl: 528; sh: 486
file content (133 lines) | stat: -rw-r--r-- 2,891 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
/*------------------------------------------------------------------
 * 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)

static FILE *out;
static char str[LEN];
int test_fprintf_s(void);

int test_fprintf_s(void) {
    errno_t rc;
    int ind;
    int errs = 0;

    out = fopen(TMP, "w");

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

    rc = fprintf_s(NULL, "%s", 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", 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()

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

    str[0] = '\0';

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

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

    strcpy(str, "keep it simple");

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

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

    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()); }