And remove all NULL checks.
mbfl_filt_conv_common_ctor(filt);
ctx = mbfl_malloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
- if (ctx == NULL) {
- mbfl_filt_conv_common_dtor(filt);
- return;
- }
-
ctx->tl_param.mode = MBFL_FILT_TL_HAN2ZEN_KATAKANA | MBFL_FILT_TL_HAN2ZEN_GLUE;
ctx->last = *filt;
*dest = *src;
ctx = mbfl_malloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
- if (ctx != NULL) {
- *ctx = *(mbfl_filt_conv_wchar_cp50220_ctx*)src->opaque;
- }
-
dest->opaque = ctx;
dest->data = &ctx->last;
}
const mbfl_encoding *to,
size_t buf_initsz)
{
- mbfl_buffer_converter *convd;
-
- /* allocate */
- convd = (mbfl_buffer_converter*)mbfl_malloc(sizeof(mbfl_buffer_converter));
- if (convd == NULL) {
- return NULL;
- }
-
- /* initialize */
+ mbfl_buffer_converter *convd = mbfl_malloc(sizeof(mbfl_buffer_converter));
convd->from = from;
convd->to = to;
}
/* allocate */
- identd = (mbfl_encoding_detector*)mbfl_malloc(sizeof(mbfl_encoding_detector));
- if (identd == NULL) {
- return NULL;
- }
- identd->filter_list = (mbfl_identify_filter **)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *));
- if (identd->filter_list == NULL) {
- mbfl_free(identd);
- return NULL;
- }
+ identd = mbfl_malloc(sizeof(mbfl_encoding_detector));
+ identd->filter_list = mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *));
/* create filters */
i = 0;
const mbfl_encoding *encoding;
/* flist is an array of mbfl_identify_filter instances */
- flist = (mbfl_identify_filter *)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter));
- if (flist == NULL) {
- return NULL;
- }
+ flist = mbfl_calloc(elistsz, sizeof(mbfl_identify_filter));
num = 0;
if (elist != NULL) {
n = end - start;
result->len = 0;
result->val = w = (unsigned char*)mbfl_malloc(n + 1);
- if (w != NULL) {
- result->len = n;
- memcpy(w, string->val + start, n);
- w[n] = '\0';
- } else {
- result = NULL;
- }
+ result->len = n;
+ memcpy(w, string->val + start, n);
+ w[n] = '\0';
} else {
mbfl_memory_device device;
struct collector_substr_data pc;
/* allocate memory and copy string */
sz = end - start;
- if ((w = (unsigned char*)mbfl_calloc(sz + 8,
- sizeof(unsigned char))) == NULL) {
- return NULL;
- }
+ w = mbfl_calloc(sz + 8, sizeof(unsigned char));
memcpy(w, start, sz);
w[sz] = '\0';
}
next_filter = decoder;
- param =
- (mbfl_filt_tl_jisx0201_jisx0208_param *)mbfl_malloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
- if (param == NULL) {
- goto out;
- }
-
+ param = mbfl_malloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
param->mode = mode;
tl_filter = mbfl_convert_filter_new2(
return NULL;
}
- pe = (struct mime_header_encoder_data*)mbfl_malloc(sizeof(struct mime_header_encoder_data));
- if (pe == NULL) {
- return NULL;
- }
-
+ pe = mbfl_malloc(sizeof(struct mime_header_encoder_data));
mbfl_memory_device_init(&pe->outdev, 0, 0);
mbfl_memory_device_init(&pe->tmpdev, 0, 0);
pe->prevpos = 0;
struct mime_header_decoder_data*
mime_header_decoder_new(const mbfl_encoding *outcode)
{
- struct mime_header_decoder_data *pd;
-
- pd = (struct mime_header_decoder_data*)mbfl_malloc(sizeof(struct mime_header_decoder_data));
- if (pd == NULL) {
- return NULL;
- }
+ struct mime_header_decoder_data *pd = mbfl_malloc(sizeof(struct mime_header_decoder_data));
mbfl_memory_device_init(&pd->outdev, 0, 0);
mbfl_memory_device_init(&pd->tmpdev, 0, 0);
#include <stddef.h>
#include "mbfl_defs.h"
+/* All allocation functions are required to be infallible.
+ * That is, they must never return NULL. */
+
typedef struct _mbfl_allocators {
void *(*malloc)(size_t);
void *(*realloc)(void *, size_t);
int (*flush_function)(void*),
void* data)
{
- mbfl_convert_filter * filter;
+ mbfl_convert_filter *filter;
const struct mbfl_convert_vtbl *vtbl;
vtbl = mbfl_convert_filter_get_vtbl(from, to);
return NULL;
}
- /* allocate */
- filter = (mbfl_convert_filter *)mbfl_malloc(sizeof(mbfl_convert_filter));
- if (filter == NULL) {
- return NULL;
- }
+ filter = mbfl_malloc(sizeof(mbfl_convert_filter));
if (mbfl_convert_filter_common_init(filter, from, to, vtbl,
output_function, flush_function, data)) {
int (*flush_function)(void*),
void* data)
{
- mbfl_convert_filter * filter;
+ mbfl_convert_filter *filter;
const mbfl_encoding *from_encoding, *to_encoding;
if (vtbl == NULL) {
from_encoding = mbfl_no2encoding(vtbl->from);
to_encoding = mbfl_no2encoding(vtbl->to);
- /* allocate */
- filter = (mbfl_convert_filter *)mbfl_malloc(sizeof(mbfl_convert_filter));
- if (filter == NULL) {
- return NULL;
- }
+ filter = mbfl_malloc(sizeof(mbfl_convert_filter));
if (mbfl_convert_filter_common_init(filter, from_encoding, to_encoding, vtbl,
output_function, flush_function, data)) {
mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding)
{
- mbfl_identify_filter *filter;
-
- /* allocate */
- filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter));
- if (filter == NULL) {
- return NULL;
- }
-
+ mbfl_identify_filter *filter = mbfl_malloc(sizeof(mbfl_identify_filter));
if (mbfl_identify_filter_init(filter, encoding)) {
mbfl_free(filter);
return NULL;
mbfl_identify_filter *mbfl_identify_filter_new2(const mbfl_encoding *encoding)
{
- mbfl_identify_filter *filter;
-
- /* allocate */
- filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter));
- if (filter == NULL) {
- return NULL;
- }
-
+ mbfl_identify_filter *filter = mbfl_malloc(sizeof(mbfl_identify_filter));
if (mbfl_identify_filter_init2(filter, encoding)) {
mbfl_free(filter);
return NULL;
device->length = 0;
device->buffer = NULL;
if (initsz > 0) {
- device->buffer = (unsigned char *)mbfl_malloc(initsz);
- if (device->buffer != NULL) {
- device->length = initsz;
- }
+ device->buffer = mbfl_malloc(initsz);
+ device->length = initsz;
}
device->pos = 0;
if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
void
mbfl_memory_device_realloc(mbfl_memory_device *device, size_t initsz, size_t allocsz)
{
- unsigned char *tmp;
-
if (device) {
if (initsz > device->length) {
- tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, initsz);
- if (tmp != NULL) {
- device->buffer = tmp;
- device->length = initsz;
- }
+ device->buffer = mbfl_realloc(device->buffer, initsz);
+ device->length = initsz;
}
if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
device->allocsz = allocsz;
if (device->pos >= device->length) {
/* reallocate buffer */
size_t newlen;
- unsigned char *tmp;
if (device->length > SIZE_MAX - device->allocsz) {
/* overflow */
}
newlen = device->length + device->allocsz;
- tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
- if (tmp == NULL) {
- return -1;
- }
+ device->buffer = mbfl_realloc(device->buffer, newlen);
device->length = newlen;
- device->buffer = tmp;
}
device->buffer[device->pos++] = (unsigned char)c;
if (2 > device->length - device->pos) {
/* reallocate buffer */
size_t newlen;
- unsigned char *tmp;
if (device->length > SIZE_MAX - device->allocsz) {
/* overflow */
}
newlen = device->length + device->allocsz;
- tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
- if (tmp == NULL) {
- return -1;
- }
+ device->buffer = mbfl_realloc(device->buffer, newlen);
device->length = newlen;
- device->buffer = tmp;
}
device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
if (4 > device->length - device->pos) {
/* reallocate buffer */
size_t newlen;
- unsigned char *tmp;
if (device->length > SIZE_MAX - device->allocsz) {
/* overflow */
}
newlen = device->length + device->allocsz;
- tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
- if (tmp == NULL) {
- return -1;
- }
+ device->buffer = mbfl_realloc(device->buffer, newlen);
device->length = newlen;
- device->buffer = tmp;
}
device->buffer[device->pos++] = (unsigned char)((c >> 24) & 0xff);
if (len > device->length - device->pos) {
/* reallocate buffer */
size_t newlen;
- unsigned char *tmp;
if (len > SIZE_MAX - MBFL_MEMORY_DEVICE_ALLOC_SIZE
|| device->length > SIZE_MAX - (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE)) {
}
newlen = device->length + len + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
- tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
- if (tmp == NULL) {
- return -1;
- }
-
+ device->buffer = mbfl_realloc(device->buffer, newlen);
device->length = newlen;
- device->buffer = tmp;
}
w = &device->buffer[device->pos];
if (device->pos >= device->length) {
/* reallocate buffer */
size_t newlen;
- unsigned int *tmp;
if (device->length > SIZE_MAX - device->allocsz) {
/* overflow */
return -1;
}
- tmp = (unsigned int *)mbfl_realloc((void *)device->buffer, newlen*sizeof(int));
- if (tmp == NULL) {
- return -1;
- }
+ device->buffer = mbfl_realloc(device->buffer, newlen*sizeof(int));
device->length = newlen;
- device->buffer = tmp;
}
device->buffer[device->pos++] = c;