use gmtime_r() instead of gmtime()

This avoids pulling in gmtime's static buffer:

function                                             old     new   delta
svlogd_main                                         1401    1412     +11
send_headers                                         668     678     +10
gmtime                                                21       -     -21
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 2/0 up/down: 21/-21)              Total: 0 bytes
   text	   data	    bss	    dec	    hex	filename
 920221	    555	   5804	 926580	  e2374	busybox_old
 920221	    555	   5740	 926516	  e2334	busybox_unstripped
                   ^^^^

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-03-06 18:11:47 +01:00
parent 33f7c8f200
commit e1b1b79269
3 changed files with 8 additions and 3 deletions

View File

@ -1046,6 +1046,7 @@ static void send_headers(int responseNum)
/* Fixed size 29-byte string. Example: Sun, 06 Nov 1994 08:49:37 GMT */ /* Fixed size 29-byte string. Example: Sun, 06 Nov 1994 08:49:37 GMT */
char date_str[40]; /* using a bit larger buffer to paranoia reasons */ char date_str[40]; /* using a bit larger buffer to paranoia reasons */
struct tm tm;
const char *responseString = ""; const char *responseString = "";
const char *infoString = NULL; const char *infoString = NULL;
#if ENABLE_FEATURE_HTTPD_ERROR_PAGES #if ENABLE_FEATURE_HTTPD_ERROR_PAGES
@ -1074,7 +1075,8 @@ static void send_headers(int responseNum)
* always fit into those kbytes. * always fit into those kbytes.
*/ */
strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime(&timer)); strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&timer, &tm));
/* ^^^ using gmtime_r() instead of gmtime() to not use static data */
len = sprintf(iobuf, len = sprintf(iobuf,
"HTTP/1.0 %d %s\r\n" "HTTP/1.0 %d %s\r\n"
"Content-type: %s\r\n" "Content-type: %s\r\n"
@ -1128,7 +1130,7 @@ static void send_headers(int responseNum)
#endif #endif
if (file_size != -1) { /* file */ if (file_size != -1) { /* file */
strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime(&last_mod)); strftime(date_str, sizeof(date_str), RFC1123FMT, gmtime_r(&last_mod, &tm));
#if ENABLE_FEATURE_HTTPD_RANGES #if ENABLE_FEATURE_HTTPD_RANGES
if (responseNum == HTTP_PARTIAL_CONTENT) { if (responseNum == HTTP_PARTIAL_CONTENT) {
len += sprintf(iobuf + len, len += sprintf(iobuf + len,

View File

@ -347,11 +347,13 @@ static unsigned pmatch(const char *p, const char *s, unsigned len)
/* NUL terminated */ /* NUL terminated */
static void fmt_time_human_30nul(char *s, char dt_delim) static void fmt_time_human_30nul(char *s, char dt_delim)
{ {
struct tm tm;
struct tm *ptm; struct tm *ptm;
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
ptm = gmtime(&tv.tv_sec); ptm = gmtime_r(&tv.tv_sec, &tm);
/* ^^^ using gmtime_r() instead of gmtime() to not use static data */
sprintf(s, "%04u-%02u-%02u%c%02u:%02u:%02u.%06u000", sprintf(s, "%04u-%02u-%02u%c%02u:%02u:%02u.%06u000",
(unsigned)(1900 + ptm->tm_year), (unsigned)(1900 + ptm->tm_year),
(unsigned)(ptm->tm_mon + 1), (unsigned)(ptm->tm_mon + 1),

View File

@ -53,6 +53,7 @@ typedef struct _code {
static const CODE *const bb_prioritynames = prioritynames; static const CODE *const bb_prioritynames = prioritynames;
static const CODE *const bb_facilitynames = facilitynames; static const CODE *const bb_facilitynames = facilitynames;
#if ENABLE_SYSLOGD #if ENABLE_SYSLOGD
#include "syslogd.c" #include "syslogd.c"
#endif #endif