Skip to content

Commit 414ba6b

Browse files
kkasperczyk-nojukkar
authored andcommitted
include: net: Fix incorrect casting of timeval on zsock_timeval.
Currently zsock_timeval implementation uses long type for both tv_sec and tv_usec fields. In the select method timeval type is directly casted on zsock_timeval, but in some cases (e.g. when using CONFIG_POSIX_API and some specific libc version) both types have fields of different sizes, what may lead to assigning them incorrect values. Using types declared by the used libc in the CONFIG_POSIX_API=y case will let to keep timeval and zsock_timeval types compatible. Signed-off-by: Kamil Kasperczyk <[email protected]>
1 parent 7dc047e commit 414ba6b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

include/net/socket_select.h

+21
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,36 @@
1616

1717
#include <zephyr/types.h>
1818

19+
#ifdef CONFIG_POSIX_API
20+
#ifdef __NEWLIB__
21+
#include <sys/_timeval.h>
22+
#else
23+
#include <sys/types.h>
24+
#endif /* __NEWLIB__ */
25+
#endif /* CONFIG_POSIX_API */
26+
1927
#ifdef __cplusplus
2028
extern "C" {
2129
#endif
2230

31+
#ifdef CONFIG_POSIX_API
32+
/* Rely on the underlying libc definition */
33+
#ifdef __NEWLIB__
34+
#define zsock_timeval timeval
35+
#else
36+
/* workaround for older Newlib 2.x, as it lacks sys/_timeval.h */
37+
struct zsock_timeval {
38+
time_t tv_sec;
39+
suseconds_t tv_usec;
40+
};
41+
#endif /* __NEWLIB__ */
42+
#else
2343
struct zsock_timeval {
2444
/* Using longs, as many (?) implementations seem to use it. */
2545
long tv_sec;
2646
long tv_usec;
2747
};
48+
#endif /* CONFIG_POSIX_API */
2849

2950
typedef struct zsock_fd_set {
3051
uint32_t bitset[(CONFIG_POSIX_MAX_FDS + 31) / 32];

0 commit comments

Comments
 (0)