Skip to content

Commit 3dd0431

Browse files
committed
Valgrind and platform fixes
1 parent 9f0c659 commit 3dd0431

10 files changed

Lines changed: 1584 additions & 1527 deletions

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.6.0
3+
rev: v6.0.0
44
hooks:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ c89stringutils
22
==============
33
[![License](https://img.shields.io/badge/license-Apache--2.0%20OR%20MIT-blue.svg)](https://opensource.org/licenses/Apache-2.0)
44
![Doc Coverage](https://img.shields.io/badge/doc__coverage-100.00%25-brightgreen)
5-
![Test Coverage](https://img.shields.io/badge/test__coverage-0.00%25-brightgreen)
5+
![Test Coverage](https://img.shields.io/badge/test__coverage-96.29%25-brightgreen)
66
[![CI for Linux, Windows, macOS](https://github.com/offscale/c89stringutils/actions/workflows/ci.yml/badge.svg)](https://github.com/offscale/c89stringutils/actions/workflows/ci.yml)
77
[![C89](https://img.shields.io/badge/C-89-blue)](https://en.wikipedia.org/wiki/C89_(C_version))
88

c89stringutils/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ if (C89STRINGUTILS_BUILD_AMALGAMATION)
7676
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/c89stringutils_safecrt.h" amalg_safecrt_header)
7777
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/c89stringutils_string_extras.h" amalg_header)
7878
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/c89stringutils_safecrt.c" amalg_safecrt_source)
79+
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/stb_sprintf.h" amalg_stb)
7980
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/c89stringutils_string_extras.c" amalg_source)
8081
file(READ "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.h" amalg_config)
8182

@@ -90,14 +91,15 @@ if (C89STRINGUTILS_BUILD_AMALGAMATION)
9091
string(REPLACE "#include \"c89stringutils_string_extras.h\"" "" amalg_source "${amalg_source}")
9192
string(REPLACE "#include \"c89stringutils_safecrt.h\"" "" amalg_source "${amalg_source}")
9293
string(REPLACE "#include \"c89stringutils_log.h\"" "" amalg_source "${amalg_source}")
94+
string(REPLACE "#include \"stb_sprintf.h\"" "${amalg_stb}" amalg_source "${amalg_source}")
9395

9496
set(amalgamation "/* c89stringutils Amalgamation STB-style */\n\n")
9597
string(APPEND amalgamation "#ifndef C89STRINGUTILS_AMALGAMATION_H\n#define C89STRINGUTILS_AMALGAMATION_H\n\n")
9698
string(APPEND amalgamation "${amalg_export}\n\n")
9799
string(APPEND amalgamation "${amalg_config}\n\n")
98100
string(APPEND amalgamation "${amalg_log}\n\n")
99-
string(APPEND amalgamation "${amalg_safecrt_header}\n\n")
100101
string(APPEND amalgamation "${amalg_header}\n\n")
102+
string(APPEND amalgamation "${amalg_safecrt_header}\n\n")
101103
string(APPEND amalgamation "#ifdef C89STRINGUTILS_IMPLEMENTATION\n\n")
102104
string(APPEND amalgamation "${amalg_safecrt_source}\n\n")
103105
string(APPEND amalgamation "${amalg_source}\n\n")

c89stringutils/c89stringutils_safecrt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ C89STRINGUTILS_EXPORT errno_t c89stringutils_freopen_s(FILE **pFile,
354354
const char *filename,
355355
const char *mode,
356356
FILE *stream) {
357-
if (!pFile || !mode || !stream) {
357+
if (!pFile || !filename || !mode || !stream) {
358358
if (pFile)
359359
*pFile = NULL;
360360
c89stringutils_invoke_constraint_handler_s(
361-
"freopen_s: pFile, mode, or stream is null", NULL, EINVAL);
361+
"freopen_s: pFile, filename, mode, or stream is null", NULL, EINVAL);
362362
return EINVAL;
363363
}
364364
#if defined(C89STRINGUTILS_HAVE_FREOPEN_S)

c89stringutils/c89stringutils_string_extras.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ extern int mock_strerror_s(char *buffer, size_t sizeInBytes, int errnum);
8989
/** @brief Mock macro for strerror_s */
9090
#define strerror_s mock_strerror_s
9191
#endif
92+
#if defined(C89STRINGUTILS_HAVE_STRERROR_R)
93+
#if defined(C89STRINGUTILS_STRERROR_R_CHAR_P)
94+
/** @brief External mock for strerror_r (GNU)
95+
* @param errnum error number
96+
* @param buf buffer
97+
* @param buflen size
98+
*/
99+
extern char *mock_strerror_r(int errnum, char *buf, size_t buflen);
100+
#else
101+
/** @brief External mock for strerror_r (POSIX)
102+
* @param errnum error number
103+
* @param buf buffer
104+
* @param buflen size
105+
*/
106+
extern int mock_strerror_r(int errnum, char *buf, size_t buflen);
107+
#endif
108+
/** @brief Mock macro for strerror_r */
109+
#define strerror_r mock_strerror_r
110+
#endif
92111
#if defined(C89STRINGUTILS_HAVE_VFPRINTF_S)
93112
/** @brief External mock for vfprintf_s
94113
* @param stream stream
@@ -109,6 +128,16 @@ extern int mock_fprintf_s(FILE *stream, const char *format, ...);
109128
/** @brief Mock macro for fprintf_s */
110129
#define fprintf_s mock_fprintf_s
111130
#endif
131+
#if defined(C89STRINGUTILS_HAVE_REALLOCARRAY)
132+
/** @brief External mock for reallocarray
133+
* @param ptr pointer
134+
* @param nmemb number of members
135+
* @param size size
136+
*/
137+
extern void *mock_reallocarray(void *ptr, size_t nmemb, size_t size);
138+
/** @brief Mock macro for reallocarray */
139+
#define reallocarray mock_reallocarray
140+
#endif
112141
/** @brief Mock macro for malloc */
113142
#define malloc mock_malloc
114143
/** @brief Mock macro for realloc */
@@ -137,7 +166,11 @@ extern int mock_fprintf_s(FILE *stream, const char *format, ...);
137166
#pragma warning(disable : 4100) /* unreferenced formal parameter */
138167
#elif defined(__clang__)
139168
#pragma clang diagnostic push
169+
#if defined(__has_warning)
170+
#if __has_warning("-Wnonnull-compare")
140171
#pragma clang diagnostic ignored "-Wnonnull-compare"
172+
#endif
173+
#endif
141174
#pragma clang diagnostic ignored "-Wunused-parameter"
142175
#elif defined(__GNUC__) && __GNUC__ >= 7
143176
#pragma GCC diagnostic push
@@ -222,7 +255,7 @@ C89STRINGUTILS_EXPORT void c89stringutils_log_debug(const char *fmt, ...) {
222255
disable : 4244 4702) /* conversion from int to char, unreachable code */
223256
#endif
224257
#include "stb_sprintf.h"
225-
#if defined(C89STRINGUTILS_HAVE_PRAGMA_WARNING)
258+
#if defined(_MSC_VER) && _MSC_VER >= 1300
226259
#pragma warning(pop)
227260
#endif
228261
#define C89STRINGUTILS_USE_STB_SPRINTF 1
@@ -308,6 +341,9 @@ C89STRINGUTILS_EXPORT errno_t c89stringutils_strerror_s(char *s,
308341
{
309342
char *res = strerror_r(errnum, s, maxsize);
310343
if (res != s) {
344+
if (res == NULL) {
345+
return 22; /* EINVAL */
346+
}
311347
#if defined(C89STRINGUTILS_HAVE_STRNCPY_S)
312348
strncpy_s(s, maxsize, res, maxsize - 1);
313349
#else
@@ -318,7 +354,13 @@ C89STRINGUTILS_EXPORT errno_t c89stringutils_strerror_s(char *s,
318354
return 0;
319355
}
320356
#else
321-
return strerror_r(errnum, s, maxsize);
357+
{
358+
int rc = strerror_r(errnum, s, maxsize);
359+
if (rc != 0) {
360+
return 22; /* EINVAL */
361+
}
362+
return 0;
363+
}
322364
#endif
323365
#else
324366
{
@@ -1127,7 +1169,7 @@ C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto, const char *fmt,
11271169
return 0;
11281170
}
11291171

1130-
#if defined(C89STRINGUTILS_HAVE_PRAGMA_WARNING)
1172+
#if defined(_MSC_VER) && _MSC_VER >= 1300
11311173
#pragma warning(pop)
11321174
#elif defined(__clang__)
11331175
#pragma clang diagnostic pop

0 commit comments

Comments
 (0)