• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aremmell / libsir / 212

24 Aug 2023 01:36PM UTC coverage: 94.835% (+0.002%) from 94.833%
212

Pull #225

gitlab-ci

johnsonjh
Support building with Oracle C++

Tested on Oracle Studio C++ on Linux

Signed-off-by: Jeffrey H. Johnson <trnsz@pobox.com>
Pull Request #225: Portability enhancements

97 of 97 new or added lines in 11 files covered. (100.0%)

3048 of 3214 relevant lines covered (94.84%)

1608268.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.22
/src/sirhelpers.c
1
/*
2
 * sirhelpers.c
3
 *
4
 * Author:    Ryan M. Lederman <lederman@gmail.com>
5
 * Copyright: Copyright (c) 2018-2023
6
 * Version:   2.2.3
7
 * License:   The MIT License (MIT)
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
10
 * this software and associated documentation files (the "Software"), to deal in
11
 * the Software without restriction, including without limitation the rights to
12
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13
 * the Software, and to permit persons to whom the Software is furnished to do so,
14
 * subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 */
26
#include "sir/helpers.h"
27
#include "sir/errors.h"
28

29
bool __sir_validptrptr(const void* restrict* pp, bool fail) {
244,857✔
30
    bool valid = NULL != pp;
244,857✔
31
    if (!valid && fail) {
244,857✔
32
        (void)_sir_seterror(_SIR_E_NULLPTR);
×
33
        SIR_ASSERT("!NULL pointer");
34
    }
35
    return valid;
244,867✔
36
}
37

38
bool __sir_validptr(const void* restrict p, bool fail) {
71,783,968✔
39
    bool valid = NULL != p;
71,783,968✔
40
    if (!valid && fail) {
71,783,968✔
41
        (void)_sir_seterror(_SIR_E_NULLPTR);
147✔
42
        SIR_ASSERT(!"NULL pointer");
140✔
43
    }
44
    return valid;
71,785,705✔
45
}
46

47
void __sir_safefree(void** pp) {
832,944✔
48
    if (!pp || !*pp)
832,944✔
49
        return;
222,778✔
50

51
    free(*pp);
567,725✔
52
    *pp = NULL;
567,725✔
53
}
54

55
void _sir_safeclose(int* restrict fd) {
174✔
56
    if (!fd || 0 > *fd)
174✔
57
        return;
×
58

59
    if (-1 == close(*fd))
174✔
60
        (void)_sir_handleerr(errno);
×
61

62
    *fd = -1;
174✔
63
}
64

65
void _sir_safefclose(FILE* restrict* restrict f) {
34,734✔
66
    if (!f || !*f)
34,734✔
67
        return;
4✔
68

69
    if (0 != fclose(*f))
34,730✔
70
        (void)_sir_handleerr(errno);
×
71

72
    *f = NULL;
34,730✔
73
}
74

75
bool _sir_validfd(int fd) {
101✔
76
    /** stdin, stdout, stderr use up 0, 1, 2 */
77
    if (2 >= fd)
101✔
78
        return _sir_handleerr(EBADF);
63✔
79

80
#if !defined(__WIN__)
81
    int ret = fcntl(fd, F_GETFL);
38✔
82
#else /* __WIN__ */
83
# if !defined(SIR_MSVCRT_MINGW)
84
    invalparamfn old = _set_thread_local_invalid_parameter_handler(_sir_invalidparameter);
85
# endif
86
    struct _stat st;
87
    int ret = _fstat(fd, &st);
88
# if !defined(SIR_MSVCRT_MINGW)
89
    _set_thread_local_invalid_parameter_handler(old);
90
# endif
91
#endif
92
    return (-1 != ret || EBADF != errno) ? true : _sir_handleerr(errno);
38✔
93
}
94

