Skip to content

Commit 07e646f

Browse files
committed
Cleanup (avoid reallocation)
1 parent 9ea98d6 commit 07e646f

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

ext/odbc/php_odbc.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,7 @@ PHP_FUNCTION(odbc_fetch_row)
20582058
PHP_FUNCTION(odbc_result)
20592059
{
20602060
char *field;
2061+
zend_string *field_str;
20612062
int field_ind;
20622063
SQLSMALLINT sql_c_type = SQL_C_CHAR;
20632064
odbc_result *result;
@@ -2163,25 +2164,25 @@ PHP_FUNCTION(odbc_result)
21632164
}
21642165
/* For char data, the length of the returned string will be longreadlen - 1 */
21652166
fieldsize = (result->longreadlen <= 0) ? 4096 : result->longreadlen;
2166-
field = emalloc(fieldsize);
2167+
field_str = zend_string_alloc(fieldsize, 0);
21672168

21682169
/* SQLGetData will truncate CHAR data to fieldsize - 1 bytes and append \0.
21692170
* For binary data it is truncated to fieldsize bytes.
21702171
*/
21712172
rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1), sql_c_type,
2172-
field, fieldsize, &result->values[field_ind].vallen);
2173+
ZSTR_VAL(field_str), fieldsize, &result->values[field_ind].vallen);
21732174

21742175
if (rc == SQL_ERROR) {
21752176
odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2176-
efree(field);
2177+
zend_string_free(field_str);
21772178
RETURN_FALSE;
21782179
}
21792180

21802181
if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2181-
efree(field);
2182+
zend_string_free(field_str);
21822183
RETURN_NULL();
21832184
} else if (rc == SQL_NO_DATA_FOUND) {
2184-
efree(field);
2185+
zend_string_free(field_str);
21852186
RETURN_FALSE;
21862187
}
21872188
/* Reduce fieldlen by 1 if we have char data. One day we might
@@ -2196,10 +2197,10 @@ PHP_FUNCTION(odbc_result)
21962197
/* Don't duplicate result, saves one emalloc.
21972198
For SQL_SUCCESS, the length is in vallen.
21982199
*/
2199-
RETVAL_STRINGL(field, (rc == SQL_SUCCESS_WITH_INFO) ? fieldsize : result->values[field_ind].vallen);
2200-
// TODO: avoid dpouble reallocation ???
2201-
efree(field);
2202-
return;
2200+
if (rc != SQL_SUCCESS_WITH_INFO) {
2201+
field_str = zend_string_truncate(field_str, result->values[field_ind].vallen, 0);
2202+
}
2203+
RETURN_NEW_STR(field_str);
22032204
break;
22042205

22052206
default:

0 commit comments

Comments
 (0)