Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions mailparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ PHP_FUNCTION(mailparse_uudecode_all)
{
zval *file, item;
char *buffer = NULL;
zend_string *outpath;
zend_string *outpath, *partpath = NULL;
int nparts = 0;
php_stream *instream, *outstream = NULL, *partstream = NULL;

Expand Down Expand Up @@ -839,23 +839,27 @@ PHP_FUNCTION(mailparse_uudecode_all)
add_assoc_string(&item, "filename", ZSTR_VAL(outpath));
add_next_index_zval(return_value, &item);
zend_string_release(outpath);
outpath = NULL;
}

/* add an item */
array_init(&item);
add_assoc_string(&item, "origfilename", origfilename);

/* create a temp file for the data */
partstream = _mailparse_create_stream(&outpath);
partstream = _mailparse_create_stream(&partpath);
if (partstream) {
nparts++;
add_assoc_string(&item, "filename", ZSTR_VAL(outpath));
add_assoc_string(&item, "filename", ZSTR_VAL(partpath));
add_next_index_zval(return_value, &item);

/* decode it */
mailparse_do_uudecode(instream, partstream);
php_stream_close(partstream);
zend_string_release(outpath);
zend_string_release(partpath);
} else {
/* temp-file creation failed: drop the half-built item */
zval_ptr_dtor(&item);
}
} else {
/* write to the output file */
Expand All @@ -867,7 +871,14 @@ PHP_FUNCTION(mailparse_uudecode_all)
efree(buffer);

if (nparts == 0) {
/* delete temporary file */
/* no uuencoded part decoded: release the unused initial temp-file
* path, or discard the partially-built array if a begin line was seen
* but every part failed to decode */
if (outpath) {
zend_string_release(outpath);
} else {
zval_ptr_dtor(return_value);
}
RETURN_FALSE;
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/uudecode_all_no_begin.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
mailparse_uudecode_all on input without a "begin" line does not leak
--SKIPIF--
<?php if (!extension_loaded("mailparse")) print "skip"; ?>
--FILE--
<?php
/* With no "begin" line the temp-file path string was never released. */
$fp = fopen("php://memory", "r+");
fwrite($fp, "plain text\nno uuencoded data here\n");
rewind($fp);
var_dump(mailparse_uudecode_all($fp));
fclose($fp);
echo "done\n";
?>
--EXPECT--
bool(false)
done
Loading