95
/** Validates a sir_update_config_data structure. */
96
#if defined(__clang__) && !defined(__EMBARCADEROC__)
97
/* only Clang has implicit-conversion; GCC BZ#87454 */
98
SANITIZE_SUPPRESS("implicit-conversion")
99
#endif
100
bool _sir_validupdatedata(sir_update_config_data* data) {
137,767✔
101
    if (!_sir_validptr(data))
137,767✔
102
        return false;
×
103

104
    bool valid = true;
113,362✔
105
    if ((data->fields & SIRU_ALL) == 0 ||
137,734✔
106
          (data->fields & ~SIRU_ALL) != 0) /* implicit-conversion */
113,373✔
107
        valid = false;
×
108

109
    if (valid && _sir_bittest(data->fields, SIRU_LEVELS))
137,733✔
110
        valid &= (_sir_validptrnofail(data->levels) &&
2,404✔
111
            _sir_validlevels(*data->levels));
1,202✔
112

113
    if (valid && _sir_bittest(data->fields, SIRU_OPTIONS))
137,719✔
114
        valid &= (_sir_validptrnofail(data->opts) &&
272,420✔
115
            _sir_validopts(*data->opts));
136,241✔
116

117
    if (valid && _sir_bittest(data->fields, SIRU_SYSLOG_ID))
137,547✔
118
        valid &= _sir_validstrnofail(data->sl_identity);
110✔
119

120
    if (valid && _sir_bittest(data->fields, SIRU_SYSLOG_CAT))
137,486✔
121
        valid &= _sir_validstrnofail(data->sl_category);
92✔
122

123
    if (!valid) {
137,459✔
124
        (void)_sir_seterror(_SIR_E_INVALID);
19✔
125
        SIR_ASSERT("!invalid sir_update_config_data");
126
    }
127

128
    return valid;
113,159✔
129
}
130

131
bool _sir_validlevels(sir_levels levels) {
73,334✔
132
    if ((SIRL_ALL == levels || SIRL_NONE == levels) ||
74,920✔
133
        ((_sir_bittest(levels, SIRL_INFO)           ||
2,999✔
134
         _sir_bittest(levels, SIRL_DEBUG)           ||
2,215✔
135
         _sir_bittest(levels, SIRL_NOTICE)          ||
1,819✔
136
         _sir_bittest(levels, SIRL_WARN)            ||
1,496✔
137
         _sir_bittest(levels, SIRL_ERROR)           ||
1,003✔
138
         _sir_bittest(levels, SIRL_CRIT)            ||
549✔
139
         _sir_bittest(levels, SIRL_ALERT)           ||
330✔
140
         _sir_bittest(levels, SIRL_EMERG))          &&
1,957✔
141
         ((levels & ~SIRL_ALL) == 0)))
142
         return true;
60,488✔
143

144
    _sir_selflog("invalid levels: %04" PRIx16, levels);
89✔
145
    return _sir_seterror(_SIR_E_LEVELS);
94✔
146
}
147

148
bool _sir_validlevel(sir_level level) {
4,527,155✔
149
    if (SIRL_INFO   == level || SIRL_DEBUG == level ||
4,527,155✔
150
        SIRL_NOTICE == level || SIRL_WARN  == level ||
6,687✔
151
        SIRL_ERROR  == level || SIRL_CRIT  == level ||
3,996✔
152
        SIRL_ALERT  == level || SIRL_EMERG == level)
1,338✔
153
        return true;
4,439,065✔
154

155
    _sir_selflog("invalid level: %04" PRIx32, level);
20✔
156
    return _sir_seterror(_SIR_E_LEVELS);
21✔
157
}
158

159
#if defined(__clang__) && !defined(__EMBARCADEROC__)
160
/* only Clang has implicit-conversion; GCC BZ#87454 */
161
SANITIZE_SUPPRESS("implicit-conversion")
162
#endif
163
bool _sir_validopts(sir_options opts) {
208,537✔
164
    if ((SIRO_ALL == opts || SIRO_MSGONLY == opts) ||
322,651✔
165
        ((_sir_bittest(opts, SIRO_NOTIME)          ||
222,870✔
166
         _sir_bittest(opts, SIRO_NOHOST)           ||
102,763✔
167
         _sir_bittest(opts, SIRO_NOLEVEL)          ||
632✔
168
         _sir_bittest(opts, SIRO_NONAME)           ||
522✔
169
         _sir_bittest(opts, SIRO_NOMSEC)           ||
414✔
170
         _sir_bittest(opts, SIRO_NOPID)            ||
293✔
171
         _sir_bittest(opts, SIRO_NOTID)            ||
177✔
172
         _sir_bittest(opts, SIRO_NOHDR))           &&
84✔
173
         ((opts & ~(SIRO_MSGONLY | SIRO_NOHDR)) == 0))) /* implicit-conversion */
138,446✔
174
         return true;
171,502✔
175

176
    _sir_selflog("invalid options: %08" PRIx32, opts);
274✔
177
    return _sir_seterror(_SIR_E_OPTIONS);
309✔
178
}
179

