From ab35743a0f42e94a58f3365177ca1e2eab38b3e1 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 29 Sep 2020 12:31:06 +0200 Subject: [PATCH] libmount: optimize mnt_optstr_apply_flags() We append to the options string in loop there. It seems better to use ul_buffer for this case to avoid duplicate strlen() and reallocs. Signed-off-by: Karel Zak --- libmount/src/optstr.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/libmount/src/optstr.c b/libmount/src/optstr.c index 829b2849f2..9af594f520 100644 --- a/libmount/src/optstr.c +++ b/libmount/src/optstr.c @@ -804,9 +804,14 @@ int mnt_optstr_apply_flags(char **optstr, unsigned long flags, /* add missing options (but ignore fl if contains MS_REC only) */ if (fl && fl != MS_REC) { + const struct libmnt_optmap *ent; + struct ul_buffer buf = UL_INIT_BUFFER; + size_t sz; char *p; + ul_buffer_refer_string(&buf, *optstr); + for (ent = map; ent && ent->name; ent++) { if ((ent->mask & MNT_INVERT) || ent->id == 0 @@ -820,17 +825,16 @@ int mnt_optstr_apply_flags(char **optstr, unsigned long flags, p--; /* name[=] */ else continue; /* name= */ - - p = strndup(ent->name, p - ent->name); - if (!p) { - rc = -ENOMEM; - goto err; - } - mnt_optstr_append_option(optstr, p, NULL); - free(p); + sz = p - ent->name; } else - mnt_optstr_append_option(optstr, ent->name, NULL); + sz = strlen(ent->name); + + rc = __buffer_append_option(&buf, ent->name, sz, NULL, 0); + if (rc) + goto err; } + + *optstr = ul_buffer_get_data(&buf); } DBG(CXT, ul_debug("new optstr '%s'", *optstr)); -- 2.47.2