tree-wide: Fix constness issues with newer glibc

Latest glibc uses _Generic to have strstr() and other functions return
const char* or char* based on whether the input is a const char* or a
char*. This causes build failures as we previously always expected a char*.

Let's fix the compilation failures and add our own macros similar to glibc's
to have string functions that return a mutable or const pointer depending on
the input.
This commit is contained in:
Daan De Meyer
2025-11-25 20:49:39 +01:00
parent 2f62e4f5d8
commit 0bac1ed242
26 changed files with 108 additions and 89 deletions
+8 -14
View File
@@ -1348,16 +1348,14 @@ char* strdupcspn(const char *a, const char *reject) {
return strndup(a, strcspn(a, reject));
}
char* find_line_startswith(const char *haystack, const char *needle) {
char *p;
char* find_line_startswith_internal(const char *haystack, const char *needle) {
assert(haystack);
assert(needle);
/* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
* first character after it */
p = strstr(haystack, needle);
char *p = (char*) strstr(haystack, needle);
if (!p)
return NULL;
@@ -1371,16 +1369,14 @@ char* find_line_startswith(const char *haystack, const char *needle) {
return p + strlen(needle);
}
char* find_line(const char *haystack, const char *needle) {
char *p;
char* find_line_internal(const char *haystack, const char *needle) {
assert(haystack);
assert(needle);
/* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
* beginning of the line */
p = find_line_startswith(haystack, needle);
char *p = (char*) find_line_startswith(haystack, needle);
if (!p)
return NULL;
@@ -1390,16 +1386,14 @@ char* find_line(const char *haystack, const char *needle) {
return NULL;
}
char* find_line_after(const char *haystack, const char *needle) {
char *p;
char* find_line_after_internal(const char *haystack, const char *needle) {
assert(haystack);
assert(needle);
/* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
* next line after it */
p = find_line_startswith(haystack, needle);
char *p = (char*) find_line_startswith(haystack, needle);
if (!p)
return NULL;
@@ -1499,7 +1493,7 @@ ssize_t strlevenshtein(const char *x, const char *y) {
return t1[yl];
}
char* strrstr(const char *haystack, const char *needle) {
char* strrstr_internal(const char *haystack, const char *needle) {
/* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
if (!haystack || !needle)
@@ -1508,7 +1502,7 @@ char* strrstr(const char *haystack, const char *needle) {
/* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
* last char, not before. */
if (*needle == 0)
return strchr(haystack, 0);
return (char*) strchr(haystack, 0);
for (const char *p = strstr(haystack, needle), *q; p; p = q) {
q = strstr(p + 1, needle);