51 lines
1015 B
Diff
51 lines
1015 B
Diff
From 03be76d6bd223e39e89976a9f75e1e9d19c30a18 Mon Sep 17 00:00:00 2001
|
|
From: Andi Kleen <andi@firstfloor.org>
|
|
Date: Sat, 2 Oct 2021 16:45:20 -0700
|
|
Subject: [PATCH] mcelog: Handle sysfs files without length
|
|
|
|
---
|
|
sysfs.c | 12 ++++--------
|
|
1 file changed, 4 insertions(+), 8 deletions(-)
|
|
|
|
diff --git a/sysfs.c b/sysfs.c
|
|
index bc61b4a..7e7166e 100644
|
|
--- a/sysfs.c
|
|
+++ b/sysfs.c
|
|
@@ -33,29 +33,25 @@ char *read_field(char *base, char *name)
|
|
int n, fd;
|
|
struct stat st;
|
|
char *s;
|
|
- char *buf = NULL;
|
|
+ char *buf = xalloc(4096);
|
|
|
|
xasprintf(&fn, "%s/%s", base, name);
|
|
fd = open(fn, O_RDONLY);
|
|
free(fn);
|
|
if (fd < 0)
|
|
goto bad;
|
|
- if (fstat(fd, &st) < 0) {
|
|
- close(fd);
|
|
- goto bad;
|
|
- }
|
|
- buf = xalloc(st.st_size);
|
|
- n = read(fd, buf, st.st_size);
|
|
+ n = read(fd, buf, 4096);
|
|
close(fd);
|
|
if (n < 0)
|
|
goto bad_buf;
|
|
val = xalloc(n + 1);
|
|
memcpy(val, buf, n);
|
|
+ val[n] = 0;
|
|
free(buf);
|
|
s = memchr(val, '\n', n);
|
|
if (s)
|
|
*s = 0;
|
|
- return val;
|
|
+ return val;
|
|
|
|
bad_buf:
|
|
free(buf);
|
|
--
|
|
2.25.1
|
|
|