Project

General

Profile

Enhancement #750 » 0023-sanitize-fix-const-char-being-used-as-non-const.patch

Loïc Gomez, 2022-01-10 19:28

View differences:

src/line.c
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)
......
{
char *tmp;
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);
l = irc_line_to_string(line);
array_set(&line->words, 1, tmp);
bip_cfree(prev);
return l;
}
......
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 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);
if (l->origin)
free(l->origin);
src/log.c
if (mode_args) {
for (i = 0; i < array_count(mode_args); i++) {
snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", tmpbuf,
(char *)array_get(mode_args, i));
(const char*)array_get(mode_args, i));
tmp = tmpbuf;
tmpbuf = tmpbuf2;
tmpbuf2 = tmp;
......
irc_line_init(&l);
l.origin = P_IRCMASK;
if (dest == cli_nick)
l.origin = (char *)bl;
l.origin = bip_strdup(bl);
_irc_line_append(&l, "PRIVMSG");
_irc_line_append(&l, dest);
_irc_line_append(&l, "End of backlog");
src/util.c
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 *r = strdup(str);
......
a->elemc = index + 1;
}
void *array_drop(array_t *a, int index)
const void *array_drop(array_t *a, int index)
{
int i;
void *ret;
const void *ret;
assert(a && array_includes(a, index));
src/util.h
typedef struct array {
int elemc;
void **elemv;
const void **elemv;
} array_t;
#define MOVE_STRING(dest, src) do {\
......
void *bip_malloc(size_t size);
void *bip_calloc(size_t nmemb, 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_strcat_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);
#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)++)
void array_init(array_t *a);
......
array_t *array_extract(array_t *a, int index, int upto);
void array_deinit(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)
{
assert(a);
......
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));
return a->elemv[index];
......
if (a->elemc == 0)
return NULL;
if (a->elemc == 1) {
void *ptr = a->elemv[0];
void *ptr = bip_strdup(a->elemv[0]);
free(a->elemv);
a->elemv = NULL;
a->elemc = 0;
return ptr;
}
return a->elemv[--a->elemc];
return (void *)bip_strdup(a->elemv[--a->elemc]);
}
#endif
(22-22/24)