[php-src] master: ext/soap: Add `UNEXPECTED()` in obvious error path (#22596)

Weilin Du via GitHub <[email protected]>
Newsgroups gmane.comp.php.cvs.general
Message-ID <[email protected]>
Author: Weilin Du (LamentXU123)
Committer: GitHub (web-flow)
Pusher: LamentXU123
Date: 2026-07-05T12:28:12+08:00

Commit: https://github.com/php/php-src/commit/aa12a652c33755f2b932b34442f2109b376d5ea7
Raw diff: https://github.com/php/php-src/commit/aa12a652c33755f2b932b34442f2109b376d5ea7.diff

ext/soap: Add `UNEXPECTED()` in obvious error path (#22596)

This added `UNEXPECTED()` in several obvious error path for optimization
in ext/soap.

Changed paths:
  M  ext/soap/php_encoding.c
  M  ext/soap/php_http.c
  M  ext/soap/php_packet_soap.c
  M  ext/soap/php_schema.c
  M  ext/soap/php_sdl.c
  M  ext/soap/soap.c


Diff:

diff --git a/ext/soap/php_encoding.c b/ext/soap/php_encoding.c
index f02a16638025..e474798df6da 100644
--- a/ext/soap/php_encoding.c
+++ b/ext/soap/php_encoding.c
@@ -2052,7 +2052,7 @@ static int calc_dimension_12(const char* str)
 
 static void soap_array_position_add_digit(int *position, int digit)
 {
-	if (*position > (INT_MAX - digit) / 10) {
+	if (UNEXPECTED(*position > (INT_MAX - digit) / 10)) {
 		soap_error0(E_ERROR, "Encoding: array index out of range");
 	}
 
@@ -2697,7 +2697,7 @@ static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
 			i = dimension;
 			while (i > 0) {
 				i--;
-				if (pos[i] == INT_MAX) {
+				if (UNEXPECTED(pos[i] == INT_MAX)) {
 					efree(dims);
 					efree(pos);
 					zval_ptr_dtor(ret);
diff --git a/ext/soap/php_http.c b/ext/soap/php_http.c
index 4fe272ea6f13..b02942f3dccb 100644
--- a/ext/soap/php_http.c
+++ b/ext/soap/php_http.c
@@ -1488,7 +1488,7 @@ static zend_string* get_http_body(php_stream *stream, bool close, zend_string *h
 				if (buf_size > 0) {
 					size_t len_size = 0;
 
-					if (http_buf_size + buf_size + 1 < 0) {
+					if (UNEXPECTED(http_buf_size + buf_size + 1 < 0)) {
 						if (http_buf) {
 							zend_string_release_ex(http_buf, 0);
 						}
@@ -1503,7 +1503,7 @@ static zend_string* get_http_body(php_stream *stream, bool close, zend_string *h
 
 					while (len_size < buf_size) {
 						ssize_t len_read = php_stream_read(stream, http_buf->val + http_buf_size, buf_size - len_size);
-						if (len_read <= 0) {
+						if (UNEXPECTED(len_read <= 0)) {
 							/* Error or EOF */
 							done = true;
 						  break;
@@ -1517,7 +1517,7 @@ static zend_string* get_http_body(php_stream *stream, bool close, zend_string *h
 					if (ch == '\r') {
 						ch = php_stream_getc(stream);
 					}
-					if (ch != '\n') {
+					if (UNEXPECTED(ch != '\n')) {
 						/* Something wrong in chunked encoding */
 						if (http_buf) {
 							zend_string_release_ex(http_buf, 0);
@@ -1555,13 +1555,13 @@ static zend_string* get_http_body(php_stream *stream, bool close, zend_string *h
 		}
 
 	} else if (header_length) {
-		if (header_length < 0 || header_length >= INT_MAX) {
+		if (UNEXPECTED(header_length < 0 || header_length >= INT_MAX)) {
 			return NULL;
 		}
 		http_buf = zend_string_alloc(header_length, 0);
 		while (http_buf_size < header_length) {
 			ssize_t len_read = php_stream_read(stream, http_buf->val + http_buf_size, header_length - http_buf_size);
-			if (len_read <= 0) {
+			if (UNEXPECTED(len_read <= 0)) {
 				break;
 			}
 			http_buf_size += len_read;
diff --git a/ext/soap/php_packet_soap.c b/ext/soap/php_packet_soap.c
index 68ed9a7a7689..29ed1294f364 100644
--- a/ext/soap/php_packet_soap.c
+++ b/ext/soap/php_packet_soap.c
@@ -59,11 +59,11 @@ bool parse_packet_soap(zval *this_ptr, zend_string *buffer, sdlFunctionPtr fn, z
 	/* Parse XML packet */
 	response = soap_xmlParseMemory(ZSTR_VAL(buffer), ZSTR_LEN(buffer));
 
-	if (!response) {
+	if (UNEXPECTED(!response)) {
 		add_soap_fault(this_ptr, "Client", "looks like we got no XML document", NULL, NULL, soap_lang_en);
 		return false;
 	}
-	if (xmlGetIntSubset(response) != NULL) {
+	if (UNEXPECTED(xmlGetIntSubset(response) != NULL)) {
 		add_soap_fault(this_ptr, "Client", "DTD are not supported by SOAP", NULL, NULL, soap_lang_en);
 		xmlFreeDoc(response);
 		return false;
@@ -90,7 +90,7 @@ bool parse_packet_soap(zval *this_ptr, zend_string *buffer, sdlFunctionPtr fn, z
 		}
 		trav = trav->next;
 	}
-	if (env == NULL) {
+	if (UNEXPECTED(env == NULL)) {
 		add_soap_fault(this_ptr, "Client", "looks like we got XML without \"Envelope\" element", NULL, NULL, soap_lang_en);
 		xmlFreeDoc(response);
 		return false;
@@ -139,7 +139,7 @@ bool parse_packet_soap(zval *this_ptr, zend_string *buffer, sdlFunctionPtr fn, z
 	while (trav != NULL && trav->type != XML_ELEMENT_NODE) {
 		trav = trav->next;
 	}
-	if (body == NULL) {
+	if (UNEXPECTED(body == NULL)) {
 		add_soap_fault(this_ptr, "Client", "Body must be present in a SOAP envelope", NULL, NULL, soap_lang_en);
 		xmlFreeDoc(response);
 		return false;
@@ -165,7 +165,7 @@ bool parse_packet_soap(zval *this_ptr, zend_string *buffer, sdlFunctionPtr fn, z
 		}
 		attr = attr->next;
 	}
-	if (trav != NULL && soap_version == SOAP_1_2) {
+	if (UNEXPECTED(trav != NULL && soap_version == SOAP_1_2)) {
 		add_soap_fault(this_ptr, "Client", "A SOAP 1.2 envelope can contain only Header and Body", NULL, NULL, soap_lang_en);
 		xmlFreeDoc(response);
 		return false;
diff --git a/ext/soap/php_schema.c b/ext/soap/php_schema.c
index d97a0eac05b5..b675b97b469f 100644
--- a/ext/soap/php_schema.c
+++ b/ext/soap/php_schema.c
@@ -63,12 +63,12 @@ static int schema_parse_int(const xmlChar *value, const char *name, bool allow_n
 	if (type != IS_LONG) {
 		errno = 0;
 		lval = ZEND_STRTOL(str, NULL, 10);
-		if (oflow_info || (errno == ERANGE && lval != 0)) {
+		if (UNEXPECTED(oflow_info || (errno == ERANGE && lval != 0))) {
 			soap_error1(E_ERROR, "Parsing Schema: %s value is out of range", name);
 		}
 	}
 
-	if (ZEND_LONG_EXCEEDS_INT(lval) || (!allow_negative && lval < 0)) {
+	if (UNEXPECTED(ZEND_LONG_EXCEEDS_INT(lval) || (!allow_negative && lval < 0))) {
 		soap_error1(E_ERROR, "Parsing Schema: %s value is out of range", name);
 	}
 
diff --git a/ext/soap/php_sdl.c b/ext/soap/php_sdl.c
index 07c5dbf4f12e..73886af5a567 100644
--- a/ext/soap/php_sdl.c
+++ b/ext/soap/php_sdl.c
@@ -1524,22 +1524,22 @@ static sdlPtr get_sdl_from_cache(const char *fn, const char *uri, size_t uri_len
 	char *in, *buf;
 
 	f = open(fn, O_RDONLY|O_BINARY);
-	if (f < 0) {
+	if (UNEXPECTED(f < 0)) {
 		return NULL;
 	}
-	if (fstat(f, &st) != 0) {
+	if (UNEXPECTED(fstat(f, &st) != 0)) {
 		close(f);
 		return NULL;
 	}
 	buf = in = emalloc(st.st_size);
-	if (read(f, in, st.st_size) != st.st_size) {
+	if (UNEXPECTED(read(f, in, st.st_size) != st.st_size)) {
 		close(f);
 		efree(in);
 		return NULL;
 	}
 	close(f);
 
-	if (strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0') {
+	if (UNEXPECTED(strncmp(in,"wsdl",4) != 0 || in[4] != WSDL_CACHE_VERSION || in[5] != '\0')) {
 		unlink(fn);
 		efree(buf);
 		return NULL;
@@ -2096,7 +2096,7 @@ static void add_sdl_to_cache(const char *fn, const char *uri, time_t t, sdlPtr s
 	zend_string *temp_file_path;
 	f = php_open_temporary_fd_ex(SOAP_GLOBAL(cache_dir), "tmp.wsdl.", &temp_file_path, PHP_TMP_FILE_SILENT);
 
-	if (f < 0) {return;}
+	if (UNEXPECTED(f < 0)) {return;}
 
 	zend_hash_init(&tmp_types, 0, NULL, NULL, 0);
 	zend_hash_init(&tmp_encoders, 0, NULL, NULL, 0);
diff --git a/ext/soap/soap.c b/ext/soap/soap.c
index a3e03e0fb58b..171b529b3355 100644
--- a/ext/soap/soap.c
+++ b/ext/soap/soap.c
@@ -1008,7 +1008,7 @@ PHP_METHOD(SoapServer, __construct)
 				goto cleanup;
 			}
 			// TODO: this still accepts mixed keys arrays and not all numerically indexed arrays are packed
-			if (HT_IS_PACKED(Z_ARRVAL_P(class_map_zv))) {
+			if (UNEXPECTED(HT_IS_PACKED(Z_ARRVAL_P(class_map_zv)))) {
 				zend_argument_value_error(2, "\"classmap\" option must be an associative array");
 				goto cleanup;
 			}
@@ -2224,7 +2224,7 @@ PHP_METHOD(SoapClient, __construct)
 			xmlCharEncodingHandlerPtr encoding;
 
 			encoding = xmlFindCharEncodingHandler(Z_STRVAL_P(tmp));
-			if (encoding == NULL) {
+			if (UNEXPECTED(encoding == NULL)) {
 				php_error_docref(NULL, E_ERROR, "Invalid 'encoding' option - '%s'", Z_STRVAL_P(tmp));
 			} else {
 				xmlCharEncCloseFunc(encoding);
@@ -2233,7 +2233,7 @@ PHP_METHOD(SoapClient, __construct)
 		}
 		if ((tmp = zend_hash_str_find(ht, "classmap", sizeof("classmap")-1)) != NULL &&
 			Z_TYPE_P(tmp) == IS_ARRAY) {
-			if (HT_IS_PACKED(Z_ARRVAL_P(tmp))) {
+			if (UNEXPECTED(HT_IS_PACKED(Z_ARRVAL_P(tmp)))) {
 				php_error_docref(NULL, E_ERROR, "'classmap' option must be an associative array");
 			}
 			ZVAL_COPY(Z_CLIENT_CLASSMAP_P(this_ptr), tmp);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.