From: Nikita Popov Date: Mon, 4 May 2020 21:17:54 +0000 (+0200) Subject: Spec mbfl allocators as infallible X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a0cae937c54fb7e3e0536d45f7a633df3657ade7;p=php Spec mbfl allocators as infallible And remove all NULL checks. --- diff --git a/ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c b/ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c index 4fa2334444..32f036be63 100644 --- a/ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c +++ b/ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c @@ -588,11 +588,6 @@ mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt) 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; @@ -614,10 +609,6 @@ mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter *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; } diff --git a/ext/mbstring/libmbfl/mbfl/mbfilter.c b/ext/mbstring/libmbfl/mbfl/mbfilter.c index fc87744d79..13afc161da 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfilter.c +++ b/ext/mbstring/libmbfl/mbfl/mbfilter.c @@ -124,15 +124,7 @@ mbfl_buffer_converter_new( 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; @@ -376,15 +368,8 @@ mbfl_encoding_detector_new(const mbfl_encoding **elist, int elistsz, int strict) } /* 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; @@ -572,10 +557,7 @@ mbfl_identify_encoding(mbfl_string *string, const mbfl_encoding **elist, int eli 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) { @@ -1128,13 +1110,9 @@ mbfl_substr( 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; @@ -1281,10 +1259,7 @@ mbfl_strcut( /* 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'; @@ -1712,12 +1687,7 @@ mbfl_ja_jp_hantozen( } 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( @@ -1953,11 +1923,7 @@ mime_header_encoder_new( 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; @@ -2301,12 +2267,7 @@ mime_header_decoder_result(struct mime_header_decoder_data *pd, mbfl_string *res 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); diff --git a/ext/mbstring/libmbfl/mbfl/mbfl_allocators.h b/ext/mbstring/libmbfl/mbfl/mbfl_allocators.h index 5caf25097e..cc341d464a 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfl_allocators.h +++ b/ext/mbstring/libmbfl/mbfl/mbfl_allocators.h @@ -34,6 +34,9 @@ #include #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); diff --git a/ext/mbstring/libmbfl/mbfl/mbfl_convert.c b/ext/mbstring/libmbfl/mbfl/mbfl_convert.c index 04e0a13de9..98f2cee048 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfl_convert.c +++ b/ext/mbstring/libmbfl/mbfl/mbfl_convert.c @@ -165,7 +165,7 @@ mbfl_convert_filter_new( 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); @@ -173,11 +173,7 @@ mbfl_convert_filter_new( 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)) { @@ -195,7 +191,7 @@ mbfl_convert_filter_new2( int (*flush_function)(void*), void* data) { - mbfl_convert_filter * filter; + mbfl_convert_filter *filter; const mbfl_encoding *from_encoding, *to_encoding; if (vtbl == NULL) { @@ -205,11 +201,7 @@ mbfl_convert_filter_new2( 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)) { diff --git a/ext/mbstring/libmbfl/mbfl/mbfl_ident.c b/ext/mbstring/libmbfl/mbfl/mbfl_ident.c index ce7cf13566..be49c64543 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfl_ident.c +++ b/ext/mbstring/libmbfl/mbfl/mbfl_ident.c @@ -191,14 +191,7 @@ const struct mbfl_identify_vtbl * mbfl_identify_filter_get_vtbl(enum mbfl_no_enc 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; @@ -209,14 +202,7 @@ mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding) 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; diff --git a/ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c b/ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c index 0dd8ea2e66..27d3ef2fac 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c +++ b/ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c @@ -49,10 +49,8 @@ mbfl_memory_device_init(mbfl_memory_device *device, size_t initsz, size_t allocs 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) { @@ -66,15 +64,10 @@ mbfl_memory_device_init(mbfl_memory_device *device, size_t initsz, size_t allocs 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; @@ -142,7 +135,6 @@ mbfl_memory_device_output(int c, void *data) if (device->pos >= device->length) { /* reallocate buffer */ size_t newlen; - unsigned char *tmp; if (device->length > SIZE_MAX - device->allocsz) { /* overflow */ @@ -150,12 +142,8 @@ mbfl_memory_device_output(int c, void *data) } 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; @@ -170,7 +158,6 @@ mbfl_memory_device_output2(int c, void *data) if (2 > device->length - device->pos) { /* reallocate buffer */ size_t newlen; - unsigned char *tmp; if (device->length > SIZE_MAX - device->allocsz) { /* overflow */ @@ -178,12 +165,8 @@ mbfl_memory_device_output2(int c, void *data) } 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); @@ -200,7 +183,6 @@ mbfl_memory_device_output4(int c, void* data) if (4 > device->length - device->pos) { /* reallocate buffer */ size_t newlen; - unsigned char *tmp; if (device->length > SIZE_MAX - device->allocsz) { /* overflow */ @@ -208,12 +190,8 @@ mbfl_memory_device_output4(int c, void* data) } 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); @@ -238,7 +216,6 @@ mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, size_t 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)) { @@ -247,13 +224,8 @@ mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, size_t } 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]; @@ -301,7 +273,6 @@ mbfl_wchar_device_output(int c, void *data) if (device->pos >= device->length) { /* reallocate buffer */ size_t newlen; - unsigned int *tmp; if (device->length > SIZE_MAX - device->allocsz) { /* overflow */ @@ -314,12 +285,8 @@ mbfl_wchar_device_output(int c, void *data) 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;