From a23bb679852f5c76a81052a3ed4838f4e41ee14d Mon Sep 17 00:00:00 2001 From: sw1tchbl4d3 Date: Fri, 16 Jul 2021 00:35:02 +0200 Subject: [PATCH] Null-terminate read data --- sessions.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sessions.h b/sessions.h index 357ae4f..28108f1 100644 --- a/sessions.h +++ b/sessions.h @@ -18,6 +18,10 @@ int getpstartts(int pid, unsigned long long* startts) { err(1, "Could not open %s", path); int bytes_read = read(fd, fc, sizeof(fc)); + if (bytes_read < 0) + err(1, "Could not read %s", path); + fc[bytes_read] = '\0'; + close(fd); if (memchr(ptr, '\0', bytes_read) != NULL) @@ -123,8 +127,10 @@ int getsession(int pid, unsigned int ts_ttl) { err(1, "Could not open %s", path); } - if (read(fd, ts_str, sizeof(ts_str)) < 0) + int bytes_read = read(fd, ts_str, sizeof(ts_str)); + if (bytes_read < 0) err(1, "Could not read %s", path); + ts_str[bytes_read] = '\0'; startts = strtoull(ts_str, NULL, 10); current = time(NULL);