Skip to content

Commit 520c00a

Browse files
committed
Fix #81223: flock() only locks first byte of file
`flock()` should lock the whole file, like on other systems which use mandatory locking. We cannot use `0` like for `flck.l_len`, so we use the largest number, what is valid according to the documentation: <https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex#remarks>. Closes GH-7216.
1 parent 28c9376 commit 520c00a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ PHP NEWS
2626
- PCRE:
2727
. Fixed bug #81101 (PCRE2 10.37 shows unexpected result). (Anatol)
2828

29+
- Standard:
30+
. Fixed bug #81223 (flock() only locks first byte of file). (cmb)
31+
2932
01 Jul 2021, PHP 7.4.21
3033

3134
- Core:

ext/standard/flock_compat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ PHPAPI int php_flock(int fd, int operation)
116116
*/
117117
{
118118
HANDLE hdl = (HANDLE) _get_osfhandle(fd);
119-
DWORD low = 1, high = 0;
119+
DWORD low = 0xFFFFFFFF, high = 0xFFFFFFFF;
120120
OVERLAPPED offset =
121121
{0, 0, 0, 0, NULL};
122122
DWORD err;

ext/standard/tests/file/bug81223.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #81223 (flock() only locks first byte of file)
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
6+
?>
7+
--FILE--
8+
<?php
9+
$filename = __FILE__;
10+
$stream1 = fopen($filename, "r");
11+
var_dump(flock($stream1, LOCK_EX));
12+
$stream2 = fopen($filename, "r");
13+
var_dump(fread($stream2, 5));
14+
fseek($stream2, 1);
15+
var_dump(fread($stream2, 4));
16+
?>
17+
--EXPECTF--
18+
bool(true)
19+
20+
Notice: fread(): read of %d bytes failed with errno=13 Permission denied in %s on line %d
21+
bool(false)
22+
23+
Notice: fread(): read of %d bytes failed with errno=13 Permission denied in %s on line %d
24+
bool(false)

0 commit comments

Comments
 (0)