Skip to content

Commit cc04642

Browse files
authored
Avoid copying the local name in SOAP's parse_namespace() (#15862)
The local name is either the entire input or is the last part, so we never need to make a copy.
1 parent ffb4405 commit cc04642

File tree

5 files changed

+46
-47
lines changed

5 files changed

+46
-47
lines changed

ext/soap/php_encoding.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@ static zval *master_to_zval_int(zval *ret, encodePtr encode, xmlNodePtr data)
538538
if (type_attr != NULL) {
539539
encodePtr new_enc;
540540
xmlNsPtr nsptr;
541-
char *ns, *cptype;
541+
const char *cptype;
542+
char *ns;
542543
smart_str nscat = {0};
543544

544545
parse_namespace(type_attr->children->content, &cptype, &ns);
@@ -549,7 +550,6 @@ static zval *master_to_zval_int(zval *ret, encodePtr encode, xmlNodePtr data)
549550
}
550551
smart_str_appends(&nscat, cptype);
551552
smart_str_0(&nscat);
552-
efree(cptype);
553553
if (ns) {efree(ns);}
554554
if ((new_enc = zend_hash_find_ptr(SOAP_GLOBAL(typemap), nscat.s)) != NULL) {
555555
encode = new_enc;
@@ -2466,7 +2466,8 @@ static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
24662466
if (data &&
24672467
(attr = get_attribute(data->properties,"arrayType")) &&
24682468
attr->children && attr->children->content) {
2469-
char *type, *end, *ns;
2469+
const char *type;
2470+
char *end, *ns;
24702471
xmlNsPtr nsptr;
24712472

24722473
parse_namespace(attr->children->content, &type, &ns);
@@ -2481,21 +2482,20 @@ static zval *to_zval_array(zval *ret, encodeTypePtr type, xmlNodePtr data)
24812482
if (nsptr != NULL) {
24822483
enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
24832484
}
2484-
efree(type);
24852485
if (ns) {efree(ns);}
24862486

24872487
} else if ((attr = get_attribute(data->properties,"itemType")) &&
24882488
attr->children &&
24892489
attr->children->content) {
2490-
char *type, *ns;
2490+
const char *type;
2491+
char *ns;
24912492
xmlNsPtr nsptr;
24922493

24932494
parse_namespace(attr->children->content, &type, &ns);
24942495
nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
24952496
if (nsptr != NULL) {
24962497
enc = get_encoder(SOAP_GLOBAL(sdl), (char*)nsptr->href, type);
24972498
}
2498-
efree(type);
24992499
if (ns) {efree(ns);}
25002500

25012501
if ((attr = get_attribute(data->properties,"arraySize")) &&
@@ -2828,7 +2828,8 @@ static zval *guess_zval_convert(zval *ret, encodeTypePtr type, xmlNodePtr data)
28282828
master_to_zval_int(ret, enc, data);
28292829
if (SOAP_GLOBAL(sdl) && type_name && enc->details.sdl_type) {
28302830
zval soapvar;
2831-
char *ns, *cptype;
2831+
const char *cptype;
2832+
char *ns;
28322833
xmlNsPtr nsptr;
28332834

28342835
object_init_ex(&soapvar, soap_var_class_entry);
@@ -2840,7 +2841,6 @@ static zval *guess_zval_convert(zval *ret, encodeTypePtr type, xmlNodePtr data)
28402841
if (nsptr) {
28412842
ZVAL_STRING(Z_VAR_ENC_NS_P(&soapvar), (char*)nsptr->href);
28422843
}
2843-
efree(cptype);
28442844
if (ns) {efree(ns);}
28452845
ZVAL_COPY_VALUE(ret, &soapvar);
28462846
}

ext/soap/php_schema.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ static int schema_list(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr listType, sdlTypeP
435435

436436
itemType = get_attribute(listType->properties, "itemType");
437437
if (itemType != NULL) {
438-
char *type, *ns;
438+
const char *type;
439+
char *ns;
439440
xmlNsPtr nsptr;
440441

441442
parse_namespace(itemType->children->content, &type, &ns);
@@ -457,7 +458,6 @@ static int schema_list(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr listType, sdlTypeP
457458
}
458459
zend_hash_next_index_insert_ptr(cur_type->elements, newType);
459460
}
460-
if (type) {efree(type);}
461461
if (ns) {efree(ns);}
462462
}
463463

@@ -519,7 +519,8 @@ static int schema_union(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr unionType, sdlTyp
519519
memberTypes = get_attribute(unionType->properties, "memberTypes");
520520
if (memberTypes != NULL) {
521521
char *str, *start, *end, *next;
522-
char *type, *ns;
522+
const char *type;
523+
char *ns;
523524
xmlNsPtr nsptr;
524525

525526
str = estrdup((char*)memberTypes->children->content);
@@ -553,7 +554,6 @@ static int schema_union(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr unionType, sdlTyp
553554
}
554555
zend_hash_next_index_insert_ptr(cur_type->elements, newType);
555556
}
556-
if (type) {efree(type);}
557557
if (ns) {efree(ns);}
558558

559559
start = next;
@@ -662,15 +662,15 @@ static int schema_restriction_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodeP
662662

663663
base = get_attribute(restType->properties, "base");
664664
if (base != NULL) {
665-
char *type, *ns;
665+
const char *type;
666+
char *ns;
666667
xmlNsPtr nsptr;
667668

668669
parse_namespace(base->children->content, &type, &ns);
669670
nsptr = xmlSearchNs(restType->doc, restType, BAD_CAST(ns));
670671
if (nsptr != NULL) {
671672
cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
672673
}
673-
if (type) {efree(type);}
674674
if (ns) {efree(ns);}
675675
} else if (!simpleType) {
676676
soap_error0(E_ERROR, "Parsing Schema: restriction has no 'base' attribute");
@@ -767,15 +767,15 @@ static int schema_restriction_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNode
767767

768768
base = get_attribute(restType->properties, "base");
769769
if (base != NULL) {
770-
char *type, *ns;
770+
const char *type;
771+
char *ns;
771772
xmlNsPtr nsptr;
772773

773774
parse_namespace(base->children->content, &type, &ns);
774775
nsptr = xmlSearchNs(restType->doc, restType, BAD_CAST(ns));
775776
if (nsptr != NULL) {
776777
cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
777778
}
778-
if (type) {efree(type);}
779779
if (ns) {efree(ns);}
780780
} else {
781781
soap_error0(E_ERROR, "Parsing Schema: restriction has no 'base' attribute");
@@ -892,15 +892,15 @@ static int schema_extension_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr
892892

893893
base = get_attribute(extType->properties, "base");
894894
if (base != NULL) {
895-
char *type, *ns;
895+
const char *type;
896+
char *ns;
896897
xmlNsPtr nsptr;
897898

898899
parse_namespace(base->children->content, &type, &ns);
899900
nsptr = xmlSearchNs(extType->doc, extType, BAD_CAST(ns));
900901
if (nsptr != NULL) {
901902
cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
902903
}
903-
if (type) {efree(type);}
904904
if (ns) {efree(ns);}
905905
} else {
906906
soap_error0(E_ERROR, "Parsing Schema: extension has no 'base' attribute");
@@ -947,15 +947,15 @@ static int schema_extension_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePt
947947

948948
base = get_attribute(extType->properties, "base");
949949
if (base != NULL) {
950-
char *type, *ns;
950+
const char *type;
951+
char *ns;
951952
xmlNsPtr nsptr;
952953

953954
parse_namespace(base->children->content, &type, &ns);
954955
nsptr = xmlSearchNs(extType->doc, extType, BAD_CAST(ns));
955956
if (nsptr != NULL) {
956957
cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
957958
}
958-
if (type) {efree(type);}
959959
if (ns) {efree(ns);}
960960
} else {
961961
soap_error0(E_ERROR, "Parsing Schema: extension has no 'base' attribute");
@@ -1096,7 +1096,8 @@ static int schema_group(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr groupType, sdlTyp
10961096
smart_str key = {0};
10971097

10981098
if (ref) {
1099-
char *type, *ns;
1099+
const char *type;
1100+
char *ns;
11001101
xmlNsPtr nsptr;
11011102

11021103
parse_namespace(ref->children->content, &type, &ns);
@@ -1120,7 +1121,6 @@ static int schema_group(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr groupType, sdlTyp
11201121
newModel->kind = XSD_CONTENT_GROUP_REF;
11211122
newModel->u.group_ref = estrndup(ZSTR_VAL(key.s), ZSTR_LEN(key.s));
11221123

1123-
if (type) {efree(type);}
11241124
if (ns) {efree(ns);}
11251125
} else {
11261126
newModel = emalloc(sizeof(sdlContentModel));
@@ -1534,7 +1534,8 @@ static int schema_element(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr element, sdlTyp
15341534

15351535
if (ref) {
15361536
smart_str nscat = {0};
1537-
char *type, *ns;
1537+
const char *type;
1538+
char *ns;
15381539
xmlNsPtr nsptr;
15391540

15401541
parse_namespace(ref->children->content, &type, &ns);
@@ -1555,7 +1556,6 @@ static int schema_element(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr element, sdlTyp
15551556
smart_str_appends(&nscat, type);
15561557
newType->name = estrdup(type);
15571558
smart_str_0(&nscat);
1558-
if (type) {efree(type);}
15591559
if (ns) {efree(ns);}
15601560
newType->ref = estrndup(ZSTR_VAL(nscat.s), ZSTR_LEN(nscat.s));
15611561
smart_str_free(&nscat);
@@ -1679,7 +1679,8 @@ static int schema_element(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr element, sdlTyp
16791679
/* type = QName */
16801680
type = get_attribute(attrs, "type");
16811681
if (type) {
1682-
char *cptype, *str_ns;
1682+
const char *cptype;
1683+
char *str_ns;
16831684
xmlNsPtr nsptr;
16841685

16851686
if (ref != NULL) {
@@ -1691,7 +1692,6 @@ static int schema_element(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr element, sdlTyp
16911692
cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(cptype));
16921693
}
16931694
if (str_ns) {efree(str_ns);}
1694-
if (cptype) {efree(cptype);}
16951695
}
16961696

16971697
trav = element->children;
@@ -1766,7 +1766,8 @@ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdl
17661766
memset(newAttr, 0, sizeof(sdlAttribute));
17671767

17681768
if (ref) {
1769-
char *attr_name, *ns;
1769+
const char *attr_name;
1770+
char *ns;
17701771
xmlNsPtr nsptr;
17711772

17721773
parse_namespace(ref->children->content, &attr_name, &ns);
@@ -1787,7 +1788,6 @@ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdl
17871788
smart_str_appends(&key, attr_name);
17881789
smart_str_0(&key);
17891790
newAttr->ref = estrndup(ZSTR_VAL(key.s), ZSTR_LEN(key.s));
1790-
if (attr_name) {efree(attr_name);}
17911791
if (ns) {efree(ns);}
17921792
} else {
17931793
xmlAttrPtr ns;
@@ -1827,7 +1827,8 @@ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdl
18271827
/* type = QName */
18281828
type = get_attribute(attrType->properties, "type");
18291829
if (type) {
1830-
char *cptype, *str_ns;
1830+
const char *cptype;
1831+
char *str_ns;
18311832
xmlNsPtr nsptr;
18321833

18331834
if (ref != NULL) {
@@ -1839,7 +1840,6 @@ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdl
18391840
newAttr->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(cptype));
18401841
}
18411842
if (str_ns) {efree(str_ns);}
1842-
if (cptype) {efree(cptype);}
18431843
}
18441844

18451845
attr = attrType->properties;
@@ -1881,7 +1881,8 @@ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdl
18811881
smart_str key2 = {0};
18821882
sdlExtraAttributePtr ext;
18831883
xmlNsPtr nsptr;
1884-
char *value, *ns;
1884+
const char *value;
1885+
char *ns;
18851886

18861887
ext = emalloc(sizeof(sdlExtraAttribute));
18871888
memset(ext, 0, sizeof(sdlExtraAttribute));
@@ -1894,7 +1895,6 @@ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdl
18941895
ext->val = estrdup((char*)attr->children->content);
18951896
}
18961897
if (ns) {efree(ns);}
1897-
efree(value);
18981898

18991899
if (!newAttr->extraAttributes) {
19001900
newAttr->extraAttributes = emalloc(sizeof(HashTable));
@@ -2007,7 +2007,8 @@ static int schema_attributeGroup(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrGrou
20072007
smart_str_free(&key);
20082008
} else if (ref) {
20092009
sdlAttributePtr newAttr;
2010-
char *group_name, *ns;
2010+
const char *group_name;
2011+
char *ns;
20112012
smart_str key = {0};
20122013
xmlNsPtr nsptr;
20132014

@@ -2027,7 +2028,6 @@ static int schema_attributeGroup(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrGrou
20272028
smart_str_appends(&key, group_name);
20282029
smart_str_0(&key);
20292030
newAttr->ref = estrndup(ZSTR_VAL(key.s), ZSTR_LEN(key.s));
2030-
if (group_name) {efree(group_name);}
20312031
if (ns) {efree(ns);}
20322032
smart_str_free(&key);
20332033

ext/soap/php_sdl.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *ty
4848
{
4949
encodePtr enc = NULL;
5050
xmlNsPtr nsptr;
51-
char *ns, *cptype;
51+
const char *cptype;
52+
char *ns;
5253

5354
parse_namespace(type, &cptype, &ns);
5455
nsptr = xmlSearchNs(node->doc, node, BAD_CAST(ns));
@@ -60,7 +61,6 @@ encodePtr get_encoder_from_prefix(sdlPtr sdl, xmlNodePtr node, const xmlChar *ty
6061
} else {
6162
enc = get_encoder_ex(sdl, (char*)type, xmlStrlen(type));
6263
}
63-
efree(cptype);
6464
if (ns) {efree(ns);}
6565
return enc;
6666
}
@@ -71,7 +71,8 @@ static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
7171

7272
if (sdl->elements) {
7373
xmlNsPtr nsptr;
74-
char *ns, *cptype;
74+
const char *cptype;
75+
char *ns;
7576
sdlTypePtr sdl_type;
7677

7778
parse_namespace(type, &cptype, &ns);
@@ -99,7 +100,6 @@ static sdlTypePtr get_element(sdlPtr sdl, xmlNodePtr node, const xmlChar *type)
99100
}
100101
}
101102

102-
efree(cptype);
103103
if (ns) {efree(ns);}
104104
}
105105
return ret;

ext/soap/php_xml.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,16 @@ xmlNodePtr get_node_with_attribute_recursive_ex(xmlNodePtr node, char *name, cha
315315
return NULL;
316316
}
317317

318-
int parse_namespace(const xmlChar *inval, char **value, char **namespace)
318+
/* namespace is either a copy or NULL, value is never NULL and never a copy. */
319+
void parse_namespace(const xmlChar *inval, const char **value, char **namespace)
319320
{
320-
char *found = strrchr((char*)inval, ':');
321+
const char *found = strrchr((const char *) inval, ':');
321322

322-
if (found != NULL && found != (char*)inval) {
323-
(*namespace) = estrndup((char*)inval, found - (char*)inval);
324-
(*value) = estrdup(++found);
323+
if (found != NULL && found != (const char *) inval) {
324+
(*namespace) = estrndup((const char *) inval, found - (const char *) inval);
325+
(*value) = ++found;
325326
} else {
326-
(*value) = estrdup((char*)inval);
327+
(*value) = (const char *) inval;
327328
(*namespace) = NULL;
328329
}
329-
330-
return FALSE;
331330
}

ext/soap/php_xml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ xmlNodePtr get_node_ex(xmlNodePtr node,char *name, char *ns);
3939
xmlNodePtr get_node_recursive_ex(xmlNodePtr node,char *name, char *ns);
4040
xmlNodePtr get_node_with_attribute_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns);
4141
xmlNodePtr get_node_with_attribute_recursive_ex(xmlNodePtr node, char *name, char *name_ns, char *attribute, char *value, char *attr_ns);
42-
int parse_namespace(const xmlChar *inval,char **value,char **namespace);
42+
void parse_namespace(const xmlChar *inval, const char **value, char **namespace);
4343

4444
#define FOREACHATTRNODE(n,c,i) FOREACHATTRNODEEX(n,c,NULL,i)
4545
#define FOREACHATTRNODEEX(n,c,ns,i) \

0 commit comments

Comments
 (0)