Skip to content

Commit 4134ebe

Browse files
committed
fixed strndup usage in the pgsql ext
1 parent cf39c3d commit 4134ebe

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()
@@ -6007,7 +6029,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
60076029
size_t new_len, field_len = strlen(field);
60086030

60096031
if (_php_pgsql_detect_identifier_escape(field, field_len) == SUCCESS) {
6010-
escaped = strndup(field, field_len);
6032+
escaped = _php_pgsql_strndup(field, field_len);
60116033
} else {
60126034
#if HAVE_PQESCAPELITERAL
60136035
escaped = PQescapeIdentifier(pg_link, field, field_len);
@@ -6101,7 +6123,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c
61016123
token = php_strtok_r(table_copy, ".", &tmp);
61026124
len = strlen(token);
61036125
if (_php_pgsql_detect_identifier_escape(token, len) == SUCCESS) {
6104-
escaped = strndup(token, len);
6126+
escaped = _php_pgsql_strndup(token, len);
61056127
} else {
61066128
#if HAVE_PQESCAPELITERAL
61076129
escaped = PQescapeIdentifier(pg_link, token, len);
@@ -6115,7 +6137,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c
61156137
len = strlen(tmp);
61166138
/* "schema"."table" format */
61176139
if (_php_pgsql_detect_identifier_escape(tmp, len) == SUCCESS) {
6118-
escaped = strndup(tmp, len);
6140+
escaped = _php_pgsql_strndup(tmp, len);
61196141
} else {
61206142
#if HAVE_PQESCAPELITERAL
61216143
escaped = PQescapeIdentifier(pg_link, tmp, len);

0 commit comments

Comments
 (0)