Skip to content

Commit

Permalink
Fix #81223: flock() only locks first byte of file
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
cmb69 committed Jul 6, 2021
1 parent 28c9376 commit 520c00a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ PHP NEWS
- PCRE:
. Fixed bug #81101 (PCRE2 10.37 shows unexpected result). (Anatol)

- Standard:
. Fixed bug #81223 (flock() only locks first byte of file). (cmb)

01 Jul 2021, PHP 7.4.21

- Core:
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/flock_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ PHPAPI int php_flock(int fd, int operation)
*/
{
HANDLE hdl = (HANDLE) _get_osfhandle(fd);
DWORD low = 1, high = 0;
DWORD low = 0xFFFFFFFF, high = 0xFFFFFFFF;
OVERLAPPED offset =
{0, 0, 0, 0, NULL};
DWORD err;
Expand Down
24 changes: 24 additions & 0 deletions ext/standard/tests/file/bug81223.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Bug #81223 (flock() only locks first byte of file)
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
?>
--FILE--
<?php
$filename = __FILE__;
$stream1 = fopen($filename, "r");
var_dump(flock($stream1, LOCK_EX));
$stream2 = fopen($filename, "r");
var_dump(fread($stream2, 5));
fseek($stream2, 1);
var_dump(fread($stream2, 4));
?>
--EXPECTF--
bool(true)

Notice: fread(): read of %d bytes failed with errno=13 Permission denied in %s on line %d
bool(false)

Notice: fread(): read of %d bytes failed with errno=13 Permission denied in %s on line %d
bool(false)

1 comment on commit 520c00a

@divinity76
Copy link
Contributor

@divinity76 divinity76 commented on 520c00a Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use (~0) instead of 0xFF because (~0) will set every bit, then you don't have to worry about the size of DWORD or how many F's you need ^^

Please sign in to comment.