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

aremmell / libsir / 301

29 Aug 2023 11:24PM UTC coverage: 94.928% (+0.04%) from 94.892%
301

Pull #239

gitlab-ci

johnsonjh
Only undef recursive glibc macro hacks during linting

Signed-off-by: Jeffrey H. Johnson <trnsz@pobox.com>
Pull Request #239: Enable Oracle Lint and additional compiler warnings

10 of 10 new or added lines in 4 files covered. (100.0%)

3032 of 3194 relevant lines covered (94.93%)

602860.87 hits per line

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

91.12
/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) {
255,197✔
30
    bool valid = NULL != pp;
255,197✔
31
    if (!valid && fail) {
255,197✔
32
        (void)_sir_seterror(_SIR_E_NULLPTR);
×
33
        SIR_ASSERT(!(bool)"NULL pointer!");
×
34
    }
35
    return valid;
255,318✔
36
}
37

38
bool __sir_validptr(const void* restrict p, bool fail) {
72,052,682✔
39
    bool valid = NULL != p;
72,052,682✔
40
    if (!valid && fail) {
72,052,682✔
41
        (void)_sir_seterror(_SIR_E_NULLPTR);
154✔
42
        SIR_ASSERT(!(bool)"NULL pointer!");
147✔
43
    }
44
    return valid;
72,058,350✔
45
}
46

47
void __sir_safefree(void** pp) {
887,429✔
48
    if (!pp || !*pp)
887,429✔
49
        return;
232,827✔
50

51
    free(*pp);
612,089✔
52
    *pp = NULL;
612,089✔
53
}
54

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

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

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

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

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

72
    *f = NULL;
36,504✔
73
}
74

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

80
#if !defined(__WIN__)
81
    int ret = fcntl(fd, F_GETFL);
40✔
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);
40✔
93
}
94

95
/** Validates a sir_update_config_data structure. */
96
bool _sir_validupdatedata(sir_update_config_data* data) {
142,617✔
97
    if (!_sir_validptr(data))
142,617✔
98
        return false;
×
99

100
    bool valid = true;
118,454✔
101
    if ((data->fields & SIRU_ALL) == 0u ||
142,596✔
102
          (data->fields & ~SIRU_ALL) != 0u)
118,463✔
103
        valid = false;
×
104

105
    if (valid && _sir_bittest(data->fields, SIRU_LEVELS))
142,598✔
106
        valid &= (_sir_validptrnofail(data->levels) &&
2,524✔
107
            _sir_validlevels(*data->levels));
1,262✔
108

109
    if (valid && _sir_bittest(data->fields, SIRU_OPTIONS))
142,576✔
110
        valid &= (_sir_validptrnofail(data->opts) &&
281,783✔
111
            _sir_validopts(*data->opts));
140,944✔
112

113
    if (valid && _sir_bittest(data->fields, SIRU_SYSLOG_ID))
142,195✔
114
        valid &= _sir_validstrnofail(data->sl_identity);
116✔
115

116
    if (valid && _sir_bittest(data->fields, SIRU_SYSLOG_CAT))
142,099✔
117
        valid &= _sir_validstrnofail(data->sl_category);
92✔
118

119
    if (!valid) {
142,108✔
120
        (void)_sir_seterror(_SIR_E_INVALID);
32✔
121
        SIR_ASSERT(!(bool)"invalid sir_update_config_data!");
×
122
    }
123

124
    return valid;
118,242✔
125
}
126

127
bool _sir_validlevels(sir_levels levels) {
77,071✔
128
    if ((SIRL_ALL == levels || SIRL_NONE == levels) ||
78,754✔
129
        ((_sir_bittest(levels, SIRL_INFO)           ||
3,160✔
130
         _sir_bittest(levels, SIRL_DEBUG)           ||
2,329✔
131
         _sir_bittest(levels, SIRL_NOTICE)          ||
1,910✔
132
         _sir_bittest(levels, SIRL_WARN)            ||
1,573✔
133
         _sir_bittest(levels, SIRL_ERROR)           ||
1,057✔
134
         _sir_bittest(levels, SIRL_CRIT)            ||
582✔
135
         _sir_bittest(levels, SIRL_ALERT)           ||
354✔
136
         _sir_bittest(levels, SIRL_EMERG))          &&
2,062✔
137
         ((levels & ~SIRL_ALL) == 0u)))
138
         return true;
64,078✔
139

140
    _sir_selflog("invalid levels: %04"PRIx16, levels);
94✔
141
    return _sir_seterror(_SIR_E_LEVELS);
99✔
142
}
143

