Skip to content

Commit f450da7

Browse files
author
Зишан Мирза
committed
fix: timezone char length
1 parent a6832a6 commit f450da7

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

libc/src/time/time_utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ void release_file(FILE *fp) {
3434
fclose(fp);
3535
}
3636

37-
void acquire_file(FILE *fp, char *timezone) {
37+
void acquire_file(FILE *fp, char *timezone, size_t timezone_size) {
3838
while (1) {
3939
if (file_usage == 0) {
4040
file_usage = 1;
4141
break;
4242
}
4343
}
4444

45-
if (fgets(timezone, sizeof(timezone), fp) == NULL) {
45+
if (fgets(timezone, (int)timezone_size, fp) == NULL) {
4646
release_file(fp);
4747
}
4848
}
@@ -150,15 +150,15 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
150150
if (years > INT_MAX || years < INT_MIN)
151151
return time_utils::out_of_range();
152152

153-
char timezone[128];
153+
char timezone[TimeConstants::TIMEZONE_SIZE];
154154

155155
FILE *fp = NULL;
156156
fp = fopen("/etc/timezone", "rb");
157157
if (fp == NULL) {
158158
// TODO: implement getting timezone from `TZ` environment variable and
159159
// storing the value in `timezone`
160160
} else {
161-
acquire_file(fp, timezone);
161+
acquire_file(fp, timezone, TimeConstants::TIMEZONE_SIZE);
162162
}
163163

164164
if (file_usage == 0) {

libc/src/time/time_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct TimeConstants {
8181
static constexpr int END_OF32_BIT_EPOCH_YEAR = 2038;
8282

8383
static constexpr time_t OUT_OF_RANGE_RETURN_VALUE = -1;
84+
85+
static constexpr size_t TIMEZONE_SIZE = 128;
8486
};
8587

8688
// Update the "tm" structure's year, month, etc. members from seconds.

0 commit comments

Comments
 (0)