Skip to content

Commit a0cae93

Browse files
committed
Spec mbfl allocators as infallible
And remove all NULL checks.
1 parent 7d4ff84 commit a0cae93

File tree

6 files changed

+29
-129
lines changed

6 files changed

+29
-129
lines changed

ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,6 @@ mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt)
588588
mbfl_filt_conv_common_ctor(filt);
589589

590590
ctx = mbfl_malloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
591-
if (ctx == NULL) {
592-
mbfl_filt_conv_common_dtor(filt);
593-
return;
594-
}
595-
596591
ctx->tl_param.mode = MBFL_FILT_TL_HAN2ZEN_KATAKANA | MBFL_FILT_TL_HAN2ZEN_GLUE;
597592

598593
ctx->last = *filt;
@@ -614,10 +609,6 @@ mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter
614609

615610
*dest = *src;
616611
ctx = mbfl_malloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
617-
if (ctx != NULL) {
618-
*ctx = *(mbfl_filt_conv_wchar_cp50220_ctx*)src->opaque;
619-
}
620-
621612
dest->opaque = ctx;
622613
dest->data = &ctx->last;
623614
}

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,7 @@ mbfl_buffer_converter_new(
124124
const mbfl_encoding *to,
125125
size_t buf_initsz)
126126
{
127-
mbfl_buffer_converter *convd;
128-
129-
/* allocate */
130-
convd = (mbfl_buffer_converter*)mbfl_malloc(sizeof(mbfl_buffer_converter));
131-
if (convd == NULL) {
132-
return NULL;
133-
}
134-
135-
/* initialize */
127+
mbfl_buffer_converter *convd = mbfl_malloc(sizeof(mbfl_buffer_converter));
136128
convd->from = from;
137129
convd->to = to;
138130

@@ -376,15 +368,8 @@ mbfl_encoding_detector_new(const mbfl_encoding **elist, int elistsz, int strict)
376368
}
377369

378370
/* allocate */
379-
identd = (mbfl_encoding_detector*)mbfl_malloc(sizeof(mbfl_encoding_detector));
380-
if (identd == NULL) {
381-
return NULL;
382-
}
383-
identd->filter_list = (mbfl_identify_filter **)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *));
384-
if (identd->filter_list == NULL) {
385-
mbfl_free(identd);
386-
return NULL;
387-
}
371+
identd = mbfl_malloc(sizeof(mbfl_encoding_detector));
372+
identd->filter_list = mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *));
388373

389374
/* create filters */
390375
i = 0;
@@ -572,10 +557,7 @@ mbfl_identify_encoding(mbfl_string *string, const mbfl_encoding **elist, int eli
572557
const mbfl_encoding *encoding;
573558

574559
/* flist is an array of mbfl_identify_filter instances */
575-
flist = (mbfl_identify_filter *)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter));
576-
if (flist == NULL) {
577-
return NULL;
578-
}
560+
flist = mbfl_calloc(elistsz, sizeof(mbfl_identify_filter));
579561

