From 4a06f851dcad6bdd730f3d2e12bd8f26709f27fe Mon Sep 17 00:00:00 2001 From: Sunny Kumar Date: Wed, 20 Dec 2017 13:30:39 +0530 Subject: snapshot: fix several coverity issues in glusterd-snapshot.c This patch fixes issues 157, 426, 428, 431, 432, 437,439, 482 from [1]. [1] https://download.gluster.org/pub/gluster/glusterfs/static-analysis/master/glusterfs-coverity/2017-12-13-e255385a/html/ Change-Id: Iff9df12bd9802db29434155badb1beda045aba5b BUG: 789278 Signed-off-by: Sunny Kumar --- libglusterfs/src/common-utils.c | 14 ++++++++++++++ libglusterfs/src/common-utils.h | 3 +++ 2 files changed, 17 insertions(+) (limited to 'libglusterfs') diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index ef76bdfe750..46a3084fe39 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -4931,3 +4931,17 @@ get_struct_variable (int mem_num, gf_gsync_status_t *sts_val) out: return NULL; } + +/* * Safe wrapper function for strncpy. + * This wrapper makes sure that when there is no null byte among the first n in + * source srting for strncpy function call, the string placed in dest will be + * null-terminated. + */ + +char * +gf_strncpy (char *dest, const char *src, const size_t dest_size) +{ + strncpy (dest, src, dest_size - 1); + dest[dest_size - 1] = '\0'; + return dest; +} diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index 8df96c338f5..89f7927aae1 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -948,4 +948,7 @@ glusterfs_compute_sha256 (const unsigned char *content, size_t size, char* get_struct_variable (int mem_num, gf_gsync_status_t *sts_val); +char * +gf_strncpy (char *dest, const char *src, const size_t dest_size); + #endif /* _COMMON_UTILS_H */ -- cgit