From: Theodore Ts'o Date: Fri, 20 Mar 2020 21:57:38 +0000 (-0400) Subject: libe2p: add a thread-safe variant of e2p_feature2string X-Git-Tag: v1.45.6~13 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=96d5cc74ec9fb1e5feb4bea0871c6dc2010abe8d;p=tools%2Fe2fsprogs.git libe2p: add a thread-safe variant of e2p_feature2string 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 --- diff --git a/lib/e2p/e2p.h b/lib/e2p/e2p.h index c3a6b25..ae7dd74 100644 --- a/lib/e2p/e2p.h +++ b/lib/e2p/e2p.h @@ -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); diff --git a/lib/e2p/feature.c b/lib/e2p/feature.c index ae7f7f0..515a0f8 100644 --- a/lib/e2p/feature.c +++ b/lib/e2p/feature.c @@ -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; }