Whamcloud - gitweb
libe2p: add a thread-safe variant of e2p_feature2string
authorTheodore Ts'o <tytso@mit.edu>
Fri, 20 Mar 2020 21:57:38 +0000 (17:57 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 20 Mar 2020 21:57:38 +0000 (17:57 -0400)
This commit adds the function e2p_feature_to_string() which allows the
caller to pass in a preallocated buffer.

Google-Bug-Id: 16978603
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/e2p/e2p.h
lib/e2p/feature.c

index c3a6b25..ae7dd74 100644 (file)
@@ -50,6 +50,8 @@ int setversion (int fd, unsigned long version);
 void e2p_list_journal_super(FILE *f, char *journal_sb_buf,
                            int exp_block_size, int flags);
 
+void e2p_feature_to_string(int compat, unsigned int mask, char *buf,
+                           size_t buf_len);
 const char *e2p_feature2string(int compat, unsigned int mask);
 const char *e2p_jrnl_feature2string(int compat, unsigned int mask);
 int e2p_string2feature(char *string, int *compat, unsigned int *mask);
index ae7f7f0..515a0f8 100644 (file)
@@ -135,17 +135,20 @@ static struct feature jrnl_feature_list[] = {
        {       0, 0, 0 },
 };
 
-const char *e2p_feature2string(int compat, unsigned int mask)
+void e2p_feature_to_string(int compat, unsigned int mask, char *buf,
+                           size_t buf_len)
 {
        struct feature  *f;
-       static char buf[20];
        char    fchar;
        int     fnum;
 
        for (f = feature_list; f->string; f++) {
                if ((compat == f->compat) &&
-                   (mask == f->mask))
-                       return f->string;
+                   (mask == f->mask)) {
+                       strncpy(buf, f->string, buf_len);
+                       buf[buf_len - 1] = 0;
+                       return;
+               }
        }
        switch (compat) {
        case  E2P_FEATURE_COMPAT:
@@ -163,6 +166,13 @@ const char *e2p_feature2string(int compat, unsigned int mask)
        }
        for (fnum = 0; mask >>= 1; fnum++);
        sprintf(buf, "FEATURE_%c%d", fchar, fnum);
+}
+
+const char *e2p_feature2string(int compat, unsigned int mask)
+{
+       static char buf[20];
+
+       e2p_feature_to_string(compat, mask, buf, sizeof(buf) / sizeof(buf[0]));
        return buf;
 }