]> granicus.if.org Git - php/commitdiff
Remove more unnecessary checks on Zend's allocator functions
authorThomas Punt <tpunt@hotmail.co.uk>
Thu, 16 Mar 2017 08:27:57 +0000 (08:27 +0000)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 16 Mar 2017 11:23:55 +0000 (12:23 +0100)
ext/bz2/bz2_filter.c
ext/mbstring/php_mbregex.c
ext/openssl/xp_ssl.c
ext/pdo/pdo_dbh.c
ext/standard/array.c
ext/standard/user_filters.c
main/fopen_wrappers.c
main/streams/filter.c
main/streams/memory.c
sapi/cli/php_cli_server.c

index cb7088828451d8adbf2d5a1ef830f85aabfd0c73..6446e29ad9eb63a876b840354125db21c0e68337 100644 (file)
@@ -315,10 +315,6 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
 
        /* Create this filter */
        data = pecalloc(1, sizeof(php_bz2_filter_data), persistent);
-       if (!data) {
-               php_error_docref(NULL, E_WARNING, "Failed allocating %zu bytes", sizeof(php_bz2_filter_data));
-               return NULL;
-       }
 
        /* Circular reference */
        data->strm.opaque = (void *) data;
@@ -328,19 +324,8 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
        data->persistent = persistent;
        data->strm.avail_out = data->outbuf_len = data->inbuf_len = 2048;
        data->strm.next_in = data->inbuf = (char *) pemalloc(data->inbuf_len, persistent);
-       if (!data->inbuf) {
-               php_error_docref(NULL, E_WARNING, "Failed allocating %zu bytes", data->inbuf_len);
-               pefree(data, persistent);
-               return NULL;
-       }
        data->strm.avail_in = 0;
        data->strm.next_out = data->outbuf = (char *) pemalloc(data->outbuf_len, persistent);
