Skip to content

Commit a96b739

Browse files
committed
Remove the static buffer in printDate
1 parent bbb0e41 commit a96b739

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ Or just `./run.sh &` which is recommended.
376376
- Add periodic server health reporting
377377
- Add IPC and TCP support
378378
- Add json support
379+
- PID reuse detection (validate process start time to prevent false positives when PIDs are recycled)
379380

380381
## :snowman: Author
381382
Eray Öztürk ([@diffstorm](https://github.com/diffstorm))

src/stats.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,20 +407,21 @@ void stats_update_memory_usage(int index, int pid)
407407
}
408408
}
409409

410-
static char *printDate(const time_t *t)
411-
{
412-
static char ts[22]; // Fixme : solve without internal static buffer
410+
#define DATE_BUFFER_SIZE 22
413411

412+
static char *printDate(const time_t *t, char *buf, size_t buf_size)
413+
{
414414
if((*t) > 0)
415415
{
416-
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", localtime(t));
416+
strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", localtime(t));
417417
}
418418
else
419419
{
420-
strcpy(ts, "Never");
420+
strncpy(buf, "Never", buf_size - 1);
421+
buf[buf_size - 1] = '\0';
421422
}
422423

423-
return ts;
424+
return buf;
424425
}
425426

426427
void stats_print_to_file(int index, const char *app_name)
@@ -435,10 +436,11 @@ void stats_print_to_file(int index, const char *app_name)
435436
return;
436437
}
437438

439+
char date_buf[DATE_BUFFER_SIZE];
438440
fprintf(fp, "Statistics for App %d %s:\n", index, app_name);
439-
fprintf(fp, "Started at: %s\n", printDate(&stats[index].started_at));
440-
fprintf(fp, "Crashed at: %s\n", printDate(&stats[index].crashed_at));
441-
fprintf(fp, "Heartbeat reset at: %s\n", printDate(&stats[index].heartbeat_reset_at));
441+
fprintf(fp, "Started at: %s\n", printDate(&stats[index].started_at, date_buf, sizeof(date_buf)));
442+
fprintf(fp, "Crashed at: %s\n", printDate(&stats[index].crashed_at, date_buf, sizeof(date_buf)));
443+
fprintf(fp, "Heartbeat reset at: %s\n", printDate(&stats[index].heartbeat_reset_at, date_buf, sizeof(date_buf)));
442444
fprintf(fp, "Start count: %zu\n", stats[index].start_count);
443445
fprintf(fp, "Crash count: %zu\n", stats[index].crash_count);
444446
fprintf(fp, "Heartbeat reset count: %zu\n", stats[index].heartbeat_reset_count);

0 commit comments

Comments
 (0)