]> granicus.if.org Git - php/commitdiff
Spec mbfl allocators as infallible
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 4 May 2020 21:17:54 +0000 (23:17 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 4 May 2020 21:19:07 +0000 (23:19 +0200)
And remove all NULL checks.

ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c
ext/mbstring/libmbfl/mbfl/mbfilter.c
ext/mbstring/libmbfl/mbfl/mbfl_allocators.h
ext/mbstring/libmbfl/mbfl/mbfl_convert.c
ext/mbstring/libmbfl/mbfl/mbfl_ident.c
ext/mbstring/libmbfl/mbfl/mbfl_memory_device.c

index 4fa23344445f217d8c7d40e0759e4feff2df1d8b..32f036be63b56e9ff8a7a936be7d36522636baaf 100644 (file)
@@ -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;
 }
index fc87744d797d36c777288ed3e061c0d6e5e8e5d3..13afc161da36d7d19d5e02467db45ad58c2fe3da 100644 (file)
@@ -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);
index 5caf25097ef475544820fe572d842f4c4af89a30..cc341d464a9a1a447136d679e68345a19515e6a7 100644 (file)
@@ -34,6 +34,9 @@
 #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);
index 04e0a13de9e0f3f592092fe38510f9572668f4ff..98f2cee04838a900f54b5029b445be883b1eda49 100644 (file)
@@ -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)) {
index ce7cf135667aa46add3f93311ec0713895778b41..be49c64543f611c20f1abb084cf21cd372963199 100644 (file)
@@ -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;
index 0dd8ea2e664c6c2279f4f789c03faaf99297e701..27d3ef2fac6d6eeb93596e7b5be579369f0c76ed 100644 (file)
@@ -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;