Skip to content

Commit f8178c9

Browse files
committed
Apply fix suggested by Copilot
Signed-off-by: Vitalii Koshura <lestat.de.lionkur@gmail.com>
1 parent dff4dae commit f8178c9

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

client/hostinfo_unix.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,19 +2301,34 @@ long xss_idle() {
23012301

23022302
bool get_idle_time_from_daemon(long &idle_time) {
23032303
static int64_t* seg_ptr = NULL;
2304-
int fd = 0;
23052304
if (!seg_ptr) {
2306-
fd = shm_open("/idle_detect_shmem", O_RDONLY, 0);
2307-
seg_ptr = (int64_t*)mmap(
2305+
int fd = shm_open("/idle_detect_shmem", O_RDONLY, 0);
2306+
if (fd < 0) {
2307+
return false;
2308+
}
2309+
2310+
struct stat st;
2311+
if (fstat(fd, &st)) {
2312+
close(fd);
2313+
return false;
2314+
}
2315+
if (st.st_size < 2*sizeof(int64_t)) {
2316+
close(fd);
2317+
return false;
2318+
}
2319+
2320+
int64_t* mapped_ptr = (int64_t*)mmap(
23082321
NULL, 2*sizeof(int64_t), PROT_READ, MAP_SHARED, fd, 0
23092322
);
2323+
close(fd);
2324+
if (mapped_ptr == MAP_FAILED) {
2325+
return false;
2326+
}
2327+
2328+
seg_ptr = mapped_ptr;
23102329
}
23112330
if (!seg_ptr) return false;
23122331

2313-
struct stat st;
2314-
if (fstat(fd, &st)) return false;
2315-
if (st.st_size < 2*sizeof(int64_t)) return false;
2316-
23172332
// make sure the shmem is actually being updated
23182333
//
23192334
int64_t update_time = seg_ptr[0];

0 commit comments

Comments
 (0)