Skip to content

Commit 0301243

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 201c691 commit 0301243

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
@@ -25,6 +25,7 @@
2525
#include <libxml/parserInternals.h>
2626
#include "zend_strtod.h"
2727
#include "zend_interfaces.h"
28+
#include "zend_enum.h"
2829

2930
/* zval type decode */
3031
static zval *to_zval_double(zval* ret, encodeTypePtr type, xmlNodePtr data);
@@ -830,33 +831,34 @@ static zval *to_zval_hexbin(zval *ret, encodeTypePtr type, xmlNodePtr data)
830831
return ret;
831832
}
832833

834+
static zend_string *get_serialization_string_from_zval(zval *data)
835+
{
836+
switch (Z_TYPE_P(data)) {
837+
default:
838+
return zval_get_string_func(data);
839+
}
840+
}
841+
833842
static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNodePtr parent)
834843
{
835844
xmlNodePtr ret, text;
836-
char *str;
837-
int new_len;
838845

839846
ret = xmlNewNode(NULL, BAD_CAST("BOGUS"));
840847
xmlAddChild(parent, ret);
841848
FIND_ZVAL_NULL(data, ret, style);
842849

843-
if (Z_TYPE_P(data) == IS_STRING) {
844-
str = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
845-
new_len = Z_STRLEN_P(data);
846-
} else {
847-
zend_string *tmp = zval_get_string_func(data);
848-
str = estrndup(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
849-
new_len = ZSTR_LEN(tmp);
850-
zend_string_release_ex(tmp, 0);
851-
}
850+
zend_string *serialization = get_serialization_string_from_zval(data);
851+
char *str = ZSTR_VAL(serialization);
852+
size_t new_len = ZSTR_LEN(serialization);
852853

853854
if (SOAP_GLOBAL(encoding) != NULL) {
854855
xmlBufferPtr in = xmlBufferCreateStatic(str, new_len);
855856
xmlBufferPtr out = xmlBufferCreate();
856857
int n = xmlCharEncInFunc(SOAP_GLOBAL(encoding), out, in);
857858

858859
if (n >= 0) {
859-
efree(str);
860+
zend_string_release(serialization);
861+
serialization = NULL;
860862
str = estrdup((char*)xmlBufferContent(out));
861863
new_len = n;
862864
}
@@ -907,7 +909,11 @@ static xmlNodePtr to_xml_string(encodeTypePtr type, zval *data, int style, xmlNo
907909

908910
text = xmlNewTextLen(BAD_CAST(str), new_len);
909911
xmlAddChild(ret, text);
910-
efree(str);
912+
if (serialization) {
913+
zend_string_release(serialization);
914+
} else {
915+
efree(str);
916+
}
911917

912918
if (style == SOAP_ENCODED) {
913919
set_ns_and_type(ret, type);

0 commit comments

Comments
 (0)