arch/common: fix host_flags_to_mode() O_RDONLY sentinel collision

host_flags_to_mode() used a trailing 0 entry in modeflags[] as the
loop-termination sentinel. O_RDONLY is defined as 0 and is exactly
modeflags[1], so the loop's termination check fired before ever
comparing that entry, and a bare O_RDONLY open always fell through
to -EINVAL.

Bound the loop by array size (nitems()) instead of a value sentinel.

Signed-off-by: liang.huang <liang.huang@houmo.ai>
This commit is contained in:
liang.huang 2026-07-15 06:58:25 +08:00 committed by Xiang Xiao
parent 2af63e91b9
commit 335aacbf1a
3 changed files with 6 additions and 6 deletions

View file

@ -31,6 +31,7 @@
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/param.h>
#include <syscall.h>
#include <unistd.h>
@ -94,11 +95,10 @@ static int host_flags_to_mode(int flags)
O_WRONLY | O_CREAT | O_APPEND,
O_RDWR | O_CREAT | O_APPEND | O_TEXT,
O_RDWR | O_CREAT | O_APPEND,
0,
};
int i;
for (i = 0; modeflags[i] != 0; i++)
for (i = 0; i < nitems(modeflags); i++)
{
if ((modemasks & flags) == modeflags[i])
{

View file

@ -31,6 +31,7 @@
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/param.h>
#include <syscall.h>
#include <unistd.h>
@ -94,11 +95,10 @@ static int host_flags_to_mode(int flags)
O_WRONLY | O_CREAT | O_APPEND,
O_RDWR | O_CREAT | O_APPEND | O_TEXT,
O_RDWR | O_CREAT | O_APPEND,
0,
};
int i;
for (i = 0; modeflags[i] != 0; i++)
for (i = 0; i < nitems(modeflags); i++)
{
if ((modemasks & flags) == modeflags[i])
{

View file

@ -31,6 +31,7 @@
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/param.h>
#include <syscall.h>
#include <unistd.h>
@ -94,11 +95,10 @@ static int host_flags_to_mode(int flags)
O_WRONLY | O_CREAT | O_APPEND,
O_RDWR | O_CREAT | O_APPEND | O_TEXT,
O_RDWR | O_CREAT | O_APPEND,
0,
};
int i;
for (i = 0; modeflags[i] != 0; i++)
for (i = 0; i < nitems(modeflags); i++)
{
if ((modemasks & flags) == modeflags[i])
{