Skip to content

Commit 56fea59

Browse files
committed
Introduce get_serialization_string_from_zval() and use it in to_xml_string()
For now this new function only returns a copy of the string, but its functionality will be expanded by later commits. to_xml_string() now uses this function and the memory management is simplified as well.
1 parent ad11cbc commit 56fea59

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

ext/soap/php_encoding.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <libxml/parserInternals.h>
2525
#include "zend_strtod.h"
2626
#include "zend_interfaces.h"
27+
#include "zend_enum.h"
2728

2829
/* zval type decode */
2930
static zval *to_zval_double(zval* ret, encodeTypePtr type, xmlNodePtr data);
@@ -822,33 +823,34 @@ static zval *to_zval_hexbin(zval *ret, encodeTypePtr type, xmlNodePtr data)
822823
return ret;
823824
}
824825

826+
static zend_string *get_serialization_string_from_zval(zval *data)
827+
{
828+
switch (Z_TYPE_P(data)) {
829+
default:
830+
return zval_get_string_func(data);
831+
}
832+
}
833+
825834
static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
826835
{
827836
xmlNodePtr ret, text;
828-
char *str;
829-
int new_len;
830837

831838
ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
832839
xmlAddChild(parent, ret);
833840
FIND_ZVAL_NULL(data, ret, style);
834841

835-
if (Z_TYPE_P(data) == IS_STRING) {
836-
str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
837-
new_len = Z_STRLEN_P(data);
838-
} else {
839-
zend_string *tmp = zval_get_string_func(data);
840-
str = estrndup(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
841-
new_len = ZSTR_LEN(tmp);
842-
zend_string_release_ex(tmp, 0);
843-
}
842+
zend_string *serialization = get_serialization_string_from_zval(data);
843+
char *str = ZSTR_VAL(serialization);
844+
size_t new_len = ZSTR_LEN(serialization);
844845

845846
if (SOAP_GLOBAL(encoding) != NULL) {
846847
xmlBufferPtr in = xmlBufferCreateStatic(str, new_len);
847848
xmlBufferPtr out = xmlBufferCreate();
848849
int n = xmlCharEncInFunc(SOAP_GLOBAL(encoding), out, in);
849850

850851
if (n >= 0) {
851-
efree(str);
852+
zend_string_release(serialization);
853+
serialization = NULL;
852854
str = estrdup((char*)xmlBufferContent(out));
853855
new_len = n;
854856
}
@@ -899,7 +901,11 @@ static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNo
899901

900902
text = xmlNewTextLen(BAD_CAST(str), new_len);
901903
xmlAddChild(ret, text);
902-
efree(str);
904+
if (serialization) {
905+
zend_string_release(serialization);
906+
} else {
907+
efree(str);
908+
}
903909

904910
if (style == SOAP_ENCODED) {
905911
set_ns_and_type(ret, type);

0 commit comments

Comments
 (0)