Skip to content

Commit 80d66a9

Browse files
committed
Fixing a typo in the record API causing segfaults, replacing localtime with thread-safe variant
1 parent 050d04f commit 80d66a9

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/http_post.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int http_post_send(hal_jpegdata *jpeg) {
2121
{
2222
time_t timer;
2323
time(&timer);
24-
struct tm *tm_info = localtime(&timer);
24+
struct tm tm_buf, *tm_info = localtime_r(&timer, &tm_buf);
2525
size_t time_len = strftime(
2626
time_url, sizeof(time_url), app_config.http_post_url, tm_info);
2727
time_url[time_len++] = 0;

src/record.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void record_start(void) {
5252
recordPath[sizeof(recordPath) - 1] = '\0';
5353
} else {
5454
char tempName[160];
55-
struct tm *tm_info = localtime(&recordStartTime);
55+
struct tm tm_buf, *tm_info = localtime_r(&recordStartTime, &tm_buf);
5656
sprintf(tempName, "recording_%s.mp4", timefmt);
5757
strftime(recordPath, sizeof(recordPath), tempName, tm_info);
5858
}

src/region.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ void region_fill_formatted(char* str) {
8787
ipos++;
8888
char s[64];
8989
time_t t = time(NULL);
90-
struct tm *tm;
90+
struct tm tm_buf, *tm_info;
9191
if (str[ipos + 1] == 'u') {
9292
ipos++;
93-
tm = gmtime(&t);
94-
} else tm = localtime(&t);
95-
strftime(s, 64, timefmt, tm);
93+
tm_info = gmtime(&t);
94+
} else tm_info = localtime_r(&t, &tm_buf);
95+
strftime(s, 64, timefmt, tm_info);
9696
strcat(out, s);
9797
opos += strlen(s);
9898
}

src/server.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,9 +1307,9 @@ void respond_request(http_request_t *req) {
13071307
record_stop();
13081308
}
13091309
}
1310-
struct tm *start = localtime(&recordStartTime);
1310+
struct tm tm_buf, *tm_info = localtime_r(&recordStartTime, &tm_buf);
13111311
char start_time[64];
1312-
strftime(start_time, sizeof(start_time), "%Y-%m-%dT%H:%M:%SZ", start);
1312+
strftime(start_time, sizeof(start_time), "%Y-%m-%dT%H:%M:%SZ", tm_info);
13131313

13141314
respLen = sprintf(response,
13151315
"HTTP/1.1 200 OK\r\n"
@@ -1318,7 +1318,7 @@ void respond_request(http_request_t *req) {
13181318
"\r\n"
13191319
"{\"recording\":%s,\"start_time\":\"%s\",\"continuous\":\"%s\",\"path\":\"%s\","
13201320
"\"filename\":\"%s\",\"segment_duration\":%d,\"segment_size\":%d}",
1321-
recordOn ? "true" : "false", recordStartTime, app_config.record_continuous ? "true" : "false",
1321+
recordOn ? "true" : "false", start_time, app_config.record_continuous ? "true" : "false",
13221322
app_config.record_path, app_config.record_filename,
13231323
app_config.record_segment_duration, app_config.record_segment_size);
13241324
send_and_close(req->clntFd, response, respLen);

0 commit comments

Comments
 (0)