Skip to content

Commit 2f4de24

Browse files
committed
handle error in xzdecode
1 parent 6f524d8 commit 2f4de24

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

tests/xzdecode.phpt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Test `xzdecode` simple case
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("xz")) {
6+
print("XZ extension is not loaded!");
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
$str = file_get_contents(__FILE__);
13+
14+
$encoded = xzencode($str);
15+
print("encoding finished\n");
16+
$decoded = xzdecode($encoded);
17+
print("decoding finished\n");
18+
var_dump($str === $decoded);
19+
20+
print("empty string\n");
21+
var_dump(xzdecode(""));
22+
print("garbage\n");
23+
var_dump(xzdecode("this is not XZ data"));
24+
?>
25+
--EXPECT--
26+
encoding finished
27+
decoding finished
28+
bool(true)
29+
empty string
30+
bool(false)
31+
garbage
32+
bool(false)

xz.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ PHP_FUNCTION(xzdecode)
273273
return;
274274
}
275275

276+
if (!in_len) {
277+
RETURN_BOOL(0); /* empty string is not xz data */
278+
}
279+
276280
/* The output string (encoded). */
277281
uint8_t *out = NULL;
278282
/* The length of the output string. */
@@ -300,6 +304,14 @@ PHP_FUNCTION(xzdecode)
300304
lzma_ret status = LZMA_OK;
301305
while (strm.avail_in != 0) {
302306
status = lzma_code(&strm, LZMA_RUN);
307+
if (status != LZMA_OK && status != LZMA_STREAM_END) {
308+
if (out) {
309+
efree(out);
310+
}
311+
lzma_end(&strm);
312+
RETURN_BOOL(0); /* probably not xz data */
313+
}
314+
303315
/* More memory is required. */
304316
if (strm.avail_out == 0) {
305317
out = memmerge(out, buff, out_len, XZ_BUFFER_SIZE);
@@ -308,7 +320,6 @@ PHP_FUNCTION(xzdecode)
308320
strm.next_out = buff;
309321
}
310322
}
311-
(void)status; // avoid -Wunused-but-set-variable warning
312323
/* Merging last fragment. */
313324
out = memmerge(out, buff, out_len, XZ_BUFFER_SIZE - strm.avail_out);
314325
out_len += XZ_BUFFER_SIZE - strm.avail_out;

0 commit comments

Comments
 (0)