Skip to content

Commit 4c1c946

Browse files
authored
Merge pull request #5068 from liukangcc/time
[update] enable asctime_r function parameter legal.
2 parents b7903b2 + a74b0ae commit 4c1c946

File tree

1 file changed

+24
-1
lines changed
  • components/libc/compilers/common

1 file changed

+24
-1
lines changed

components/libc/compilers/common/time.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <rtdevice.h>
3030
#endif
3131

32-
#define DBG_TAG "TIME"
32+
#define DBG_TAG "time"
3333
#define DBG_LVL DBG_INFO
3434
#include <rtdbg.h>
3535

@@ -239,6 +239,29 @@ RTM_EXPORT(mktime);
239239

240240
char* asctime_r(const struct tm *t, char *buf)
241241
{
242+
/* Checking input validity */
243+
if (rt_strlen(days) <= (t->tm_wday << 2) || rt_strlen(months) <= (t->tm_mon << 2))
244+
{
245+
LOG_W("asctime_r: the input parameters exceeded the limit, please check it.");
246+
*(int*) buf = *(int*) days;
247+
*(int*) (buf + 4) = *(int*) months;
248+
num2str(buf + 8, t->tm_mday);
249+
if (buf[8] == '0')
250+
buf[8] = ' ';
251+
buf[10] = ' ';
252+
num2str(buf + 11, t->tm_hour);
253+
buf[13] = ':';
254+
num2str(buf + 14, t->tm_min);
255+
buf[16] = ':';
256+
num2str(buf + 17, t->tm_sec);
257+
buf[19] = ' ';
258+
num2str(buf + 20, 2000 / 100);
259+
num2str(buf + 22, 2000 % 100);
260+
buf[24] = '\n';
261+
buf[25] = '\0';
262+
return buf;
263+
}
264+
242265
/* "Wed Jun 30 21:49:08 1993\n" */
243266
*(int*) buf = *(int*) (days + (t->tm_wday << 2));
244267
*(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));

0 commit comments

Comments
 (0)