File: test_getenv_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 (159 lines) | stat: -rw-r--r-- 3,682 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
/*------------------------------------------------------------------
 * test_getenv_s
 * File 'os/getenv_s.c'
 * Lines executed:95.24% of 42
 *
 *------------------------------------------------------------------
 */

#include "test_private.h"
#include "safe_lib.h"
#include <stdlib.h>

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

#if RSIZE_MAX_STR < 4090
#define LEN (RSIZE_MAX_STR)
#else
#define LEN (4090)
#endif

static char dest[LEN];
int test_getenv_s(void);

int test_getenv_s(void) {
    errno_t rc;
    int errs = 0;
    int ind;
    size_t len;
    char *str2;
    const char *name = "PATH";

    /*--------------------------------------------------*/
    /* Note that native getenv_s with MSVC looks completely broken for
       most cases. Maybe we should force our implementation there. */

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("empty name")
    rc = getenv_s(&len, dest, LEN, NULL);
    if (use_msvcrt && rc == ESNULLP) {
        printf("safec.dll overriding msvcrt.dll\n");
        use_msvcrt = false;
    }
    ERR_MSVC(ESNULLP, 0);
    ind = len;
    if (!use_msvcrt) {
        INDCMP(!= 0);
    }

    EXPECT_BOS("empty name")
    rc = getenv_s(&len, NULL, 0, NULL);
    ERR_MSVC(ESNULLP, EINVAL);
    ind = len;
    if (!use_msvcrt) {
        INDCMP(!= 0);
    }
#endif

    /*--------------------------------------------------*/
    /* cases where name exists */

    str2 = getenv(name); // for PATH
    rc = getenv_s(&len, NULL, 0, name);
    ERR_MSVC(0, EINVAL);
    ind = strlen(str2);
#ifdef _WIN32
    // microsoft returns the size, not the len. so do we now
    INDCMP(!= (int)len-1);
#else
    INDCMP(!= (int)len);
#endif

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

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("dest overflow or empty")
    rc = getenv_s(&len, dest, RSIZE_MAX_STR + 1, name);
    ERR_MSVC(ESLEMAX, 0);
    ind = len;
    if (!use_msvcrt) {
        INDCMP(!= 0);
    }
#endif

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

    rc = getenv_s(&len, dest, 1, name);
    ERR_MSVC(ESNOSPC, 34);
    if (len != 0) {
        printf("%s %u  Error len=%u rc=%d \n", __FUNCTION__, __LINE__,
               (unsigned)len, rc);
        errs++;
    }

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

    rc = getenv_s(&len, dest, LEN, name);
    ERR(EOK);
    EXPSTR(dest, str2);
    ind = strlen(str2);
#ifdef _WIN32
    INDCMP(!= (int)len-1);
#else
    INDCMP(!= (int)len);
#endif

    rc = getenv_s(NULL, dest, LEN, name);
    ERR_MSVC(EOK, EINVAL);
    EXPSTR(dest, str2);

    rc = getenv_s(&len, dest, LEN, name);
    ERR(EOK);
    EXPSTR(dest, str2);
    ind = strlen(str2);
#ifdef _WIN32
    INDCMP(!= (int)len-1);
#else
    INDCMP(!= (int)len);
#endif

#ifndef HAVE_CT_BOS_OVR
    EXPECT_BOS("dest overflow or empty")
    rc = getenv_s(&len, NULL, LEN, name);
    ERR_MSVC(ESNULLP, EINVAL); // with NULL dmax must be 0
    ind = len;
    if (!use_msvcrt) {
        INDCMP(!= 0);
    }
#endif

    /*--------------------------------------------------*/
    /* cases where name does not exist */

    rc = getenv_s(NULL, dest, LEN, "c#hewhc&wehc%erwhc$weh");
    ERR_MSVC(-1, EINVAL);
    if (!use_msvcrt) {
        EXPNULL(dest);
    }

    rc = getenv_s(&len, dest, LEN, "c#hewhc&wehc%erwhc$weh");
    ERR_MSVC(-1, 0);
    ind = len;
    INDCMP(!= 0);
    EXPNULL(dest);

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

    return (errs);
}

#ifndef __KERNEL__
/* simple hack to get this to work for both userspace and Linux kernel,
   until a better solution can be created. */
int main(void) { return (test_getenv_s()); }
#endif