From 6d8d2422eafb4d75918f7ddb69e57e2478869bb8 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Tue, 30 Oct 2018 02:39:11 -0400 Subject: Lua update strftime.c from dietlibc source Adds %F -- %Y-%m-%d Fixes possible buffer overflow when writing final \0 Frees a bit of code on NON-RTC targets Change-Id: I1c2600a68ee88c6c99f411ae6646861578683f90 --- apps/plugins/lua/strftime.c | 74 +++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 36 deletions(-) (limited to 'apps/plugins') diff --git a/apps/plugins/lua/strftime.c b/apps/plugins/lua/strftime.c index 681d4be1cf..c6152bf492 100644 --- a/apps/plugins/lua/strftime.c +++ b/apps/plugins/lua/strftime.c @@ -45,16 +45,17 @@ size_t strftime ( char* dst, size_t max, const char* format, const struct tm* t else again: switch (*format) { -// case '%': *p++ = '%'; break; // reduce size of jump table - case 'n': *p++ = '\n'; break; - case 't': *p++ = '\t'; break; +// case '%': *p++ = '%'; break; // reduce size of jump table + case 'n': *p++ = '\n'; break; + case 't': *p++ = '\t'; break; case 'O': case 'E': ++format; goto again; - case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf; - case 'r': src = "%I:%M:%S %p"; goto _strf; - case 'R': src = "%H:%M"; goto _strf; - case 'x': src = "%b %a %d"; goto _strf; - case 'X': src = "%k:%M:%S"; goto _strf; - case 'D': src = "%m/%d/%y"; goto _strf; + case 'c': src = "%b %a %d %k:%M:%S %Z %Y"; goto _strf; + case 'r': src = "%I:%M:%S %p"; goto _strf; + case 'R': src = "%H:%M"; goto _strf; + case 'x': src = "%b %a %d"; goto _strf; + case 'X': src = "%k:%M:%S"; goto _strf; + case 'D': src = "%m/%d/%y"; goto _strf; + case 'F': src = "%Y-%m-%d"; goto _strf; case 'T': src = "%H:%M:%S"; _strf: p += strftime (p, (size_t)(dst+max-p), src, tm); break; case 'a': src = sweekdays [tm->tm_wday]; goto _str; @@ -64,37 +65,35 @@ again: case 'B': src = months [tm->tm_mon]; goto _str; case 'p': src = ampm [tm->tm_hour > 12 ? 3 : 2]; goto _str; case 'P': src = ampm [tm->tm_hour > 12 ? 1 : 0]; goto _str; - case 'C': no = tm->tm_year/100 + 19; goto _no; - case 'd': no = tm->tm_mday; goto _no; - case 'e': no = tm->tm_mday; goto _nos; - case 'H': no = tm->tm_hour; goto _no; - case 'I': no = tm->tm_hour % 12; goto _no; - case 'j': no = tm->tm_yday; goto _no; - case 'k': no = tm->tm_hour; goto _nos; - case 'l': no = tm->tm_hour % 12; goto _nos; - case 'm': no = tm->tm_mon + 1; goto _no; - case 'M': no = tm->tm_min; goto _no; - case 'S': no = tm->tm_sec; goto _no; - case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no; - case 'w': no = tm->tm_wday; goto _no; + case 'C': no = tm->tm_year/100 + 19; goto _no; + case 'd': no = tm->tm_mday; goto _no; + case 'e': no = tm->tm_mday; goto _nos; + case 'H': no = tm->tm_hour; goto _no; + case 'I': no = tm->tm_hour % 12; goto _no; + case 'j': no = tm->tm_yday; goto _no; + case 'k': no = tm->tm_hour; goto _nos; + case 'l': no = tm->tm_hour % 12; goto _nos; + case 'm': no = tm->tm_mon + 1; goto _no; + case 'M': no = tm->tm_min; goto _no; + case 'S': no = tm->tm_sec; goto _no; + case 'u': no = tm->tm_wday ? tm->tm_wday : 7; goto _no; + case 'w': no = tm->tm_wday; goto _no; case 'U': no = (tm->tm_yday - tm->tm_wday + 7) / 7; goto _no; case 'W': no = (tm->tm_yday - (tm->tm_wday - 1 + 7) % 7 + 7) / 7; goto _no; case 's': { - time_t t = #if CONFIG_RTC - rb->mktime((struct tm*)tm) -#else - 0 -#endif - ; - char buf[101]; + time_t t = rb->mktime((struct tm*)tm); + char sbuf[101]; char* c; - buf[100]=0; - for (c=buf+99; c>buf; --c) { + sbuf[100]=0; + for (c=sbuf+99; c>sbuf; --c) { *c=(t%10)+'0'; t/=10; if (!t) break; } +#else + char* c = "0"; +#endif src=c; goto _str; } @@ -109,15 +108,15 @@ again: i2a ( buf+2, (unsigned int)(tm->tm_year % 100) ); src = buf; goto _str; - case 'y': no = tm->tm_year % 100; goto _no; - _no: i2a ( buf, no ); /* append number 'no' */ + case 'y': no = tm->tm_year % 100; goto _no; + _no: i2a ( buf, no ); /* append number 'no' */ src = buf; goto _str; - _nos: i2a ( buf, no ); /* the same, but '0'->' ' */ + _nos: i2a ( buf, no ); /* the same, but '0'->' ' */ if (buf[0] == '0') buf[0] = ' '; src = buf; - _str: while (*src && p < dst+max) /* append string */ + _str: while (*src && p < dst+max) /* append string */ *p++ = *src++; break; }; @@ -129,7 +128,10 @@ again: break; } - *p = '\0'; + if ((size_t)(p-dst)>=max) { + if (max) p[-1]=0; + } else + *p = '\0'; return p - dst; } -- cgit v1.2.3