Skip to content

Commit e9b46e2

Browse files
keith-packardtpambor
authored andcommitted
lib/libc: Provide optional check for time_t size
The LIBC_ALLOW_LESS_THAN_64BIT_TIME option disables a compile-time check ensuring that time_t can hold 64-bit values. This check can prevent accidental builds on targets with 32-bit time_t and the consequent 2038 issues. This option is enabled when using the native C library as i386 glibc uses 32-bit time_t. A new file, validate_libc.c, is added to lib/libc to contain this and any future libc tests. The check for CONFIG_EXTERNAL_LIBC is moved to libc/CMakeLists.txt to ensure that this new file is always compiled. Signed-off-by: Keith Packard <[email protected]>
1 parent d5e9a02 commit e9b46e2

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

lib/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
add_compile_options($<TARGET_PROPERTY:compiler,warning_shadow_variables>)
55

66
add_subdirectory(crc)
7-
if(NOT CONFIG_EXTERNAL_LIBC)
87
add_subdirectory(libc)
9-
endif()
108
if(NOT CONFIG_NATIVE_LIBC)
119
add_subdirectory(posix)
1210
endif()

lib/libc/CMakeLists.txt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
zephyr_syscall_header(
4-
${ZEPHYR_BASE}/include/zephyr/sys/libc-hooks.h
5-
)
6-
7-
add_subdirectory_ifdef(CONFIG_ARCMWDT_LIBC arcmwdt)
8-
add_subdirectory_ifdef(CONFIG_ARMCLANG_STD_LIBC armstdc)
9-
add_subdirectory_ifdef(CONFIG_IAR_LIBC iar)
10-
add_subdirectory_ifdef(CONFIG_MINIMAL_LIBC minimal)
11-
add_subdirectory_ifdef(CONFIG_NEWLIB_LIBC newlib)
12-
add_subdirectory_ifdef(CONFIG_PICOLIBC picolibc)
13-
14-
add_subdirectory(common)
3+
if(NOT CONFIG_EXTERNAL_LIBC)
4+
zephyr_syscall_header(
5+
${ZEPHYR_BASE}/include/zephyr/sys/libc-hooks.h
6+
)
7+
8+
add_subdirectory_ifdef(CONFIG_ARCMWDT_LIBC arcmwdt)
9+
add_subdirectory_ifdef(CONFIG_ARMCLANG_STD_LIBC armstdc)
10+
add_subdirectory_ifdef(CONFIG_IAR_LIBC iar)
11+
add_subdirectory_ifdef(CONFIG_MINIMAL_LIBC minimal)
12+
add_subdirectory_ifdef(CONFIG_NEWLIB_LIBC newlib)
13+
add_subdirectory_ifdef(CONFIG_PICOLIBC picolibc)
14+
15+
add_subdirectory(common)
16+
endif()
17+
18+
zephyr_sources(
19+
validate_libc.c
20+
)

lib/libc/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ config REQUIRES_FLOAT_PRINTF
1818
Select a printf implementation that provides a complete
1919
implementation including floating point support.
2020

21+
config LIBC_ALLOW_LESS_THAN_64BIT_TIME
22+
bool "Don't require that time_t be at least 64-bits"
23+
default y if NATIVE_LIBC
24+
help
25+
When selected, time_t will not be required to be at least 64
26+
bits. Background: To avoid Y2038 issues time_t needs to be
27+
bigger than 32bits. But some C libraries do not provide or
28+
default to a type for time_t which is at least
29+
64bits. Enable this option if your C library defined time_t
30+
is more than 32bits (e.g. 48 bits) or are sure your project
31+
is not subject to Y2038 issues.
32+
2133
config FULL_LIBC_SUPPORTED
2234
bool
2335
help

lib/libc/validate_libc.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright © 2025 Keith Packard <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/toolchain.h>
9+
10+
/* Validate various C library implementation characteristics */
11+
12+
#ifndef CONFIG_LIBC_ALLOW_LESS_THAN_64BIT_TIME
13+
#include <time.h>
14+
/*
15+
* Ensure that time_t can hold at least 64 bit values.
16+
*/
17+
BUILD_ASSERT(sizeof(time_t) >= 8, "time_t cannot hold 64-bit values");
18+
#endif

0 commit comments

Comments
 (0)