Minor fgetcsv cleanup

Reduce some variable scopes.
This commit is contained in:
Nikita Popov 2021-07-06 12:22:15 +02:00
parent 1a81251dbb
commit 076fbd360a

View file

@ -2047,10 +2047,10 @@ PHP_FUNCTION(fgetcsv)
PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value) /* {{{ */ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value) /* {{{ */
{ {
char *temp, *tptr, *bptr, *line_end, *limit; char *temp, *bptr, *line_end, *limit;
size_t temp_len, line_end_len; size_t temp_len, line_end_len;
int inc_len; int inc_len;
bool first_field = 1; bool first_field = true;
ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE); ZEND_ASSERT((escape_char >= 0 && escape_char <= UCHAR_MAX) || escape_char == PHP_CSV_NO_ESCAPE);
@ -2062,9 +2062,8 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
/* Strip trailing space from buf, saving end of line in case required for enclosure field */ /* Strip trailing space from buf, saving end of line in case required for enclosure field */
bptr = buf; bptr = buf;
tptr = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len); line_end = limit = (char *)php_fgetcsv_lookup_trailing_spaces(buf, buf_len);
line_end_len = buf_len - (size_t)(tptr - buf); line_end_len = buf_len - (size_t)(limit - buf);
line_end = limit = tptr;
/* reserve workspace for building each individual field */ /* reserve workspace for building each individual field */
temp_len = buf_len; temp_len = buf_len;
@ -2078,8 +2077,7 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
do { do {
char *comp_end, *hunk_begin; char *comp_end, *hunk_begin;
char *tptr = temp;
tptr = temp;
inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0); inc_len = (bptr < limit ? (*bptr == '\0' ? 1 : php_mblen(bptr, limit - bptr)): 0);
if (inc_len == 1) { if (inc_len == 1) {
@ -2096,7 +2094,7 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
add_next_index_null(return_value); add_next_index_null(return_value);
break; break;
} }
first_field = 0; first_field = false;
/* 2. Read field, leaving bptr pointing at start of next field */ /* 2. Read field, leaving bptr pointing at start of next field */
if (inc_len != 0 && *bptr == enclosure) { if (inc_len != 0 && *bptr == enclosure) {
int state = 0; int state = 0;
@ -2122,10 +2120,6 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
ZEND_FALLTHROUGH; ZEND_FALLTHROUGH;
case 0: { case 0: {
char *new_buf;
size_t new_len;
char *new_temp;
if (hunk_begin != line_end) { if (hunk_begin != line_end) {
memcpy(tptr, hunk_begin, bptr - hunk_begin); memcpy(tptr, hunk_begin, bptr - hunk_begin);
tptr += (bptr - hunk_begin); tptr += (bptr - hunk_begin);
@ -2138,7 +2132,11 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
if (stream == NULL) { if (stream == NULL) {
goto quit_loop_2; goto quit_loop_2;
} else if ((new_buf = php_stream_get_line(stream, NULL, 0, &new_len)) == NULL) { }
size_t new_len;
char *new_buf = php_stream_get_line(stream, NULL, 0, &new_len);
if (!new_buf) {
/* we've got an unterminated enclosure, /* we've got an unterminated enclosure,
* assign all the data from the start of * assign all the data from the start of
* the enclosure to end of data to the * the enclosure to end of data to the
@ -2150,8 +2148,9 @@ PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int
RETVAL_FALSE; RETVAL_FALSE;
goto out; goto out;
} }
temp_len += new_len; temp_len += new_len;
new_temp = erealloc(temp, temp_len); char *new_temp = erealloc(temp, temp_len);
tptr = new_temp + (size_t)(tptr - temp); tptr = new_temp + (size_t)(tptr - temp);
temp = new_temp; temp = new_temp;