Skip to content

Commit 9f0c659

Browse files
committed
Increase coverage and improve toolchain support
1 parent b47c0cc commit 9f0c659

9 files changed

Lines changed: 357 additions & 334 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,8 @@ jobs:
2424
cmake -B build -DC89STRINGUTILS_BUILD_AMALGAMATION=ON
2525
cmake --build build
2626
27-
- name: Create Test File
28-
run: |
29-
cat << 'EOF' > test_amalg.c
30-
#define _GNU_SOURCE
31-
#define C89STRINGUTILS_IMPLEMENTATION
32-
#include "build/c89stringutils/c89stringutils_amalgamation.h"
33-
#include <stdio.h>
34-
#include <stdlib.h>
35-
36-
int main(void) {
37-
char* x = NULL;
38-
int rc = c89stringutils_asprintf(&x, "hello %s", "world");
39-
if (rc == 0 && x != NULL) {
40-
printf("%s\n", x);
41-
free(x);
42-
return 0;
43-
}
44-
return 1;
45-
}
46-
EOF
47-
4827
- name: Compile Test Amalgamation
49-
run: gcc -Wall -Wextra -Werror -o test_amalg test_amalg.c
28+
run: gcc -Wall -Wextra -Werror -Ibuild/c89stringutils -o test_amalg c89stringutils/tests/test_amalg.c
5029

5130
- name: Run Test Amalgamation
5231
run: ./test_amalg

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
*build*
44
*_export.h
55

6-
test_amalg.c
6+
77
cmake/scripts/

c89stringutils/c89stringutils_safecrt.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,17 @@ C89STRINGUTILS_EXPORT errno_t c89stringutils_tmpfile_s(FILE **pFile) {
378378
return EINVAL;
379379
}
380380
#if defined(C89STRINGUTILS_HAVE_TMPFILE_S)
381-
return tmpfile_s(pFile);
381+
{
382+
errno_t rc = tmpfile_s(pFile);
383+
/* Workaround for MinGW bug where tmpfile_s returns 0 but leaves pFile NULL
384+
*/
385+
if (rc == 0 && *pFile == NULL) {
386+
*pFile = tmpfile();
387+
if (*pFile == NULL)
388+
return errno ? errno : 5; /* EIO fallback */
389+
}
390+
return rc;
391+
}
382392
#else
383393
*pFile = tmpfile();
384394
if (*pFile == NULL)

c89stringutils/c89stringutils_safecrt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,6 @@ C89STRINGUTILS_EXPORT int c89stringutils_vswprintf_s(wchar_t *buffer,
196196

197197
#ifdef __cplusplus
198198
}
199-
#endif
199+
#endif /* __cplusplus */
200200

201-
#endif
201+
#endif /* !C89STRINGUTILS_SAFECRT_H */

c89stringutils/c89stringutils_string_extras.c

