]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: optimize mnt_optstr_apply_flags()
authorKarel Zak <kzak@redhat.com>
Tue, 29 Sep 2020 10:31:06 +0000 (12:31 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 29 Sep 2020 10:31:06 +0000 (12:31 +0200)
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 <kzak@redhat.com>
libmount/src/optstr.c

index 829b2849f29710a13947b605c142d1b69217ac5a..9af594f520dbd5eec92f4e57f3a16263e09bdaeb 100644 (file)
@@ -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));