-       if (!data->outbuf) {
-               php_error_docref(NULL, E_WARNING, "Failed allocating %zu bytes", data->outbuf_len);
-               pefree(data->inbuf, persistent);
-               pefree(data, persistent);
-               return NULL;
-       }
 
        if (strcasecmp(filtername, "bzip2.decompress") == 0) {
                data->small_footprint = 0;
index 998d9a7072addd261b20fe89372f0773cdac9835..a2389df880e92d4646431f0c7ffcc6b83fd2c7e5 100644 (file)
@@ -88,9 +88,6 @@ zend_mb_regex_globals *php_mb_regex_globals_alloc(void)
 {
        zend_mb_regex_globals *pglobals = pemalloc(
                        sizeof(zend_mb_regex_globals), 1);
-       if (!pglobals) {
-               return NULL;
-       }
        if (SUCCESS != _php_mb_regex_globals_ctor(pglobals)) {
                pefree(pglobals, 1);
                return NULL;
index 65693c23c75406783cfa1c325683656dcce6854e..a2d6860ecdc0b5ae75e5bd214ac763a8d5191440 100644 (file)
@@ -1431,9 +1431,6 @@ static unsigned char *alpn_protos_parse(unsigned short *outlen, const char *in)
        }
 
        out = emalloc(strlen(in) + 1);
-       if (!out) {
-               return NULL;
-       }
 
        for (i = 0; i <= len; ++i) {
                if (i == len || in[i] == ',') {
index 586797e744bc981f94f98407af5ceff2c39766b9..4cfd01020fae4f2a00fb36c789e0dd6ab0726200 100644 (file)
@@ -1294,9 +1294,7 @@ int pdo_hash_methods(pdo_dbh_object_t *dbh_obj, int kind)
                return 0;
        }
 
-       if (!(dbh->cls_methods[kind] = pemalloc(sizeof(HashTable), dbh->is_persistent))) {
-               php_error_docref(NULL, E_ERROR, "out of memory while allocating PDO methods.");
-       }
+       dbh->cls_methods[kind] = pemalloc(sizeof(HashTable), dbh->is_persistent);
        zend_hash_init_ex(dbh->cls_methods[kind], 8, NULL,
                        dbh->is_persistent? cls_method_pdtor : cls_method_dtor, dbh->is_persistent, 0);
 
index 94343dc45e26fbeaefed2755ed093463bddee32c..d3c8dae19b6049bb11b0d2862de953609060af7c 100644 (file)
@@ -4487,10 +4487,6 @@ PHP_FUNCTION(array_unique)
 
        /* create and sort array with pointers to the target_hash buckets */
        arTmp = (struct bucketindex *) pemalloc((Z_ARRVAL_P(array)->nNumOfElements + 1) * sizeof(struct bucketindex), Z_ARRVAL_P(array)->u.flags & HASH_FLAG_PERSISTENT);
-       if (!arTmp) {
-               zval_dtor(return_value);
-               RETURN_FALSE;
-       }
        for (i = 0, idx = 0; idx < Z_ARRVAL_P(array)->nNumUsed; idx++) {
                p = Z_ARRVAL_P(array)->arData + idx;
                if (Z_TYPE(p->val) == IS_UNDEF) continue;
@@ -4781,13 +4777,6 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
                }
                hash = Z_ARRVAL(args[i]);
                list = (Bucket *) pemalloc((hash->nNumOfElements + 1) * sizeof(Bucket), hash->u.flags & HASH_FLAG_PERSISTENT);
-               if (!list) {
-                       PHP_ARRAY_CMP_FUNC_RESTORE();
-
-                       efree(ptrs);
-                       efree(lists);
-                       RETURN_FALSE;
-               }
                lists[i] = list;
                ptrs[i] = list;
                for (idx = 0; idx < hash->nNumUsed; idx++) {
@@ -5203,13 +5192,6 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
                }
                hash = Z_ARRVAL(args[i]);
                list = (Bucket *) pemalloc((hash->nNumOfElements + 1) * sizeof(Bucket), hash->u.flags & HASH_FLAG_PERSISTENT);
-               if (!list) {
-                       PHP_ARRAY_CMP_FUNC_RESTORE();
-
-                       efree(ptrs);
-                       efree(lists);
-                       RETURN_FALSE;
-               }
                lists[i] = list;
                ptrs[i] = list;
                for (idx = 0; idx < hash->nNumUsed; idx++) {
index 4d7d76d13f9e3da930db08d228fca5da7dceb382..b0205094d9bc7aa3d9fbfe93b5024c0a00f291ab 100644 (file)
@@ -508,9 +508,7 @@ PHP_FUNCTION(stream_bucket_new)
 
        php_stream_from_zval(stream, zstream);
 
-       if (!(pbuffer = pemalloc(buffer_len, php_stream_is_persistent(stream)))) {
-               RETURN_FALSE;
-       }
+       pbuffer = pemalloc(buffer_len, php_stream_is_persistent(stream));
 
        memcpy(pbuffer, buffer, buffer_len);
 
index 578e5d51707c4f7c4aa0df164fdc70825cb3c6fb..578acc2101077fc3a47534ff6c169fa979af620b 100644 (file)
@@ -406,16 +406,14 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
                IS_ABSOLUTE_PATH(PG(doc_root), length)) {
                int path_len = (int)strlen(path_info);
                filename = emalloc(length + path_len + 2);
-               if (filename) {
-                       memcpy(filename, PG(doc_root), length);
-                       if (!IS_SLASH(filename[length - 1])) {  /* length is never 0 */
-                               filename[length++] = PHP_DIR_SEPARATOR;
-                       }
-                       if (IS_SLASH(path_info[0])) {
-                               length--;
-                       }
-                       strncpy(filename + length, path_info, path_len + 1);
+               memcpy(filename, PG(doc_root), length);
+               if (!IS_SLASH(filename[length - 1])) {  /* length is never 0 */
+                       filename[length++] = PHP_DIR_SEPARATOR;
+               }
+               if (IS_SLASH(path_info[0])) {
+                       length--;
                }
+               strncpy(filename + length, path_info, path_len + 1);
        } else {
                filename = SG(request_info).path_translated;
        }
index 47df8834bd8ed814bf7740ef2b758d04f3f8ea90..901cf00ad946add7a61ede289157d8f8ec8646cd 100644 (file)
@@ -74,22 +74,11 @@ PHPAPI php_stream_bucket *php_stream_bucket_new(php_stream *stream, char *buf, s
        php_stream_bucket *bucket;
 
        bucket = (php_stream_bucket*)pemalloc(sizeof(php_stream_bucket), is_persistent);
-
-       if (bucket == NULL) {
-               return NULL;
-       }
-
        bucket->next = bucket->prev = NULL;
 
        if (is_persistent && !buf_persistent) {
                /* all data in a persistent bucket must also be persistent */
                bucket->buf = pemalloc(buflen, 1);
-
-               if (bucket->buf == NULL) {
-                       pefree(bucket, 1);
-                       return NULL;
-               }
-
                memcpy(bucket->buf, buf, buflen);
                bucket->buflen = buflen;
                bucket->own_buf = 1;
index 41c09faca881eb59e9a4551011aaa94c9ffd8248..253d1d83403734acb7025c30419632ae7790a995 100644 (file)
@@ -62,12 +62,8 @@ static size_t php_stream_memory_write(php_stream *stream, const char *buf, size_
                } else {
                        tmp = erealloc(ms->data, ms->fpos + count);
                }
-               if (!tmp) {
-                       count = ms->fsize - ms->fpos + 1;
-               } else {
-                       ms->data = tmp;
-                       ms->fsize = ms->fpos + count;
-               }
+               ms->data = tmp;
+               ms->fsize = ms->fpos + count;
        }
        if (!ms->data)
                count = 0;
index 62e1601c09000a4ff953998a648df06efca121d4..de4cfb2195fe2db0f5f4ed2db03da2835915256a 100644 (file)
@@ -928,9 +928,6 @@ static size_t php_cli_server_buffer_size(const php_cli_server_buffer *buffer) /*
 static php_cli_server_chunk *php_cli_server_chunk_immortal_new(const char *buf, size_t len) /* {{{ */
 {
        php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
-       if (!chunk) {
-               return NULL;
-       }
 
        chunk->type = PHP_CLI_SERVER_CHUNK_IMMORTAL;
        chunk->next = NULL;
@@ -942,9 +939,6 @@ static php_cli_server_chunk *php_cli_server_chunk_immortal_new(const char *buf,
 static php_cli_server_chunk *php_cli_server_chunk_heap_new(void *block, char *buf, size_t len) /* {{{ */
 {
        php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
-       if (!chunk) {
-               return NULL;
-       }
 
        chunk->type = PHP_CLI_SERVER_CHUNK_HEAP;
        chunk->next = NULL;
@@ -957,9 +951,6 @@ static php_cli_server_chunk *php_cli_server_chunk_heap_new(void *block, char *bu
 static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len) /* {{{ */
 {
        php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk) + len, 1);
-       if (!chunk) {
-               return NULL;
-       }
 
        chunk->type = PHP_CLI_SERVER_CHUNK_HEAP;
        chunk->next = NULL;
@@ -1215,12 +1206,6 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s
 #if HAVE_GETADDRINFO && HAVE_IPV6
                case AF_INET6:
                        sa = pemalloc(sizeof(struct sockaddr_in6), 1);
-                       if (!sa) {
-                               closesocket(retval);
-                               retval = SOCK_ERR;
-                               *errstr = NULL;
-                               goto out;
-                       }
                        *(struct sockaddr_in6 *)sa = *(struct sockaddr_in6 *)*p;
                        ((struct sockaddr_in6 *)sa)->sin6_port = htons(*port);
                        *socklen = sizeof(struct sockaddr_in6);
@@ -1228,12 +1213,6 @@ static php_socket_t php_network_listen_socket(const char *host, int *port, int s
 #endif
                case AF_INET:
                        sa = pemalloc(sizeof(struct sockaddr_in), 1);
-                       if (!sa) {
-                               closesocket(retval);
-                               retval = SOCK_ERR;
-                               *errstr = NULL;
-                               goto out;
-                       }
                        *(struct sockaddr_in *)sa = *(struct sockaddr_in *)*p;
                        ((struct sockaddr_in *)sa)->sin_port = htons(*port);
                        *socklen = sizeof(struct sockaddr_in);
@@ -1370,10 +1349,6 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
        size_t prev_path_len = 0;
        int  is_static_file = 0;
 
-       if (!buf) {
-               return;
-       }
-
        memmove(p, document_root, document_root_len);
        p += document_root_len;
        vpath = p;
@@ -1642,9 +1617,6 @@ static int php_cli_server_client_read_request_on_body(php_http_parser *parser, c
        php_cli_server_client *client = parser->data;
        if (!client->request.content) {
                client->request.content = pemalloc(parser->content_length, 1);
-               if (!client->request.content) {
-                       return -1;
-               }
                client->request.content_len = 0;
        }
        client->request.content = perealloc(client->request.content, client->request.content_len + length, 1);
@@ -1720,9 +1692,6 @@ static int php_cli_server_client_read_request(php_cli_server_client *client, cha
        }
        if (client->current_header_name) {
                char *header_name = safe_pemalloc(client->current_header_name_len, 1, 1, 1);
-               if (!header_name) {
-                       return -1;
-               }
                memmove(header_name, client->current_header_name, client->current_header_name_len);
                client->current_header_name = header_name;
                client->current_header_name_allocated = 1;
@@ -2389,9 +2358,6 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socke
                php_socket_t client_sock;
                socklen_t socklen = server->socklen;
                struct sockaddr *sa = pemalloc(server->socklen, 1);
-               if (!sa) {
-                       return FAILURE;
-               }
                client_sock = accept(server->server_sock, sa, &socklen);
                if (!ZEND_VALID_SOCKET(client_sock)) {
                        char *errstr;
@@ -2406,7 +2372,8 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socke
                        closesocket(client_sock);
                        return SUCCESS;
                }
-               if (!(client = pemalloc(sizeof(php_cli_server_client), 1)) || FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen)) {
+               client = pemalloc(sizeof(php_cli_server_client), 1);
+               if (FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen)) {
                        php_cli_server_logf("Failed to create a new request object");
                        pefree(sa, 1);
                        closesocket(client_sock);