180
bool _sir_validtextattr(sir_textattr attr) {
158,745✔
181
    switch(attr) {
158,745✔
182
        case SIRTA_NORMAL:
131,448✔
183
        case SIRTA_BOLD:
184
        case SIRTA_DIM:
185
        case SIRTA_EMPH:
186
        case SIRTA_ULINE:
187
            return true;
131,448✔
188
        default: {
21✔
189
            _sir_selflog("invalid text attr: %d", attr);
20✔
190
            return _sir_seterror(_SIR_E_TEXTATTR);
21✔
191
        }
192
    }
193
}
194

195
bool _sir_validtextcolor(sir_colormode mode, sir_textcolor color) {
317,427✔
196
    bool valid = false;
262,878✔
197
    switch (mode) {
317,427✔
198
        case SIRCM_16:
295,251✔
199
            /* in 16-color mode:
200
             * compare to 30..37, 39, 40..47, 49, 90..97, 100..107. */
201
            valid = SIRTC_DEFAULT == color ||
295,251✔
202
                    (color >= 30 && color <= 37) || color == 39 ||
295,251✔
203
                    (color >= 40 && color <= 47) || color == 49 ||
125,700✔
204
                    (color >= 90 && color <= 97) || (color >= 100 && color <= 107);
487,740✔
205
            break;
243,870✔
206
        case SIRCM_256:
11,046✔
207
            /* in 256-color mode: compare to 0..255. sir_textcolor is unsigned,
208
             * so only need to ensure it's <= 255. */
209
            valid = SIRTC_DEFAULT == color || color <= 255;
11,046✔
210
            break;
11,046✔
211
        case SIRCM_RGB: {
11,130✔
212
            /* in RGB-color mode: mask and compare to 0x00ffffff. */
213
            valid = SIRTC_DEFAULT == color || ((color & 0xff000000) == 0);
11,130✔
214
            break;
11,130✔
215
        }
216
        case SIRCM_INVALID: // GCOVR_EXCL_START
217
        default:
218
            valid = false;
219
            break;
220
    } // GCOVR_EXCL_STOP
221

222
    if (!valid) {
266,046✔
223
        _sir_selflog("invalid text color for mode %d %08" PRIx32" (%" PRIu32")",
20✔
224
            mode, color, color);
225
        (void)_sir_seterror(_SIR_E_TEXTCOLOR);
21✔
226
    }
227

228
    return valid;
317,427✔
229
}
230

231
bool _sir_validcolormode(sir_colormode mode) {
159,509✔
232
    switch (mode) {
159,509✔
233
        case SIRCM_16:
132,101✔
234
        case SIRCM_256:
235
        case SIRCM_RGB:
236
            return true;
132,101✔
237
        case SIRCM_INVALID:
21✔
238
        default: {
239
            _sir_selflog("invalid color mode: %d", mode);
20✔
240
            return _sir_seterror(_SIR_E_COLORMODE);
21✔
241
        }
242
    }
243
}
244

245
bool __sir_validstr(const char* restrict str, bool fail) {
45,129,446✔
246
    bool valid = str && (*str != '\0');
45,129,446✔
247
    if (!valid && fail) {
45,129,446✔
248
        (void)_sir_seterror(_SIR_E_STRING);
19,904✔
249
        SIR_ASSERT(!"invalid string");
19,900✔
250
    }
251
    return valid;
45,130,306✔
252
}
253