144
bool _sir_validlevel(sir_level level) {
4,550,981✔
145
    if (SIRL_INFO   == level || SIRL_DEBUG == level ||
4,550,981✔
146
        SIRL_NOTICE == level || SIRL_WARN  == level ||
7,137✔
147
        SIRL_ERROR  == level || SIRL_CRIT  == level ||
4,265✔
148
        SIRL_ALERT  == level || SIRL_EMERG == level)
1,428✔
149
        return true;
4,462,919✔
150

151
    _sir_selflog("invalid level: %04"PRIx16, level);
21✔
152
    return _sir_seterror(_SIR_E_LEVELS);
22✔
153
}
154

155
#if defined(__clang__) /* only Clang has implicit-conversion; GCC BZ#87454 */
156
SANITIZE_SUPPRESS("implicit-conversion")
157
#endif
158
bool _sir_validopts(sir_options opts) {
216,951✔
159
    if ((SIRO_ALL == opts || SIRO_MSGONLY == opts) ||
335,908✔
160
        ((_sir_bittest(opts, SIRO_NOTIME)          ||
231,117✔
161
         _sir_bittest(opts, SIRO_NOHOST)           ||
106,077✔
162
         _sir_bittest(opts, SIRO_NOLEVEL)          ||
672✔
163
         _sir_bittest(opts, SIRO_NONAME)           ||
550✔
164
         _sir_bittest(opts, SIRO_NOMSEC)           ||
430✔
165
         _sir_bittest(opts, SIRO_NOPID)            ||
305✔
166
         _sir_bittest(opts, SIRO_NOTID)            ||
186✔
167
         _sir_bittest(opts, SIRO_NOHDR))           &&
88✔
168
         ((opts & ~(SIRO_MSGONLY | SIRO_NOHDR)) == 0u))) /* implicit-conversion */
143,162✔
169
         return true;
179,906✔
170

171
    _sir_selflog("invalid options: %08"PRIx32, opts);
181✔
172
    return _sir_seterror(_SIR_E_OPTIONS);
323✔
173
}
174

175
bool _sir_validtextattr(sir_textattr attr) {
165,822✔
176
    switch(attr) {
165,822✔
177
        case SIRTA_NORMAL:
138,524✔
178
        case SIRTA_BOLD:
179
        case SIRTA_DIM:
180
        case SIRTA_EMPH:
181
        case SIRTA_ULINE:
182
            return true;
138,524✔
183
        default: {
22✔
184
            _sir_selflog("invalid text attr: %d", attr);
21✔
185
            return _sir_seterror(_SIR_E_TEXTATTR);
22✔
186
        }
187
    }
188
}
189

190
bool _sir_validtextcolor(sir_colormode mode, sir_textcolor color) {
331,578✔
191
    bool valid = false;
277,029✔
192
    switch (mode) {
331,578✔
193
        case SIRCM_16:
308,346✔
194
            /* in 16-color mode:
195
             * compare to 30..37, 39, 40..47, 49, 90..97, 100..107. */
196
            valid = SIRTC_DEFAULT == color ||
308,346✔
197
                    (color >= 30u && color <= 37u) || color == 39u ||
308,346✔
198
                    (color >= 40u && color <= 47u) || color == 49u ||
132,455✔
199
                    (color >= 90u && color <= 97u) || (color >= 100u && color <= 107u);
513,930✔
200
            break;
256,965✔
201
        case SIRCM_256:
11,572✔
202
            /* in 256-color mode: compare to 0..255. sir_textcolor is unsigned,
203
             * so only need to ensure it's <= 255. */
204
            valid = SIRTC_DEFAULT == color || color <= 255u;
11,572✔
205
            break;
11,572✔
206
        case SIRCM_RGB: {
11,660✔
207
            /* in RGB-color mode: mask and compare to 0x00ffffff. */
208
            valid = SIRTC_DEFAULT == color || ((color & 0xff000000u) == 0u);
11,660✔
209
            break;
11,660✔
210
        }
211
        case SIRCM_INVALID: // GCOVR_EXCL_START
212
        default:
213
            valid = false;
214
            break;
215
    } // GCOVR_EXCL_STOP
216

217
    if (!valid) {
280,197✔
218
        _sir_selflog("invalid text color for mode %d %08"PRIx32" (%"PRIu32")",
21✔
219
            mode, color, color);
220
        (void)_sir_seterror(_SIR_E_TEXTCOLOR);
22✔
221
    }
222

223
    return valid;
331,578✔
224
}
225

226
bool _sir_validcolormode(sir_colormode mode) {
166,622✔
227
    switch (mode) {
166,622✔
228
        case SIRCM_16:
139,213✔
229
        case SIRCM_256:
230
        case SIRCM_RGB:
231
            return true;
139,213✔
232
        case SIRCM_INVALID:
22✔
233
        default: {
234
            _sir_selflog("invalid color mode: %d", mode);
21✔
235
            return _sir_seterror(_SIR_E_COLORMODE);
22✔
236
        }
237
    }
238
}
239

