[PATCH v4 11/12] EDAC/ghes: Small buffer fixes for mem other_detail helpers
From: Pierre Gondois
Date: Fri Nov 13 2026 - 17:10:00 EST
scnprintf() already reserves space for the terminating NULL,
leaving one byte unused and under-flowing the capacity when len=0.
Callers also remove the last character unconditionally, which
can discard valid text after truncation.
Update the following functions to fix the above issues.
- print_mem_error_other_detail()
- print_mem_error2_other_detail()
Signed-off-by: Pierre Gondois <pierre.gondois@xxxxxxx>
---
drivers/edac/ghes_edac.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/drivers/edac/ghes_edac.c b/drivers/edac/ghes_edac.c
index f0ca61bddee6..33e3aa897c18 100644
--- a/drivers/edac/ghes_edac.c
+++ b/drivers/edac/ghes_edac.c
@@ -243,15 +243,14 @@ static void ghes_scan_system(void)
}
static int print_mem_error_other_detail(const struct cper_sec_mem_err *mem, char *msg,
- const char *location, unsigned int len)
+ const char *location, size_t len)
{
u32 n;
- if (!msg)
+ if (!msg || !len)
return 0;
n = 0;
- len -= 1;
n += scnprintf(msg + n, len - n, "APEI location: %s ", location);
@@ -377,8 +376,8 @@ static int ghes_edac_report_mem_error(struct notifier_block *nb,
/* All other fields are mapped on e->other_detail */
p = pvt->other_detail;
- p += print_mem_error_other_detail(mem_err, p, e->location, OTHER_DETAIL_LEN);
- if (p > pvt->other_detail)
+ p += print_mem_error_other_detail(mem_err, p, e->location, sizeof(pvt->other_detail));
+ if (p > pvt->other_detail && p[-1] == ' ')
*(p - 1) = '\0';
edac_raw_mc_handle_error(e);
@@ -392,15 +391,14 @@ static struct notifier_block ghes_edac_mem_err_nb = {
};
static int print_mem_error2_other_detail(const struct cper_sec_mem_err2 *mem, char *msg,
- const char *location, unsigned int len)
+ const char *location, size_t len)
{
u32 n;
- if (!msg)
+ if (!msg || !len)
return 0;
n = 0;
- len -= 1;
n += scnprintf(msg + n, len - n, "APEI location: %s ", location);
@@ -494,8 +492,8 @@ static int ghes_edac_report_mem_error2(struct notifier_block *nb,
/* All other fields are mapped on e->other_detail */
p = pvt->other_detail;
- p += print_mem_error2_other_detail(mem_err, p, e->location, OTHER_DETAIL_LEN);
- if (p > pvt->other_detail)
+ p += print_mem_error2_other_detail(mem_err, p, e->location, sizeof(pvt->other_detail));
+ if (p > pvt->other_detail && p[-1] == ' ')
*(p - 1) = '\0';
edac_raw_mc_handle_error(e);
--
2.43.0