Message ID | 20210903150539.7282-1-len.baker@gmx.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [v6] EDAC/mc: Prefer strscpy or scnprintf over strcpy | expand |
On 2021-09-03 08:05, Len Baker wrote: > strcpy() performs no bounds checking on the destination buffer. > len.baker@gmx.com/ [] > @@ -1113,12 +1115,9 @@ void edac_mc_handle_error(const enum > hw_event_mc_err_type type, > p = e->label; > *p = '\0'; > } else { > - if (p != e->label) { > - strcpy(p, OTHER_LABEL); > - p += strlen(OTHER_LABEL); > - } > - strcpy(p, dimm->label); > - p += strlen(p); > + n += scnprintf(e->label + n, sizeof(e->label) - n, > + "%s%s", prefix, dimm->label); > + prefix = OTHER_LABEL; OTHER_LABEL is a define specific to this module IMO: Used once text macros are just obfuscating and should be removed.
Hi, On Fri, Sep 03, 2021 at 10:03:18AM -0700, Joe Perches wrote: > On 2021-09-03 08:05, Len Baker wrote: > > strcpy() performs no bounds checking on the destination buffer. > > len.baker@gmx.com/ > > [] > > > @@ -1113,12 +1115,9 @@ void edac_mc_handle_error(const enum > > hw_event_mc_err_type type, > > p = e->label; > > *p = '\0'; > > } else { > > - if (p != e->label) { > > - strcpy(p, OTHER_LABEL); > > - p += strlen(OTHER_LABEL); > > - } > > - strcpy(p, dimm->label); > > - p += strlen(p); > > + n += scnprintf(e->label + n, sizeof(e->label) - n, > > + "%s%s", prefix, dimm->label); > > + prefix = OTHER_LABEL; > > OTHER_LABEL is a define specific to this module > > IMO: Used once text macros are just obfuscating and should be removed. This macro is used in "/include/linux/edac.h" as follows: struct edac_raw_error_desc { [...] char label[(EDAC_MC_LABEL_LEN + 1 + sizeof(OTHER_LABEL)) * EDAC_MAX_LABELS]; [...] }; If we remove this define the size of label would be: char label[(EDAC_MC_LABEL_LEN + 6) * EDAC_MAX_LABELS]; So, I think now is more complicated to understand because the size is what it is. If you prefer this option, I can remove the macro and add a comment with some explanation. Regards, Len
On Sat, Sep 04, 2021 at 01:23:03PM +0200, Len Baker wrote:
> I can remove the macro and add a comment with some explanation.
No, please leave the macro.
Hi, On Sat, Sep 04, 2021 at 01:36:26PM +0200, Borislav Petkov wrote: > On Sat, Sep 04, 2021 at 01:23:03PM +0200, Len Baker wrote: > > I can remove the macro and add a comment with some explanation. > > No, please leave the macro. Ok, so I leave the patch as is. Thanks, Len
diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c index f6d462d0be2d..97dff62970a5 100644 --- a/drivers/edac/edac_mc.c +++ b/drivers/edac/edac_mc.c @@ -1032,6 +1032,8 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, int i, n_labels = 0; struct edac_raw_error_desc *e = &mci->error_desc; bool any_memory = true; + const char *prefix = ""; + int n = 0; edac_dbg(3, "MC%d\n", mci->mc_idx); @@ -1113,12 +1115,9 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, p = e->label; *p = '\0'; } else { - if (p != e->label) { - strcpy(p, OTHER_LABEL); - p += strlen(OTHER_LABEL); - } - strcpy(p, dimm->label); - p += strlen(p); + n += scnprintf(e->label + n, sizeof(e->label) - n, + "%s%s", prefix, dimm->label); + prefix = OTHER_LABEL; } /* @@ -1140,9 +1139,9 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type, } if (any_memory) - strcpy(e->label, "any memory"); + strscpy(e->label, "any memory", sizeof(e->label)); else if (!*e->label) - strcpy(e->label, "unknown memory"); + strscpy(e->label, "unknown memory", sizeof(e->label)); edac_inc_csrow(e, row, chan);