Lines changed: 102 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -881,122 +881,127 @@ C89STRINGUTILS_EXPORT size_t c89stringutils_strerrorlen_s(errno_t errnum) {
881881
*/
882882
C89STRINGUTILS_EXPORT int c89stringutils_vasprintf(char **str, const char *fmt,
883883
va_list ap) {
884-
#if defined(C89STRINGUTILS_HAVE_VASPRINTF)
885-
return vasprintf(str, fmt, ap);
886-
#else
887-
int rc;
888-
va_list ap2;
889-
char *string, *newstr;
890-
size_t len;
891-
892884
if (str == NULL || fmt == NULL) {
893885
LOG_DEBUG("str or fmt is NULL");
894886
return -1;
895887
}
896888

897-
string = (char *)malloc(INIT_SZ);
898-
if (string == NULL) {
899-
rc = -1;
900-
goto fail;
889+
#if defined(C89STRINGUTILS_HAVE_VASPRINTF)
890+
{
891+
int rc = vasprintf(str, fmt, ap);
892+
return (rc >= 0) ? 0 : -1;
901893
}
894+
#else
895+
{
896+
int rc;
897+
va_list ap2;
898+
char *string, *newstr;
899+
size_t len;
902900

903-
VA_COPY(ap2, ap);
904-
rc = vsnprintf(string, INIT_SZ, fmt, ap2);
905-
va_end(ap2);
906-
907-
if (rc < 0) { /* Bad length or legacy compiler truncation */
908-
size_t current_sz = INIT_SZ;
909-
while (
910-
rc < 0 &&
911-
current_sz <=
912-
(size_t)1048576) { /* Cap at 1MB to avoid runaway on real errors */
913-
char *t;
914-
current_sz *= 2;
915-
t = (char *)realloc(string, current_sz);
916-
if (t == NULL) {
917-
free(string);
918-
rc = -1;
919-
goto fail;
920-
}
921-
string = t;
922-
VA_COPY(ap2, ap);
923-
rc = vsnprintf(string, current_sz, fmt, ap2);
924-
va_end(ap2);
925-
}
926-
if (rc < 0 || (size_t)rc >= current_sz) {
927-
free(string);
928-
rc = -1;
929-
goto fail;
930-
}
931-
*str = string;
932-
return rc;
933-
} else if (rc == INT_MAX) { /* Bad length */
934-
free(string);
935-
rc = -1;
936-
goto fail;
937-
} else if (rc < INIT_SZ) { /* succeeded with initial alloc */
938-
*str = string;
939-
} else { /* bigger than initial, realloc allowing for nul */
940-
len = (size_t)rc + 1;
941-
newstr = (char *)realloc(string, len);
942-
if (newstr == NULL) {
943-
free(string);
901+
string = (char *)malloc(INIT_SZ);
902+
if (string == NULL) {
944903
rc = -1;
945904
goto fail;
946905
}
906+
947907
VA_COPY(ap2, ap);
948-
rc = vsnprintf(newstr, len, fmt, ap2);
908+
rc = vsnprintf(string, INIT_SZ, fmt, ap2);
949909
va_end(ap2);
950-
if (rc < 0) { /* failed with realloc'ed string */
951-
free(newstr);
952-
rc = -1;
953-
goto fail;
954-
} else if ((size_t)rc >= len) {
955-
free(newstr);
910+
911+
if (rc < 0) { /* Bad length or legacy compiler truncation */
912+
size_t current_sz = INIT_SZ;
913+
while (rc < 0 &&
914+
current_sz <= (size_t)1048576) { /* Cap at 1MB to avoid runaway on
915+
real errors */
916+
char *t;
917+
current_sz *= 2;
918+
t = (char *)realloc(string, current_sz);
919+
if (t == NULL) {
920+
free(string);
921+
rc = -1;
922+
goto fail;
923+
}
924+
string = t;
925+
VA_COPY(ap2, ap);
926+
rc = vsnprintf(string, current_sz, fmt, ap2);
927+
va_end(ap2);
928+
}
929+
if (rc < 0 || (size_t)rc >= current_sz) {
930+
free(string);
931+
rc = -1;
932+
goto fail;
933+
}
934+
*str = string;
935+
return 0;
936+
} else if (rc == INT_MAX) { /* Bad length */
937+
free(string);
956938
rc = -1;
957939
goto fail;
940+
} else if (rc < INIT_SZ) { /* succeeded with initial alloc */
941+
*str = string;
942+
} else { /* bigger than initial, realloc allowing for nul */
943+
len = (size_t)rc + 1;
944+
newstr = (char *)realloc(string, len);
945+
if (newstr == NULL) {
946+
free(string);
947+
rc = -1;
948+
goto fail;
949+
}
950+
VA_COPY(ap2, ap);
951+
rc = vsnprintf(newstr, len, fmt, ap2);
952+
va_end(ap2);
953+
if (rc < 0) { /* failed with realloc'ed string */
954+
free(newstr);
955+
rc = -1;
956+
goto fail;
957+
} else if ((size_t)rc >= len) {
958+
free(newstr);
959+
rc = -1;
960+
goto fail;
961+
}
962+
*str = newstr;
958963
}
959-
*str = newstr;
960-
}
961-
return rc;
964+
return 0;
962965

963-
fail:
966+
fail:
964967
#if defined(C89STRINGUTILS_HAVE_STRERROR_S)
965-
{
966-
char errbuf[256];
967-
int err_rc = strerror_s(errbuf, sizeof(errbuf), errno);
968-
if (err_rc != 0) {
969-
LOG_DEBUG("strerror_s failed with rc=%d", err_rc);
970-
errbuf[0] = '\0';
968+
{
969+
char errbuf[256];
970+
int err_rc = strerror_s(errbuf, sizeof(errbuf), errno);
971+
if (err_rc != 0) {
972+
LOG_DEBUG("strerror_s failed with rc=%d", err_rc);
973+
errbuf[0] = '\0';
974+
}
975+
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, errbuf);
971976
}
972-
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, errbuf);
973-
}
974977
#elif defined(C89STRINGUTILS_HAVE_STRERROR_R)
975-
{
976-
char errbuf[256];
978+
{
979+
char errbuf[256];
977980
#if defined(C89STRINGUTILS_STRERROR_R_CHAR_P)
978-
char *res = strerror_r(errno, errbuf, sizeof(errbuf));
979-
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, res ? res : "");
981+
char *res = strerror_r(errno, errbuf, sizeof(errbuf));
982+
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, res ? res : "");
980983
#else
981-
int err_rc;
982-
errbuf[0] = '\0';
983-
err_rc = strerror_r(errno, errbuf, sizeof(errbuf));
984-
errbuf[sizeof(errbuf) - 1] = '\0';
985-
if (err_rc != 0) {
984+
int err_rc;
986985
errbuf[0] = '\0';
987-
}
988-
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, errbuf);
986+
err_rc = strerror_r(errno, errbuf, sizeof(errbuf));
987+
errbuf[sizeof(errbuf) - 1] = '\0';
988+
if (err_rc != 0) {
989+
errbuf[0] = '\0';
990+
}
991+
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, errbuf);
989992
#endif
990-
}
993+
}
991994
#else
992-
{
993-
const char *errstr = strerror(errno);
994-
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc, errstr ? errstr : "");
995-
}
995+
{
996+
const char *errstr = strerror(errno);
997+
LOG_DEBUG("vasprintf failed with rc=%d, error=%s", rc,
998+
errstr ? errstr : "");
999+
}
9961000
#endif
997-
*str = NULL;
998-
errno = ENOMEM;
999-
return -1;
1001+
*str = NULL;
1002+
errno = ENOMEM;
1003+
return -1;
1004+
}
10001005
#endif
10011006
}
10021007

