Skip to content

Commit

Permalink
Avoid undefined behavior in Windows ftok(3) emulation
Browse files Browse the repository at this point in the history
`.nFileIndexHigh` is a unsigned 32bit number.  Casting that to `__int64`
and shifting left by 32bits triggers undefined behavior if the most
significant bit of `.nFileIndexHigh` is set.  We could avoid that by
casting to `(__uint64)`, but in that case the whole clause doesn't have
an effect anymore, so we drop it altogether.

Closes GH-9958.
  • Loading branch information
cmb69 committed Nov 18, 2022
1 parent 8d65c2f commit 2d94ee5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -8,6 +8,7 @@ PHP NEWS
. Fixed bug GH-9918 (License information for xxHash is not included in
README.REDIST.BINS file). (Akama Hitoshi)
. Fixed bug GH-9650 (Can't initialize heap: [0x000001e7]). (Michael Voříšek)
. Fixed potentially undefined behavior in Windows ftok(3) emulation. (cmb)

- MBString:
. Fixed bug GH-9535 (The behavior of mb_strcut in mbstring has been changed in
Expand Down
2 changes: 1 addition & 1 deletion win32/ftok.c
Expand Up @@ -51,7 +51,7 @@ ftok(const char *pathname, int proj_id)
return (key_t)-1;
}

ret = (key_t) ((proj_id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | ((bhfi.nFileIndexLow | (__int64)bhfi.nFileIndexHigh << 32) & 0xffff));
ret = (key_t) ((proj_id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | (bhfi.nFileIndexLow & 0xffff));

CloseHandle(fh);
PHP_WIN32_IOUTIL_CLEANUP_W()
Expand Down

0 comments on commit 2d94ee5

Please sign in to comment.