zlib: use zend_string_{extend,truncate} over *_realloc (#18462)

These cases seemed obvious enough to me to confidently change as an
outsider to zlib.
This commit is contained in:
Levi Morrison 2025-04-30 08:18:04 -06:00 committed by GitHub
parent 39a56a1687
commit 6406d5f792
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -991,7 +991,7 @@ PHP_FUNCTION(inflate_add)
case Z_OK: case Z_OK:
if (ctx->Z.avail_out == 0) { if (ctx->Z.avail_out == 0) {
/* more output buffer space needed; realloc and try again */ /* more output buffer space needed; realloc and try again */
out = zend_string_realloc(out, ZSTR_LEN(out) + CHUNK_SIZE, 0); out = zend_string_extend(out, ZSTR_LEN(out) + CHUNK_SIZE, 0);
ctx->Z.avail_out = CHUNK_SIZE; ctx->Z.avail_out = CHUNK_SIZE;
ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used; ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
break; break;
@ -1003,7 +1003,7 @@ PHP_FUNCTION(inflate_add)
case Z_BUF_ERROR: case Z_BUF_ERROR:
if (flush_type == Z_FINISH && ctx->Z.avail_out == 0) { if (flush_type == Z_FINISH && ctx->Z.avail_out == 0) {
/* more output buffer space needed; realloc and try again */ /* more output buffer space needed; realloc and try again */
out = zend_string_realloc(out, ZSTR_LEN(out) + CHUNK_SIZE, 0); out = zend_string_extend(out, ZSTR_LEN(out) + CHUNK_SIZE, 0);
ctx->Z.avail_out = CHUNK_SIZE; ctx->Z.avail_out = CHUNK_SIZE;
ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used; ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
break; break;
@ -1039,7 +1039,7 @@ PHP_FUNCTION(inflate_add)
} while (1); } while (1);
complete: complete:
out = zend_string_realloc(out, buffer_used, 0); out = zend_string_truncate(out, buffer_used, 0);
ZSTR_VAL(out)[buffer_used] = 0; ZSTR_VAL(out)[buffer_used] = 0;
RETURN_STR(out); RETURN_STR(out);
} }
@ -1228,7 +1228,7 @@ PHP_FUNCTION(deflate_add)
if (ctx->Z.avail_out == 0) { if (ctx->Z.avail_out == 0) {
/* more output buffer space needed; realloc and try again */ /* more output buffer space needed; realloc and try again */
/* adding 64 more bytes solved every issue I have seen */ /* adding 64 more bytes solved every issue I have seen */
out = zend_string_realloc(out, ZSTR_LEN(out) + 64, 0); out = zend_string_extend(out, ZSTR_LEN(out) + 64, 0);
ctx->Z.avail_out = 64; ctx->Z.avail_out = 64;
ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used; ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
} }