Skip to content

Commit 0fe3a91

Browse files
committed
main/streams: Remove questionable use of PHP_STRLCPY
The description of PHP_STRLCPY says that this is a fast version of strlcpy that should be used if we *know* the size of both the source and destination buffers. This is clearly not the case as we use strlen() to compute it. Moreover if the result cannot fit in the destination buffer something seriously strange has happened and we should return a failure state rather than truncating.
1 parent f55d6cc commit 0fe3a91

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

main/streams/plain_wrapper.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,12 @@ static ssize_t php_plain_files_dirstream_read(php_stream *stream, char *buf, siz
10281028

10291029
result = readdir(dir);
10301030
if (result) {
1031-
PHP_STRLCPY(ent->d_name, result->d_name, sizeof(ent->d_name), strlen(result->d_name));
1031+
size_t len = strlen(result->d_name);
1032+
if (UNEXPECTED(len >= sizeof(ent->d_name))) {
1033+
return -1;
1034+
}
1035+
/* Include null byte */
1036+
memcpy(ent->d_name, result->d_name, len+1);
10321037
#ifdef _DIRENT_HAVE_D_TYPE
10331038
ent->d_type = result->d_type;
10341039
#else

0 commit comments

Comments
 (0)