Skip to content
Myr

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When we use a large format string to `strftime/1` and `strflocaltime/1`, we get segmentation fault. This happens because the result buffer is allocated on the stack using `alloca` function, and `strftime` function writes out of the space when the size is not enough to format time. I fixed the segmentation fault issue by allocating the result buffer on the heap using `malloc` function, because `alloca` function does not provide a way to detect insufficient space.

authored by itchyny · committed by GitHub · parent dc849e9 · Browse files · 1 file+18 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+18 added−25 removed

  • other:+18 added−25 removed

1 file+18 added−25 removed

modified:src/builtin.c+18 added-25 removed
9 unmodified lines
10#endif11#include <sys/time.h>12#include <stdlib.h>13#include <stddef.h>14#ifdef HAVE_ALLOCA_H15# include <alloca.h>16#elif !defined alloca17# ifdef __GNUC__18# define alloca __builtin_alloca19# elif defined _MSC_VER20# include <malloc.h>21# define alloca _alloca22# elif !defined HAVE_ALLOCA23# ifdef __cplusplus24extern "C"25# endif26void *alloca (size_t);27# endif28#endif14#include <assert.h>15#include <ctype.h>16#include <limits.h>17#include <math.h>
1734 unmodified lines
1752 if (!jv2tm(a, &tm, 0))1753 return ret_error(b, jv_string("strftime/1 requires parsed datetime inputs"));17541755 const char *fmt = jv_string_value(b);1771 size_t alloced = strlen(fmt) + 100;1772 char *buf = alloca(alloced);1756 size_t max_size = strlen(fmt) + 100;1757 char *buf = jv_mem_alloc(max_size);1758#ifdef __APPLE__1759 /* Apple Libc (as of version 1669.40.2) contains a bug which causes it to1760 * ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the1761 * proper timezone offset we temporarily switch the TZ to UTC. */1762 char *tz = (tz = getenv("TZ")) != NULL ? strdup(tz) : NULL;1763 setenv("TZ", "UTC", 1);1764#endif1780 size_t n = strftime(buf, alloced, fmt, &tm);1765 size_t n = strftime(buf, max_size, fmt, &tm);1766#ifdef __APPLE__1767 if (tz) {1768 setenv("TZ", tz, 1);1769 free(tz);
2 unmodified lines
1772 }1773#endif1774 jv_free(b);1775 /* POSIX doesn't provide errno values for strftime() failures; weird */1791 if (n == 0 || n > alloced)1776 if ((n == 0 && *fmt) || n > max_size) {1777 free(buf);1778 return jv_invalid_with_msg(jv_string("strftime/1: unknown system failure"));1793 return jv_string(buf);1779 }1780 jv ret = jv_string_sized(buf, n);1781 free(buf);1782 return ret;1783}1784#else1785static jv f_strftime(jq_state *jq, jv a, jv b) {1786 jv_free(a);
14 unmodified lines
1801 struct tm tm;1802 if (!jv2tm(a, &tm, 1))1803 return ret_error(b, jv_string("strflocaltime/1 requires parsed datetime inputs"));1804 const char *fmt = jv_string_value(b);1816 size_t alloced = strlen(fmt) + 100;1817 char *buf = alloca(alloced);1818 size_t n = strftime(buf, alloced, fmt, &tm);1805 size_t max_size = strlen(fmt) + 100;1806 char *buf = jv_mem_alloc(max_size);1807 size_t n = strftime(buf, max_size, fmt, &tm);1808 jv_free(b);1809 /* POSIX doesn't provide errno values for strftime() failures; weird */1821 if (n == 0 || n > alloced)1810 if ((n == 0 && *fmt) || n > max_size) {1811 free(buf);1812 return jv_invalid_with_msg(jv_string("strflocaltime/1: unknown system failure"));1823 return jv_string(buf);1813 }1814 jv ret = jv_string_sized(buf, n);1815 free(buf);1816 return ret;1817}1818#else1819static jv f_strflocaltime(jq_state *jq, jv a, jv b) {1820 jv_free(a);
237 unmodified lines