254
int _sir_strncpy(char* restrict dest, size_t destsz, const char* restrict src, size_t count) {
2,165,446✔
255
    if (_sir_validptr(dest) && _sir_validstr(src)) {
2,165,446✔
256
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
257
        int ret = strncpy_s(dest, destsz, src, count);
258
        if (0 != ret) {
259
            (void)_sir_handleerr(ret);
260
            return -1;
261
        }
262
        return 0;
263
#else
264
        SIR_UNUSED(count);
265
        size_t cpy = strlcpy(dest, src, destsz);
2,165,439✔
266
        SIR_ASSERT_UNUSED(cpy < destsz, cpy);
2,157,491✔
267
        return 0;
2,165,440✔
268
#endif
269
    }
270

271
    return -1;
6✔
272
}
273

274
int _sir_strncat(char* restrict dest, size_t destsz, const char* restrict src, size_t count) {
25,142,203✔
275
    if (_sir_validptr(dest) && _sir_validstr(src)) {
25,142,203✔
276
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
277
        int ret = strncat_s(dest, destsz, src, count);
278
        if (0 != ret) {
279
            (void)_sir_handleerr(ret);
280
            return -1;
281
        }
282
        return 0;
283
#else
284
        SIR_UNUSED(count);
285
        size_t cat = strlcat(dest, src, destsz);
25,121,369✔
286
        SIR_ASSERT_UNUSED(cat < destsz, cat);
25,000,120✔
287
        return 0;
25,123,333✔
288
#endif
289
    }
290

291
    return -1;
19,769✔
292
}
293

294
int _sir_fopen(FILE* restrict* restrict streamptr, const char* restrict filename,
34,817✔
295
    const char* restrict mode) {
296
    if (_sir_validptrptr(streamptr) && _sir_validstr(filename) && _sir_validstr(mode)) {
34,817✔
297
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
298
        int ret = fopen_s(streamptr, filename, mode);
299
        if (0 != ret) {
300
            (void)_sir_handleerr(ret);
301
            return -1;
302
        }
303
        return 0;
304
#else
305
        *streamptr = fopen(filename, mode);
34,817✔
306
        if (!*streamptr) {
34,817✔
307
            (void)_sir_handleerr(errno);
67✔
308
            return -1;
67✔
309
        }
310
        return 0;
28,665✔
311
#endif
312
    }
313

314
    return -1;
×
315
}
316

317
struct tm* _sir_localtime(const time_t* restrict timer, struct tm* restrict buf) {
2,165,767✔
318
    if (_sir_validptr(timer) && _sir_validptr(buf)) {
2,165,767✔
319
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
320
# if defined(__WIN__)
321
        errno_t ret = (errno_t)localtime_s(buf, timer);
322
        if (0 != ret) {
323
            (void)_sir_handleerr(ret);
324
            return NULL;
325
        }
326

327
        return buf;
328
# else /* __WIN__ */
329
        struct tm* ret = localtime_s(timer, buf);
330
        if (!ret)
331
            (void)_sir_handleerr(errno);
332

333
        return ret;
334
# endif
335
#else
336
        SIR_UNUSED(buf);
337
        struct tm* ret = localtime(timer);
2,165,767✔
338
        if (!ret)
2,165,769✔
339
            (void)_sir_handleerr(errno);
×
340

341
        return ret;
2,165,769✔
342
#endif
343
    }
344

345
    return NULL;
×
346
}
347

348
bool _sir_getchar(char* input) {
1✔
349
    if (!_sir_validptr(input))
1✔
350
        return false;
×
351

352
#if defined(__WIN__)
353
# if defined(__EMBARCADEROC__)
354
     *input = (char)getch();
355
# else
356
     *input = (char)_getch();
357
# endif
358
     return true;
359
#else /* !__WIN__ */
360
    struct termios cur = {0}, tnew = {0};
1✔
361
    if (0 != tcgetattr(STDIN_FILENO, &cur))
1✔
362
        return _sir_handleerr(errno);
×
363

364
    memcpy(&tnew, &cur, sizeof(struct termios));
×
365
    tnew.c_lflag &= ~(ICANON | ECHO);
1✔
366

367
    if (0 != tcsetattr(STDIN_FILENO, TCSANOW, &tnew))
1✔
368
        return _sir_handleerr(errno);
×
369

370
    *input = (char)getchar();
1✔
371

372
    return 0 == tcsetattr(STDIN_FILENO, TCSANOW, &cur) ? true
1✔
373
        : _sir_handleerr(errno);
1✔
374
#endif
375
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc