Commit d791eaf
committed
Fix localtime/1 memory leak on newlib/picolibc platforms
Both newlib and picolibc leak the old "NAME=value" string when setenv
overwrites an environment variable with a longer value. On ESP32 this
causes unbounded heap growth when localtime/1 is called repeatedly with
a timezone argument.
Fix by replacing setenv/unsetenv with a static putenv buffer that is
installed once and modified in place, avoiding all further allocations.
The workaround is guarded by __NEWLIB__/__PICOLIBC__ so platforms with
a non-leaking libc (glibc, musl) continue to use standard setenv/unsetenv.
Additional fixes:
- Replace space-padding with zero-fill to avoid TZ parser issues
- Reject oversized TZ values instead of silently truncating
- Restore "UTC0" instead of empty string when TZ was originally unset,
giving well-defined UTC behavior on newlib/picolibc
- Check strdup for OOM
- Check putenv return value
- Check localtime_r for NULL
See: espressif/esp-idf#3046
See: espressif/esp-idf#9764
Signed-off-by: Peter M <petermm@gmail.com>1 parent cef8c1a commit d791eaf
1 file changed
Lines changed: 84 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1939 | 1939 | | |
1940 | 1940 | | |
1941 | 1941 | | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
1942 | 1991 | | |
1943 | 1992 | | |
1944 | 1993 | | |
| |||
1962 | 2011 | | |
1963 | 2012 | | |
1964 | 2013 | | |
1965 | | - | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
1966 | 2030 | | |
| 2031 | + | |
1967 | 2032 | | |
1968 | 2033 | | |
| 2034 | + | |
1969 | 2035 | | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
1970 | 2039 | | |
| 2040 | + | |
| 2041 | + | |
1971 | 2042 | | |
| 2043 | + | |
| 2044 | + | |
| 2045 | + | |
| 2046 | + | |
| 2047 | + | |
1972 | 2048 | | |
| 2049 | + | |
1973 | 2050 | | |
| 2051 | + | |
1974 | 2052 | | |
1975 | | - | |
1976 | 2053 | | |
1977 | 2054 | | |
1978 | 2055 | | |
| |||
1981 | 2058 | | |
1982 | 2059 | | |
1983 | 2060 | | |
| 2061 | + | |
| 2062 | + | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
1984 | 2066 | | |
1985 | 2067 | | |
1986 | 2068 | | |
| |||
0 commit comments