Avoid various compilation warnings due to strncpy:

libf2fs.c:590:33: warning: ‘snprintf’ output may be truncated before
the last format character [-Wformat-truncation=]
  snprintf(rootdev, ret, "/dev/%s", buf);

../include/f2fs_fs.h:1384:2: warning: ‘strncpy’ specified bound
depends on the length of the source argument [-Wstringop-overflow=]
  strncpy(buf, features, strlen(features) + 1);

f2fstat.c:243:3: warning: ‘strncpy’ output truncated before
terminating nul copying as many bytes from a string as its length
[-Wstringop-truncation]
   strncpy(ptr_buf, name[i], strlen(name[i]));

This patch does not change any functionality.

Signed-off-by: Damien Le Moal <damien.lem...@wdc.com>
Reviewed-by: Chao Yu <yuch...@huawei.com>
---
 include/f2fs_fs.h | 6 +++---
 lib/libf2fs.c     | 2 +-
 tools/f2fstat.c   | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index a730688..e073723 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -1379,9 +1379,9 @@ static inline int parse_feature(struct feature *table, 
const char *features)
 {
        char *buf, *sub, *next;
 
-       buf = calloc(strlen(features) + 1, sizeof(char));
-       ASSERT(buf);
-       strncpy(buf, features, strlen(features) + 1);
+       buf = strdup(features);
+       if (!buf)
+               return -1;
 
        for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
                /* Skip the beginning blanks */
diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index c692bf2..60b84e0 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -587,7 +587,7 @@ char *get_rootdev()
                return NULL;
        rootdev[ret] = '\0';
 
-       snprintf(rootdev, ret, "/dev/%s", buf);
+       snprintf(rootdev, ret + 1, "/dev/%s", buf);
        return rootdev;
 #endif
 }
diff --git a/tools/f2fstat.c b/tools/f2fstat.c
index 7f485b0..0618806 100644
--- a/tools/f2fstat.c
+++ b/tools/f2fstat.c
@@ -240,7 +240,7 @@ void print_head(char *res)
 
        for (i = 0; i < 20; i++) {
                ptr = (i == 0) ? strtok(res, " ") : strtok(NULL, " ");
-               strncpy(ptr_buf, name[i], strlen(name[i]));
+               strcpy(ptr_buf, name[i]);
                if (i == 1) {
                        prev_index = ptr_buf - buf - 1;
                } else if (i == 7) {
-- 
2.20.1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to