580562
num = 0;
581563
if (elist != NULL) {
@@ -1128,13 +1110,9 @@ mbfl_substr(
11281110
n = end - start;
11291111
result->len = 0;
11301112
result->val = w = (unsigned char*)mbfl_malloc(n + 1);
1131-
if (w != NULL) {
1132-
result->len = n;
1133-
memcpy(w, string->val + start, n);
1134-
w[n] = '\0';
1135-
} else {
1136-
result = NULL;
1137-
}
1113+
result->len = n;
1114+
memcpy(w, string->val + start, n);
1115+
w[n] = '\0';
11381116
} else {
11391117
mbfl_memory_device device;
11401118
struct collector_substr_data pc;
@@ -1281,10 +1259,7 @@ mbfl_strcut(
12811259

12821260
/* allocate memory and copy string */
12831261
sz = end - start;
1284-
if ((w = (unsigned char*)mbfl_calloc(sz + 8,
1285-
sizeof(unsigned char))) == NULL) {
1286-
return NULL;
1287-
}
1262+
w = mbfl_calloc(sz + 8, sizeof(unsigned char));
12881263

12891264
memcpy(w, start, sz);
12901265
w[sz] = '\0';
@@ -1712,12 +1687,7 @@ mbfl_ja_jp_hantozen(
17121687
}
17131688
next_filter = decoder;
17141689

1715-
param =
1716-
(mbfl_filt_tl_jisx0201_jisx0208_param *)mbfl_malloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
1717-
if (param == NULL) {
1718-
goto out;
1719-
}
1720-
1690+
param = mbfl_malloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
17211691
param->mode = mode;
17221692

17231693
tl_filter = mbfl_convert_filter_new2(
@@ -1953,11 +1923,7 @@ mime_header_encoder_new(
19531923
return NULL;
19541924
}
19551925

1956-
pe = (struct mime_header_encoder_data*)mbfl_malloc(sizeof(struct mime_header_encoder_data));
1957-
if (pe == NULL) {
1958-
return NULL;
1959-
}
1960-
1926+
pe = mbfl_malloc(sizeof(struct mime_header_encoder_data));
19611927
mbfl_memory_device_init(&pe->outdev, 0, 0);
19621928
mbfl_memory_device_init(&pe->tmpdev, 0, 0);
19631929
pe->prevpos = 0;
@@ -2301,12 +2267,7 @@ mime_header_decoder_result(struct mime_header_decoder_data *pd, mbfl_string *res
23012267
struct mime_header_decoder_data*
23022268
mime_header_decoder_new(const mbfl_encoding *outcode)
23032269
{
2304-
struct mime_header_decoder_data *pd;
2305-
2306-
pd = (struct mime_header_decoder_data*)mbfl_malloc(sizeof(struct mime_header_decoder_data));
2307-
if (pd == NULL) {
2308-
return NULL;
2309-
}
2270+
struct mime_header_decoder_data *pd = mbfl_malloc(sizeof(struct mime_header_decoder_data));
23102271

23112272
mbfl_memory_device_init(&pd->outdev, 0, 0);
23122273
mbfl_memory_device_init(&pd->tmpdev, 0, 0);

ext/mbstring/libmbfl/mbfl/mbfl_allocators.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#include <stddef.h>
3535
#include "mbfl_defs.h"
3636

37+
/* All allocation functions are required to be infallible.
38+
* That is, they must never return NULL. */
39+
3740
typedef struct _mbfl_allocators {
3841
void *(*malloc)(size_t);
3942
void *(*realloc)(void *, size_t);

ext/mbstring/libmbfl/mbfl/mbfl_convert.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,15 @@ mbfl_convert_filter_new(
165165
int (*flush_function)(void*),
166166
void* data)
167167
{
168-
mbfl_convert_filter * filter;
168+
mbfl_convert_filter *filter;
169169
const struct mbfl_convert_vtbl *vtbl;
170170

171171
vtbl = mbfl_convert_filter_get_vtbl(from, to);
172172
if (vtbl == NULL) {
173173
return NULL;
174174
}
175175

176-
/* allocate */
177-
filter = (mbfl_convert_filter *)mbfl_malloc(sizeof(mbfl_convert_filter));
178-
if (filter == NULL) {
179-
return NULL;
180-
}
176+
filter = mbfl_malloc(sizeof(mbfl_convert_filter));
181177

182178
if (mbfl_convert_filter_common_init(filter, from, to, vtbl,
183179
output_function, flush_function, data)) {
@@ -195,7 +191,7 @@ mbfl_convert_filter_new2(
195191
int (*flush_function)(void*),
196192
void* data)
197193
{
198-
mbfl_convert_filter * filter;
194+
mbfl_convert_filter *filter;
199195
const mbfl_encoding *from_encoding, *to_encoding;
200196

201197
if (vtbl == NULL) {
@@ -205,11 +201,7 @@ mbfl_convert_filter_new2(
205201
from_encoding = mbfl_no2encoding(vtbl->from);
206202
to_encoding = mbfl_no2encoding(vtbl->to);
207203

208-
/* allocate */
209-
filter = (mbfl_convert_filter *)mbfl_malloc(sizeof(mbfl_convert_filter));
210-
if (filter == NULL) {
211-
return NULL;
212-
}
204+
filter = mbfl_malloc(sizeof(mbfl_convert_filter));
213205

214206
if (mbfl_convert_filter_common_init(filter, from_encoding, to_encoding, vtbl,
215207
output_function, flush_function, data)) {

ext/mbstring/libmbfl/mbfl/mbfl_ident.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,7 @@ const struct mbfl_identify_vtbl * mbfl_identify_filter_get_vtbl(enum mbfl_no_enc
191191

192192
mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding)
193193
{
194-
mbfl_identify_filter *filter;
195-
196-
/* allocate */
197-
filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter));
198-
if (filter == NULL) {
199-
return NULL;
200-
}
201-
194+
mbfl_identify_filter *filter = mbfl_malloc(sizeof(mbfl_identify_filter));
202195
if (mbfl_identify_filter_init(filter, encoding)) {
203196
mbfl_free(filter);
204197
return NULL;
@@ -209,14 +202,7 @@ mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding)
209202

210203
mbfl_identify_filter *mbfl_identify_filter_new2(const mbfl_encoding *encoding)
211204
{
212-
mbfl_identify_filter *filter;
213-
214-
/* allocate */
215-
filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter));
216-
if (filter == NULL) {
217-
return NULL;
218-
}
219-
205+
mbfl_identify_filter *filter = mbfl_malloc(sizeof(mbfl_identify_filter));
220206
if (mbfl_identify_filter_init2(filter, encoding)) {
221207
mbfl_free(filter);
222208
return NULL;

ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ mbfl_memory_device_init(mbfl_memory_device *device, size_t initsz, size_t allocs
4949
device->length = 0;
5050
device->buffer = NULL;
5151
if (initsz > 0) {
52-
device->buffer = (unsigned char *)mbfl_malloc(initsz);
53-
if (device->buffer != NULL) {
54-
device->length = initsz;
55-
}
52+
device->buffer = mbfl_malloc(initsz);
53+
device->length = initsz;
5654
}
5755
device->pos = 0;
5856
if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
@@ -66,15 +64,10 @@ mbfl_memory_device_init(mbfl_memory_device *device, size_t initsz, size_t allocs
6664
void
6765
mbfl_memory_device_realloc(mbfl_memory_device *device, size_t initsz, size_t allocsz)
6866
{
69-
unsigned char *tmp;
70-
7167
if (device) {
7268
if (initsz > device->length) {
73-
tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, initsz);
74-
if (tmp != NULL) {
75-
device->buffer = tmp;
76-
device->length = initsz;
77-
}
69+
device->buffer = mbfl_realloc(device->buffer, initsz);
70+
device->length = initsz;
7871
}
7972
if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
8073
device->allocsz = allocsz;
@@ -142,20 +135,15 @@ mbfl_memory_device_output(int c, void *data)
142135
if (device->pos >= device->length) {
143136
/* reallocate buffer */
144137
size_t newlen;
145-
unsigned char *tmp;
146138

147139
if (device->length > SIZE_MAX - device->allocsz) {
148140
/* overflow */
149141
return -1;
150142
}
151143

152144
newlen = device->length + device->allocsz;
153-
tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
154-
if (tmp == NULL) {
155-
return -1;
156-
}
145+
device->buffer = mbfl_realloc(device->buffer, newlen);
157146
device->length = newlen;
158-
device->buffer = tmp;
159147
}
160148

161149
device->buffer[device->pos++] = (unsigned char)c;
@@ -170,20 +158,15 @@ mbfl_memory_device_output2(int c, void *data)
170158
if (2 > device->length - device->pos) {
171159
/* reallocate buffer */
172160
size_t newlen;
173-
unsigned char *tmp;
174161

175162
if (device->length > SIZE_MAX - device->allocsz) {
176163
/* overflow */
177164
return -1;
178165
}
179166

180167
newlen = device->length + device->allocsz;
181-
tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
182-
if (tmp == NULL) {
183-
return -1;
184-
}
168+
device->buffer = mbfl_realloc(device->buffer, newlen);
185169
device->length = newlen;
186-
device->buffer = tmp;
187170
}
188171

189172
device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
@@ -200,20 +183,15 @@ mbfl_memory_device_output4(int c, void* data)
200183
if (4 > device->length - device->pos) {
201184
/* reallocate buffer */
202185
size_t newlen;
203-
unsigned char *tmp;
204186

205187
if (device->length > SIZE_MAX - device->allocsz) {
206188
/* overflow */
207189
return -1;
208190
}
209191

210192
newlen = device->length + device->allocsz;
211-
tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
212-
if (tmp == NULL) {
213-
return -1;
214-
}
193+
device->buffer = mbfl_realloc(device->buffer, newlen);
215194
device->length = newlen;
216-
device->buffer = tmp;
217195
}
218196

219197
device->buffer[device->pos++] = (unsigned char)((c >> 24) & 0xff);
@@ -238,7 +216,6 @@ mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, size_t
238216
if (len > device->length - device->pos) {
239217
/* reallocate buffer */
240218
size_t newlen;
241-
unsigned char *tmp;
242219

243220
if (len > SIZE_MAX - MBFL_MEMORY_DEVICE_ALLOC_SIZE
244221
|| device->length > SIZE_MAX - (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE)) {
@@ -247,13 +224,8 @@ mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, size_t
247224
}
248225

249226
newlen = device->length + len + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
250-
tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
251-
if (tmp == NULL) {
252-
return -1;
253-
}
254-
227+
device->buffer = mbfl_realloc(device->buffer, newlen);
255228
device->length = newlen;
256-
device->buffer = tmp;
257229
}
258230

259231
w = &device->buffer[device->pos];
@@ -301,7 +273,6 @@ mbfl_wchar_device_output(int c, void *data)
301273
if (device->pos >= device->length) {
302274
/* reallocate buffer */
303275
size_t newlen;
304-
unsigned int *tmp;
305276

306277
if (device->length > SIZE_MAX - device->allocsz) {
307278
/* overflow */
@@ -314,12 +285,8 @@ mbfl_wchar_device_output(int c, void *data)
314285
return -1;
315286
}
316287

317-
tmp = (unsigned int *)mbfl_realloc((void *)device->buffer, newlen*sizeof(int));
318-
if (tmp == NULL) {
319-
return -1;
320-
}
288+
device->buffer = mbfl_realloc(device->buffer, newlen*sizeof(int));
321289
device->length = newlen;
322-
device->buffer = tmp;
323290
}
324291

325292
device->buffer[device->pos++] = c;

0 commit comments

Comments
 (0)