Skip to content

Commit

Permalink
remove hidden limit from fgets (VKCOM#552)
Browse files Browse the repository at this point in the history
Remove hidden limit in function fgets when parameter is -1 and set it to size of file
  • Loading branch information
astrophysik authored Jul 7, 2022
1 parent 8438196 commit e69c843
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion runtime/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <errno.h>
#include <libgen.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <unistd.h>

#undef basename
Expand Down Expand Up @@ -719,7 +720,14 @@ static Optional<string> file_fgets(const Stream &stream, int64_t length) {
}

if (length < 0) {
length = 1024; // TODO remove limit
struct stat st{};
int fd = fileno(f);
fstat(fd, &st);
if (st.st_size > 0) {
length = st.st_size + 1;
} else {
return false;
}
}
if (length > string::max_size()) {
php_warning("Parameter length in function fgetc is too large");
Expand Down
9 changes: 9 additions & 0 deletions tests/phpt/dl/473_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@
@var_dump (file_put_contents ('tmp', 'test'));
var_dump (file_put_contents ($filename, "test-test-test"));
var_dump (file_get_contents ($filename));
var_dump (file_put_contents ($filename, ''));
fclose($file);

$large_text = str_repeat("s", 3000);
var_dump (file_put_contents($filename, $large_text));
$file = fopen ($filename, 'r');
$large_line = fgets ($file);
var_dump (strlen ($large_line));
fclose ($file);
unlink ($filename);

@var_dump (realpath ("../dl/473-file.php"));
Expand Down

0 comments on commit e69c843

Please sign in to comment.