240
bool __sir_validstr(const char* restrict str, bool fail) {
45,322,079✔
241
    bool valid = str && (*str != '\0');
45,322,079✔
242
    if (!valid && fail) {
45,322,079✔
243
        (void)_sir_seterror(_SIR_E_STRING);
19,032✔
244
        SIR_ASSERT(!(bool)"invalid string!");
19,027✔
245
    }
246
    return valid;
45,324,804✔
247
}
248

249
int _sir_strncpy(char* restrict dest, size_t destsz, const char* restrict src, size_t count) {
2,173,225✔
250
    if (_sir_validptr(dest) && _sir_validstr(src)) {
2,173,225✔
251
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
252
        int ret = strncpy_s(dest, destsz, src, count);
253
        if (0 != ret) {
254
            (void)_sir_handleerr(ret);
255
            return -1;
256
        }
257
        return 0;
258
#else
259
        SIR_UNUSED(count);
260
        size_t cpy = strlcpy(dest, src, destsz);
2,149,373✔
261
        SIR_ASSERT_UNUSED(cpy < destsz, cpy);
2,165,271✔
262
        return 0;
2,173,220✔
263
#endif
264
    }
265

266
    return -1;
5✔
267
}
268

269
int _sir_strncat(char* restrict dest, size_t destsz, const char* restrict src, size_t count) {
25,149,858✔
270
    if (_sir_validptr(dest) && _sir_validstr(src)) {
25,149,858✔
271
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
272
        int ret = strncat_s(dest, destsz, src, count);
273
        if (0 != ret) {
274
            (void)_sir_handleerr(ret);
275
            return -1;
276
        }
277
        return 0;
278
#else
279
        SIR_UNUSED(count);
280
        size_t cat = strlcat(dest, src, destsz);
24,769,414✔
281
        SIR_ASSERT_UNUSED(cat < destsz, cat);
25,014,680✔
282
        return 0;
25,131,820✔
283
#endif
284
    }
285

286
    return -1;
18,893✔
287
}
288

289
int _sir_fopen(FILE* restrict* restrict streamptr, const char* restrict filename,
36,594✔
290
    const char* restrict mode) {
291
    if (_sir_validptrptr(streamptr) && _sir_validstr(filename) && _sir_validstr(mode)) {
36,594✔
292
#if defined(__HAVE_STDC_SECURE_OR_EXT1__)
293
        int ret = fopen_s(streamptr, filename, mode);
294
        if (0 != ret) {
295
            (void)_sir_handleerr(ret);
296
            return -1;
297
        }
298
        return 0;
299
#else
300
        *streamptr = fopen(filename, mode);
36,594✔
301
        if (!*streamptr) {
36,594✔
302
            (void)_sir_handleerr(errno);
69✔
303
            return -1;
69✔
304
        }
305
        return 0;
30,369✔
306
#endif
307
    }
308

309
    return -1;
×
310
}
311

312
struct tm* _sir_localtime(const time_t* restrict timer, struct tm* restrict buf) {
2,174,484✔
313
    if (_sir_validptr(timer) && _sir_validptr(buf)) {
2,174,484✔
314
#if defined(__HAVE_STDC_SECURE_OR_EXT1__) && !defined(__EMBARCADEROC__)
315
# if !defined(__WIN__)
316
        struct tm* ret = localtime_s(timer, buf);
317
        if (!ret) {
318
            (void)_sir_handleerr(errno);
319
            return NULL;
320
        }
321
# else /* __WIN__ */
322
        errno_t ret = localtime_s(buf, timer);
323
        if (0 != ret) {
324
            (void)_sir_handleerr(ret);
325
            return NULL;
326
        }
327
# endif
328

329
        return buf;
330
#else /* !__HAVE_STDC_SECURE_OR_EXT1__ */
331
# if !defined(__WIN__) || defined(__EMBARCADEROC__)
332
        struct tm* ret = localtime_r(timer, buf);
2,174,484✔
333
# else
334
        struct tm* ret = localtime(timer);
335
# endif
336
        if (!ret)
2,174,484✔
337
            (void)_sir_handleerr(errno);
×
338

339
        return ret;
2,174,484✔
340
#endif
341
    }
342

343
    return NULL;
×
344
}
345

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

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

362
    memcpy(&new, &cur, sizeof(struct termios));
×
363
    new.c_lflag &= ~(ICANON | ECHO);
1✔
364

365
    if (0 != tcsetattr(STDIN_FILENO, TCSANOW, &new))
1✔
366
        return _sir_handleerr(errno);
×
367

368
    *input = (char)getchar();
1✔
369

370
    return 0 == tcsetattr(STDIN_FILENO, TCSANOW, &cur) ? true
1✔
371
        : _sir_handleerr(errno);
1✔
372
#endif
373
}
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