Skip to content

Commit 42171b1

Browse files
committed
Merge branch 'PHP-5.5'
* PHP-5.5: fixed strndup usage in the pgsql ext
2 parents acea91b + 8719898 commit 42171b1

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

ext/pgsql/pgsql.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,30 @@ static char *_php_pgsql_escape_identifier(const char *field, size_t field_len)
966966
field_escaped[j] = '\0';
967967
return field_escaped;
968968
}
969+
/* }}} */
969970
#endif
970971

972+
/* {{{ _php_pgsql_strndup, no strndup should be used */
973+
static char *_php_pgsql_strndup(const char *s, size_t len)
974+
{
975+
char *new;
976+
977+
if (NULL == s) {
978+
return (char *)NULL;
979+
}
980+
981+
new = (char *) malloc(len + 1);
982+
983+
if (NULL == new) {
984+
return (char *)NULL;
985+
}
986+
987+
new[len] = '\0';
988+
989+
return memmove(new, s, len);
990+
}
991+
/* }}} */
992+
971993
/* {{{ PHP_INI
972994
*/
973995
PHP_INI_BEGIN()
@@ -6008,7 +6030,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
60086030
size_t new_len, field_len = strlen(field);
60096031

60106032
if (_php_pgsql_detect_identifier_escape(field, field_len) == SUCCESS) {
6011-
escaped = strndup(field, field_len);
6033+
escaped = _php_pgsql_strndup(field, field_len);
60126034
} else {
60136035
#if HAVE_PQESCAPELITERAL
60146036
escaped = PQescapeIdentifier(pg_link, field, field_len);
@@ -6102,7 +6124,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c
61026124
token = php_strtok_r(table_copy, ".", &tmp);
61036125
len = strlen(token);
61046126
if (_php_pgsql_detect_identifier_escape(token, len) == SUCCESS) {
6105-
escaped = strndup(token, len);
6127+
escaped = _php_pgsql_strndup(token, len);
61066128
} else {
61076129
#if HAVE_PQESCAPELITERAL
61086130
escaped = PQescapeIdentifier(pg_link, token, len);
@@ -6116,7 +6138,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c
61166138
len = strlen(tmp);
61176139
/* "schema"."table" format */
61186140
if (_php_pgsql_detect_identifier_escape(tmp, len) == SUCCESS) {
6119-
escaped = strndup(tmp, len);
6141+
escaped = _php_pgsql_strndup(tmp, len);
61206142
} else {
61216143
#if HAVE_PQESCAPELITERAL
61226144
escaped = PQescapeIdentifier(pg_link, tmp, len);

0 commit comments

Comments
 (0)