Skip to content

Commit af7705b

Browse files
committed
Avoid strings duplication (zend_hash* and printf may work with non zero terminated strings)
1 parent e221e73 commit af7705b

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

main/streams/memory.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, con
638638
{
639639
php_stream *stream;
640640
php_stream_temp_data *ts;
641-
char *comma, *semi, *sep, *key;
641+
char *comma, *semi, *sep;
642642
size_t mlen, dlen, plen, vlen, ilen;
643643
zend_off_t newoffs;
644644
zval meta;
@@ -710,11 +710,9 @@ static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, con
710710
/* found parameter ... the heart of cs ppl lies in +1/-1 or was it +2 this time? */
711711
plen = sep - path;
712712
vlen = (semi ? (size_t)(semi - sep) : (mlen - plen)) - 1 /* '=' */;
713-
key = estrndup(path, plen);
714-
if (plen != sizeof("mediatype")-1 || memcmp(key, "mediatype", sizeof("mediatype")-1)) {
715-
add_assoc_stringl_ex(&meta, key, plen, sep + 1, vlen);
713+
if (plen != sizeof("mediatype")-1 || memcmp(path, "mediatype", sizeof("mediatype")-1)) {
714+
add_assoc_stringl_ex(&meta, path, plen, sep + 1, vlen);
716715
}
717-
efree(key);
718716
plen += vlen + 1;
719717
mlen -= plen;
720718
path += plen;

main/streams/streams.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,10 +1742,11 @@ PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const
17421742
}
17431743

17441744
if (protocol) {
1745-
char *tmp = estrndup(protocol, n);
1746-
if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, (char*)tmp, n))) {
1745+
if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, protocol, n))) {
1746+
char *tmp = estrndup(protocol, n);
1747+
17471748
php_strtolower(tmp, n);
1748-
if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, (char*)tmp, n))) {
1749+
if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, tmp, n))) {
17491750
char wrapper_name[32];
17501751

17511752
if (n >= sizeof(wrapper_name)) {
@@ -1758,8 +1759,8 @@ PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const
17581759
wrapper = NULL;
17591760
protocol = NULL;
17601761
}
1762+
efree(tmp);
17611763
}
1762-
efree(tmp);
17631764
}
17641765
/* TODO: curl based streams probably support file:// properly */
17651766
if (!protocol || !strncasecmp(protocol, "file", n)) {
@@ -1833,13 +1834,11 @@ PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const
18331834
PG(in_user_include)) && !PG(allow_url_include)))) {
18341835
if (options & REPORT_ERRORS) {
18351836
/* protocol[n] probably isn't '\0' */
1836-
char *protocol_dup = estrndup(protocol, n);
18371837
if (!PG(allow_url_fopen)) {
1838-
php_error_docref(NULL, E_WARNING, "%s:// wrapper is disabled in the server configuration by allow_url_fopen=0", protocol_dup);
1838+
php_error_docref(NULL, E_WARNING, "%.*s:// wrapper is disabled in the server configuration by allow_url_fopen=0", (int)n, protocol);
18391839
} else {
1840-
php_error_docref(NULL, E_WARNING, "%s:// wrapper is disabled in the server configuration by allow_url_include=0", protocol_dup);
1840+
php_error_docref(NULL, E_WARNING, "%.*s:// wrapper is disabled in the server configuration by allow_url_include=0", (int)n, protocol);
18411841
}
1842-
efree(protocol_dup);
18431842
}
18441843
return NULL;
18451844
}

main/streams/transports.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in
112112
}
113113

114114
if (protocol) {
115-
char *tmp = estrndup(protocol, n);
116-
if (NULL == (factory = zend_hash_str_find_ptr(&xport_hash, tmp, n))) {
115+
if (NULL == (factory = zend_hash_str_find_ptr(&xport_hash, protocol, n))) {
117116
char wrapper_name[32];
118117

119118
if (n >= sizeof(wrapper_name))
@@ -123,10 +122,8 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in
123122
ERR_REPORT(error_string, "Unable to find the socket transport \"%s\" - did you forget to enable it when you configured PHP?",
124123
wrapper_name);
125124

126-
efree(tmp);
127125
return NULL;
128126
}
129-
efree(tmp);
130127
}
131128

132129
if (factory == NULL) {

0 commit comments

Comments
 (0)