Skip to content

Commit dd4305d

Browse files
committed
fix(build): resolve -Wcast-qual and macro redefinition errors
1 parent 4e937e8 commit dd4305d

5 files changed

Lines changed: 22 additions & 15 deletions

File tree

‎c89stringutils/c89stringutils_safecrt.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ __attribute__((noreturn))
1919
#elif defined(_MSC_VER)
2020
__declspec(noreturn)
2121
#endif
22-
extern void mock_abort(void);
22+
extern void
23+
mock_abort(void);
2324
extern FILE *mock_fopen(const char *filename, const char *mode);
2425
extern FILE *mock_freopen(const char *filename, const char *mode, FILE *stream);
2526
extern FILE *mock_tmpfile(void);

‎c89stringutils/c89stringutils_string_extras.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,9 @@ C89STRINGUTILS_EXPORT int c89stringutils_strcasecmp(const char *s1,
745745
}
746746
#endif
747747

748+
/* Helper to remove const without triggering -Wcast-qual */
749+
#define C89STRINGUTILS_UNCONST(type, ptr) ((type)(size_t)(const void *)(ptr))
750+
748751
#if !defined(C89STRINGUTILS_HAVE_STRNSTR)
749752
/**
750753
* @brief Locate a substring in a string, looking at no more than len
@@ -772,15 +775,15 @@ C89STRINGUTILS_EXPORT char *c89stringutils_strnstr(const char *buffer,
772775
targetLength = strlen(target);
773776
#endif
774777
if (targetLength == 0) {
775-
return (char *)buffer;
778+
return C89STRINGUTILS_UNCONST(char *, buffer);
776779
}
777780
remaining = bufferLength;
778781
for (start = buffer; *start && remaining >= targetLength;
779782
start++, remaining--) {
780783
if (*start == *target) {
781784
rc = strncmp(start + 1, target + 1, targetLength - 1);
782785
if (rc == 0) {
783-
return (char *)(start);
786+
return C89STRINGUTILS_UNCONST(char *, start);
784787
}
785788
}
786789
}
@@ -810,12 +813,12 @@ C89STRINGUTILS_EXPORT char *c89stringutils_strcasestr(const char *h,
810813
l = strlen(n);
811814
#endif
812815
if (l == 0) {
813-
return (char *)h;
816+
return C89STRINGUTILS_UNCONST(char *, h);
814817
}
815818
for (; *h; h++) {
816819
cmp = c89stringutils_strncasecmp(h, n, l);
817820
if (cmp == 0) {
818-
return (char *)h;
821+
return C89STRINGUTILS_UNCONST(char *, h);
819822
}
820823
}
821824
return NULL;

‎c89stringutils/c89stringutils_string_extras.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,9 @@ typedef size_t rsize_t;
358358
#if defined(C89STRINGUTILS_HAVE_STRERROR_S)
359359
#define c89stringutils_strerror_s strerror_s
360360
#else
361-
extern C89STRINGUTILS_EXPORT
362-
errno_t c89stringutils_strerror_s(char *s, rsize_t maxsize, errno_t errnum);
361+
extern C89STRINGUTILS_EXPORT errno_t c89stringutils_strerror_s(char *s,
362+
rsize_t maxsize,
363+
errno_t errnum);
363364
#endif
364365

365366
/**
@@ -495,13 +496,13 @@ c89stringutils_vsnprintf(char *s, size_t n, const char *format, va_list arg)
495496
#define c89stringutils_strncasecmp(s1, s2, n) \
496497
((((const void *)(s1)) == NULL && ((const void *)(s2)) == NULL) ? 0 \
497498
: (((const void *)(s1)) == NULL) ? -1 \
498-
: (((const void *)(s2)) == NULL) ? 1 \
499+
: (((const void *)(s2)) == NULL) ? 1 \
499500
: _strnicmp((s1), (s2), (n)))
500501
#elif defined(C89STRINGUTILS_HAVE_STRNCASECMP)
501502
#define c89stringutils_strncasecmp(s1, s2, n) \
502503
((((const void *)(s1)) == NULL && ((const void *)(s2)) == NULL) ? 0 \
503504
: (((const void *)(s1)) == NULL) ? -1 \
504-
: (((const void *)(s2)) == NULL) ? 1 \
505+
: (((const void *)(s2)) == NULL) ? 1 \
505506
: strncasecmp((s1), (s2), (n)))
506507
#else
507508
extern C89STRINGUTILS_EXPORT int
@@ -519,13 +520,13 @@ c89stringutils_strncasecmp(const char *s1, const char *s2, size_t n);
519520
#define c89stringutils_strcasecmp(s1, s2) \
520521
((((const void *)(s1)) == NULL && ((const void *)(s2)) == NULL) ? 0 \
521522
: (((const void *)(s1)) == NULL) ? -1 \
522-
: (((const void *)(s2)) == NULL) ? 1 \
523+
: (((const void *)(s2)) == NULL) ? 1 \
523524
: _stricmp((s1), (s2)))
524525
#elif defined(C89STRINGUTILS_HAVE_STRCASECMP)
525526
#define c89stringutils_strcasecmp(s1, s2) \
526527
((((const void *)(s1)) == NULL && ((const void *)(s2)) == NULL) ? 0 \
527528
: (((const void *)(s1)) == NULL) ? -1 \
528-
: (((const void *)(s2)) == NULL) ? 1 \
529+
: (((const void *)(s2)) == NULL) ? 1 \
529530
: strcasecmp((s1), (s2)))
530531
#else
531532
extern C89STRINGUTILS_EXPORT int c89stringutils_strcasecmp(const char *s1,

‎c89stringutils/stb_sprintf.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
598598
/* get the string */
599599
s = va_arg(va, char *);
600600
if (s == 0)
601-
s = (char *)"null";
601+
s = (char *)(size_t)(const void *)"null";
602602
/* get the length, limited to desired precision */
603603
/* always limit to ~0u chars since our counts are 32b */
604604
l = stbsp__strlen_limited(s, (pr >= 0) ? pr : ~0u);
@@ -637,7 +637,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
637637
case 'e': /* float */
638638
case 'f': /* float */
639639
va_arg(va, double); /* eat it */
640-
s = (char *)"No float";
640+
s = (char *)(size_t)(const void *)"No float";
641641
l = 8;
642642
lead[0] = 0;
643643
tail[0] = 0;
@@ -768,7 +768,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
768768
tail[0] = 0;
769769
stbsp__lead_sign(fl, lead);
770770
if (dp == STBSP__SPECIAL) {
771-
s = (char *)sn;
771+
s = (char *)(size_t)(const void *)sn;
772772
cs = 0;
773773
pr = 0;
774774
goto scopy;
@@ -837,7 +837,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
837837
tail[0] = 0;
838838
stbsp__lead_sign(fl, lead);
839839
if (dp == STBSP__SPECIAL) {
840-
s = (char *)sn;
840+
s = (char *)(size_t)(const void *)sn;
841841
cs = 0;
842842
pr = 0;
843843
goto scopy;

‎c89stringutils/tests/test_amalg.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#ifndef _GNU_SOURCE
12
#define _GNU_SOURCE
3+
#endif
24
#define C89STRINGUTILS_IMPLEMENTATION
35
/* clang-format off */
46
#include "c89stringutils_amalgamation.h"

0 commit comments

Comments
 (0)