Skip to content

Commit

Permalink
Merge branch 'PHP-8.4'
Browse files Browse the repository at this point in the history
* PHP-8.4:
  Fix potential OOB read in zend_dirname() on Windows
  • Loading branch information
cmb69 committed Nov 29, 2024
2 parents 1668a16 + ba7dee5 commit fdd3839
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ ZEND_API size_t zend_dirname(char *path, size_t len)
}

/* Strip trailing slashes */
while (end >= path && IS_SLASH_P(end)) {
while (end >= path && IS_SLASH_P_EX(end, end == path)) {
end--;
}
if (end < path) {
Expand All @@ -2207,7 +2207,7 @@ ZEND_API size_t zend_dirname(char *path, size_t len)
}

/* Strip filename */
while (end >= path && !IS_SLASH_P(end)) {
while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
end--;
}
if (end < path) {
Expand All @@ -2218,7 +2218,7 @@ ZEND_API size_t zend_dirname(char *path, size_t len)
}

/* Strip slashes which came before the file name */
while (end >= path && IS_SLASH_P(end)) {
while (end >= path && IS_SLASH_P_EX(end, end == path)) {
end--;
}
if (end < path) {
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_virtual_cwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ typedef unsigned short mode_t;
#define DEFAULT_SLASH '\\'
#define DEFAULT_DIR_SEPARATOR ';'
#define IS_SLASH(c) ((c) == '/' || (c) == '\\')
// IS_SLASH_P() may read the previous char on Windows, which may be OOB; use IS_SLASH_P_EX() instead
#define IS_SLASH_P(c) (*(c) == '/' || \
(*(c) == '\\' && !IsDBCSLeadByte(*(c-1))))
#define IS_SLASH_P_EX(c, first_byte) (*(c) == '/' || \
(*(c) == '\\' && ((first_byte) || !IsDBCSLeadByte(*(c-1)))))

/* COPY_WHEN_ABSOLUTE is 2 under Win32 because by chance both regular absolute paths
in the file system and UNC paths need copying of two characters */
Expand Down Expand Up @@ -110,7 +113,9 @@ typedef unsigned short mode_t;
#endif

#define IS_SLASH(c) ((c) == '/')
// IS_SLASH_P() may read the previous char on Windows, which may be OOB; use IS_SLASH_P_EX() instead
#define IS_SLASH_P(c) (*(c) == '/')
#define IS_SLASH_P_EX(c, first_byte) IS_SLASH_P(c)

#endif

Expand Down

0 comments on commit fdd3839

Please sign in to comment.