Skip to content

Commit 12ea06c

Browse files
Christopher Friedtcfriedt
authored andcommitted
posix: fdtable: ensure stdin, stdout, and stderr are initialized
Ensure that stdin, stdout, and stderr are initialized statically. Previously, the mutex and condition variable were uninitialized. Signed-off-by: Christopher Friedt <[email protected]>
1 parent d912be1 commit 12ea06c

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

lib/os/fdtable.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
#include <errno.h>
17+
#include <string.h>
18+
1719
#include <zephyr/posix/fcntl.h>
1820
#include <zephyr/kernel.h>
1921
#include <zephyr/sys/fdtable.h>
@@ -41,17 +43,23 @@ static struct fd_entry fdtable[CONFIG_POSIX_MAX_FDS] = {
4143
{
4244
/* STDIN */
4345
.vtable = &stdinout_fd_op_vtable,
44-
.refcount = ATOMIC_INIT(1)
46+
.refcount = ATOMIC_INIT(1),
47+
.lock = Z_MUTEX_INITIALIZER(fdtable[0].lock),
48+
.cond = Z_CONDVAR_INITIALIZER(fdtable[0].cond),
4549
},
4650
{
4751
/* STDOUT */
4852
.vtable = &stdinout_fd_op_vtable,
49-
.refcount = ATOMIC_INIT(1)
53+
.refcount = ATOMIC_INIT(1),
54+
.lock = Z_MUTEX_INITIALIZER(fdtable[1].lock),
55+
.cond = Z_CONDVAR_INITIALIZER(fdtable[1].cond),
5056
},
5157
{
5258
/* STDERR */
5359
.vtable = &stdinout_fd_op_vtable,
54-
.refcount = ATOMIC_INIT(1)
60+
.refcount = ATOMIC_INIT(1),
61+
.lock = Z_MUTEX_INITIALIZER(fdtable[2].lock),
62+
.cond = Z_CONDVAR_INITIALIZER(fdtable[2].cond),
5563
},
5664
#else
5765
{
@@ -124,6 +132,30 @@ static int _check_fd(int fd)
124132
return 0;
125133
}
126134

135+
#ifdef CONFIG_ZTEST
136+
bool fdtable_fd_is_initialized(int fd)
137+
{
138+
struct k_mutex ref_lock;
139+
struct k_condvar ref_cond;
140+
141+
if (fd < 0 || fd >= ARRAY_SIZE(fdtable)) {
142+
return false;
143+
}
144+
145+
ref_lock = (struct k_mutex)Z_MUTEX_INITIALIZER(fdtable[fd].lock);
146+
if (memcmp(&ref_lock, &fdtable[fd].lock, sizeof(ref_lock)) != 0) {
147+
return false;
148+
}
149+
150+
ref_cond = (struct k_condvar)Z_CONDVAR_INITIALIZER(fdtable[fd].cond);
151+
if (memcmp(&ref_cond, &fdtable[fd].cond, sizeof(ref_cond)) != 0) {
152+
return false;
153+
}
154+
155+
return true;
156+
}
157+
#endif /* CONFIG_ZTEST */
158+
127159
void *z_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err)
128160
{
129161
struct fd_entry *entry;

0 commit comments

Comments
 (0)