sanitize: fix const char * being used as non-const

This commit is contained in:
Loïc Gomez 2022-01-10 18:10:50 +01:00 committed by Pierre-Louis Bonicoli
parent ee6ee0bd34
commit 26d347dec6
Signed by: pilou
GPG Key ID: 06914C4A5EDAA6DD
4 changed files with 29 additions and 14 deletions

View File

@ -66,7 +66,7 @@ char *irc_line_pop(struct line *l)
void _irc_line_append(struct line *l, const char *s) void _irc_line_append(struct line *l, const char *s)
{ {
array_push(&l->words, (char *)s); array_push(&l->words, bip_strdup(s));
} }
void irc_line_append(struct line *l, const char *s) void irc_line_append(struct line *l, const char *s)
@ -110,11 +110,14 @@ char *irc_line_to_string_to(struct line *line, char *nick)
{ {
char *tmp; char *tmp;
char *l; char *l;
const char *prev;
tmp = (char *)irc_line_elem(line, 1); prev = irc_line_elem(line, 1);
tmp = bip_strdup(prev);
array_set(&line->words, 1, nick); array_set(&line->words, 1, nick);
l = irc_line_to_string(line); l = irc_line_to_string(line);
array_set(&line->words, 1, tmp); array_set(&line->words, 1, tmp);
bip_cfree(prev);
return l; return l;
} }
@ -136,7 +139,7 @@ const char *irc_line_elem(struct line *line, int elem)
void irc_line_drop(struct line *line, int elem) void irc_line_drop(struct line *line, int elem)
{ {
free(array_drop(&line->words, elem)); bip_cfree(array_drop(&line->words, elem));
} }
int irc_line_elem_equals(struct line *line, int elem, const char *cmp) int irc_line_elem_equals(struct line *line, int elem, const char *cmp)
@ -214,7 +217,7 @@ void irc_line_free(struct line *l)
int i; int i;
for (i = 0; i < array_count(&l->words); i++) for (i = 0; i < array_count(&l->words); i++)
free(array_get(&l->words, i)); bip_cfree(array_get(&l->words, i));
array_deinit(&l->words); array_deinit(&l->words);
if (l->origin) if (l->origin)
free(l->origin); free(l->origin);

View File

@ -642,7 +642,7 @@ void log_mode(log_t *logdata, const char *ircmask, const char *channel,
if (mode_args) { if (mode_args) {
for (i = 0; i < array_count(mode_args); i++) { for (i = 0; i < array_count(mode_args); i++) {
snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", tmpbuf, snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", tmpbuf,
(char *)array_get(mode_args, i)); (const char*)array_get(mode_args, i));
tmp = tmpbuf; tmp = tmpbuf;
tmpbuf = tmpbuf2; tmpbuf = tmpbuf2;
tmpbuf2 = tmp; tmpbuf2 = tmp;
@ -1408,7 +1408,7 @@ list_t *backlog_lines(log_t *log, const char *bl, const char *cli_nick,
irc_line_init(&l); irc_line_init(&l);
l.origin = P_IRCMASK; l.origin = P_IRCMASK;
if (dest == cli_nick) if (dest == cli_nick)
l.origin = (char *)bl; l.origin = bip_strdup(bl);
_irc_line_append(&l, "PRIVMSG"); _irc_line_append(&l, "PRIVMSG");
_irc_line_append(&l, dest); _irc_line_append(&l, dest);
_irc_line_append(&l, "End of backlog"); _irc_line_append(&l, "End of backlog");

View File

@ -77,6 +77,17 @@ void *bip_realloc(void *ptr, size_t size)
return r; return r;
} }
void bip_cfree(const void *ptr)
{
if (!ptr)
return;
// there's no other way to free a const pointer
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
free((void *)ptr);
#pragma GCC diagnostic pop
}
char *bip_strdup(const char *str) char *bip_strdup(const char *str)
{ {
char *r = strdup(str); char *r = strdup(str);
@ -862,10 +873,10 @@ void array_ensure(array_t *a, int index)
a->elemc = index + 1; a->elemc = index + 1;
} }
void *array_drop(array_t *a, int index) const void *array_drop(array_t *a, int index)
{ {
int i; int i;
void *ret; const void *ret;
assert(a && array_includes(a, index)); assert(a && array_includes(a, index));

View File

@ -71,7 +71,7 @@ typedef struct hash_iterator {
typedef struct array { typedef struct array {
int elemc; int elemc;
void **elemv; const void **elemv;
} array_t; } array_t;
#define MOVE_STRING(dest, src) do {\ #define MOVE_STRING(dest, src) do {\
@ -178,12 +178,13 @@ char *checkmode2text(int v);
void *bip_malloc(size_t size); void *bip_malloc(size_t size);
void *bip_calloc(size_t nmemb, size_t size); void *bip_calloc(size_t nmemb, size_t size);
void *bip_realloc(void *ptr, size_t size); void *bip_realloc(void *ptr, size_t size);
void bip_cfree(const void *ptr);
char *bip_strdup(const char *str); char *bip_strdup(const char *str);
char *bip_strcat_fit(size_t *remaining, char *str, const char *str2); char *bip_strcat_fit(size_t *remaining, char *str, const char *str2);
char *bip_strcatf_fit(size_t *remaining, char *str, const char *str2, ...); char *bip_strcatf_fit(size_t *remaining, char *str, const char *str2, ...);
void bip_clock_gettime(clockid_t clockid, struct timespec *tp); void bip_clock_gettime(clockid_t clockid, struct timespec *tp);
#define array_each(a, idx, ptr) for ((idx) = 0; \ #define array_each(a, idx, ptr) for ((idx) = 0; \
(idx) < (a)->elemc && (((ptr) = array_get((a), (idx))) || 1); \ (idx) < (a)->elemc && (((ptr) = bip_strdup(array_get((a), (idx)))) || 1); \
(idx)++) (idx)++)
void array_init(array_t *a); void array_init(array_t *a);
@ -192,7 +193,7 @@ void array_ensure(array_t *a, int index);
array_t *array_extract(array_t *a, int index, int upto); array_t *array_extract(array_t *a, int index, int upto);
void array_deinit(array_t *a); void array_deinit(array_t *a);
void array_free(array_t *a); void array_free(array_t *a);
void *array_drop(array_t *a, int index); const void *array_drop(array_t *a, int index);
static inline int array_count(array_t *a) static inline int array_count(array_t *a)
{ {
assert(a); assert(a);
@ -212,7 +213,7 @@ static inline void array_set(array_t *a, int index, void *ptr)
a->elemv[index] = ptr; a->elemv[index] = ptr;
} }
static inline void *array_get(array_t *a, int index) static inline const void *array_get(array_t *a, int index)
{ {
assert(a && array_includes(a, index)); assert(a && array_includes(a, index));
return a->elemv[index]; return a->elemv[index];
@ -234,13 +235,13 @@ static inline void *array_pop(array_t *a)
if (a->elemc == 0) if (a->elemc == 0)
return NULL; return NULL;
if (a->elemc == 1) { if (a->elemc == 1) {
void *ptr = a->elemv[0]; void *ptr = bip_strdup(a->elemv[0]);
free(a->elemv); free(a->elemv);
a->elemv = NULL; a->elemv = NULL;
a->elemc = 0; a->elemc = 0;
return ptr; return ptr;
} }
return a->elemv[--a->elemc]; return (void *)bip_strdup(a->elemv[--a->elemc]);
} }
#endif #endif