@@ -1080,7 +1085,7 @@ C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto, const char *fmt,
10801085
rc = c89stringutils_vasprintf(&new_part, fmt, args);
10811086
va_end(args);
10821087

1083-
if (rc < 0 || new_part == NULL) {
1088+
if (rc != 0 || new_part == NULL) {
10841089
return -1;
10851090
}
10861091

@@ -1092,10 +1097,11 @@ C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto, const char *fmt,
10921097
{
10931098
#if defined(C89STRINGUTILS_HAVE_STRNLEN_S)
10941099
size_t base_length = strnlen_s(*unto, RSIZE_MAX);
1100+
size_t new_length = strnlen_s(new_part, RSIZE_MAX);
10951101
#else
10961102
size_t base_length = strlen(*unto);
1103+
size_t new_length = strlen(new_part);
10971104
#endif
1098-
size_t new_length = (size_t)rc;
10991105
char *result;
11001106

11011107
#if defined(C89STRINGUTILS_HAVE_REALLOCARRAY)
@@ -1183,7 +1189,7 @@ C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...) {
11831189
rc = c89stringutils_vasprintf(&new_part, fmt, args);
11841190
va_end(args);
11851191

1186-
if (rc < 0 || new_part == NULL) {
1192+
if (rc != 0 || new_part == NULL) {
11871193
return -1;
11881194
}
11891195

@@ -1195,10 +1201,11 @@ C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...) {
11951201
{
11961202
#if defined(C89STRINGUTILS_HAVE_STRNLEN_S)
11971203
size_t base_length = strnlen_s(*unto, RSIZE_MAX);
1204+
size_t new_length = strnlen_s(new_part, RSIZE_MAX);
11981205
#else
11991206
size_t base_length = strlen(*unto);
1207+
size_t new_length = strlen(new_part);
12001208
#endif
1201-
size_t new_length = (size_t)rc;
12021209
char *result;
12031210

12041211
#if defined(C89STRINGUTILS_HAVE_REALLOCARRAY)

0 commit comments

Comments
 (0)