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
65 65

  
66 66
void _irc_line_append(struct line *l, const char *s)
67 67
{
68
	array_push(&l->words, (char *)s);
68
	array_push(&l->words, bip_strdup(s));
69 69
}
70 70

  
71 71
void irc_line_append(struct line *l, const char *s)
......
109 109
{
110 110
	char *tmp;
111 111
	char *l;
112
	const char *prev;
112 113

  
113
	tmp = (char *)irc_line_elem(line, 1);
114
	prev = irc_line_elem(line, 1);
115
	tmp = bip_strdup(prev);
114 116
	array_set(&line->words, 1, nick);
115 117
	l = irc_line_to_string(line);
116 118
	array_set(&line->words, 1, tmp);
119
	bip_cfree(prev);
117 120

  
118 121
	return l;
119 122
}
......
135 138

  
136 139
void irc_line_drop(struct line *line, int elem)
137 140
{
138
	free(array_drop(&line->words, elem));
141
	bip_cfree(array_drop(&line->words, elem));
139 142
}
140 143

  
141 144
int irc_line_elem_equals(struct line *line, int elem, const char *cmp)
......
213 216
	int i;
214 217

  
215 218
	for (i = 0; i < array_count(&l->words); i++)
216
		free(array_get(&l->words, i));
219
		bip_cfree(array_get(&l->words, i));
217 220
	array_deinit(&l->words);
218 221
	if (l->origin)
219 222
		free(l->origin);
src/log.c
641 641
	if (mode_args) {
642 642
		for (i = 0; i < array_count(mode_args); i++) {
643 643
			snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", tmpbuf,
644
					(char *)array_get(mode_args, i));
644
					(const char*)array_get(mode_args, i));
645 645
			tmp = tmpbuf;
646 646
			tmpbuf = tmpbuf2;
647 647
			tmpbuf2 = tmp;
......
1407 1407
				irc_line_init(&l);
1408 1408
				l.origin = P_IRCMASK;
1409 1409
				if (dest == cli_nick)
1410
					l.origin = (char *)bl;
1410
					l.origin = bip_strdup(bl);
1411 1411
				_irc_line_append(&l, "PRIVMSG");
1412 1412
				_irc_line_append(&l, dest);
1413 1413
				_irc_line_append(&l, "End of backlog");
src/util.c
76 76
	return r;
77 77
}
78 78

  
79
void bip_cfree(const void *ptr)
80
{
81
	if (!ptr)
82
		return;
83
// there's no other way to free a const pointer
84
#pragma GCC diagnostic push
85
#pragma GCC diagnostic ignored "-Wcast-qual"
86
	free((void *)ptr);
87
#pragma GCC diagnostic pop
88
}
89

  
79 90
char *bip_strdup(const char *str)
80 91
{
81 92
	char *r = strdup(str);
......
861 872
	a->elemc = index + 1;
862 873
}
863 874

  
864
void *array_drop(array_t *a, int index)
875
const void *array_drop(array_t *a, int index)
865 876
{
866 877
	int i;
867
	void *ret;
878
	const void *ret;
868 879

  
869 880
	assert(a && array_includes(a, index));
870 881

  
src/util.h
70 70

  
71 71
typedef struct array {
72 72
	int elemc;
73
	void **elemv;
73
	const void **elemv;
74 74
} array_t;
75 75

  
76 76
#define MOVE_STRING(dest, src) do {\
......
177 177
void *bip_malloc(size_t size);
178 178
void *bip_calloc(size_t nmemb, size_t size);
179 179
void *bip_realloc(void *ptr, size_t size);
180
void bip_cfree(const void *ptr);
180 181
char *bip_strdup(const char *str);
181 182
char *bip_strcat_fit(size_t *remaining, char *str, const char *str2);
182 183
char *bip_strcatf_fit(size_t *remaining, char *str, const char *str2, ...);
183 184
void bip_clock_gettime(clockid_t clockid, struct timespec *tp);
184 185
#define array_each(a, idx, ptr) for ((idx) = 0; \
185
		(idx) < (a)->elemc && (((ptr) = array_get((a), (idx))) || 1); \
186
		(idx) < (a)->elemc && (((ptr) = bip_strdup(array_get((a), (idx)))) || 1); \
186 187
		(idx)++)
187 188

  
188 189
void array_init(array_t *a);
......
191 192
array_t *array_extract(array_t *a, int index, int upto);
192 193
void array_deinit(array_t *a);
193 194
void array_free(array_t *a);
194
void *array_drop(array_t *a, int index);
195
const void *array_drop(array_t *a, int index);
195 196
static inline int array_count(array_t *a)
196 197
{
197 198
	assert(a);
......
211 212
	a->elemv[index] = ptr;
212 213
}
213 214

  
214
static inline void *array_get(array_t *a, int index)
215
static inline const void *array_get(array_t *a, int index)
215 216
{
216 217
	assert(a && array_includes(a, index));
217 218
	return a->elemv[index];
......
233 234
	if (a->elemc == 0)
234 235
		return NULL;
235 236
	if (a->elemc == 1) {
236
		void *ptr = a->elemv[0];
237
		void *ptr = bip_strdup(a->elemv[0]);
237 238
		free(a->elemv);
238 239
		a->elemv = NULL;
239 240
		a->elemc = 0;
240 241
		return ptr;
241 242
	}
242
	return a->elemv[--a->elemc];
243
	return (void *)bip_strdup(a->elemv[--a->elemc]);
243 244
}
244 245

  
245 246
#endif
246
- 
(22-22/24)