summaryrefslogtreecommitdiffstats
path: root/xlators/features/bit-rot/src/stub/bit-rot-stub.c
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2018-08-21 19:20:25 +0300
committerAmar Tumballi <amarts@redhat.com>2018-08-31 01:36:34 +0000
commit4b5e3c347cfb80e4749f1963e25d1dd93563f784 (patch)
treef82b1948aec0110f46d3c1e2b230c6971871f413 /xlators/features/bit-rot/src/stub/bit-rot-stub.c
parent4996a229f3d09cbb6ed3b9dcdf1d527f36803919 (diff)
bit-rot xlator: strncpy()->sprintf(), reduce strlen()'s
xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c xlators/features/bit-rot/src/stub/bit-rot-stub-helpers.c xlators/features/bit-rot/src/stub/bit-rot-stub.c xlators/features/bit-rot/src/stub/bit-rot-stub.h strncpy may not be very efficient for short strings copied into a large buffer: If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written. Instead, use snprintf(). Ensure sprintf() results do not truncate. Also: - save the result of strlen() and re-use it when possible. - move from strlen to SLEN or sizeof() for const strings. - move ret from int32 to int. Compile-tested only! Change-Id: Ib9b923c45d2d59ac15a105410e8160b252061018 updates: bz#1193929 Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'xlators/features/bit-rot/src/stub/bit-rot-stub.c')
-rw-r--r--xlators/features/bit-rot/src/stub/bit-rot-stub.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/xlators/features/bit-rot/src/stub/bit-rot-stub.c b/xlators/features/bit-rot/src/stub/bit-rot-stub.c
index c485348..f7fdd49 100644
--- a/xlators/features/bit-rot/src/stub/bit-rot-stub.c
+++ b/xlators/features/bit-rot/src/stub/bit-rot-stub.c
@@ -55,11 +55,11 @@ mem_acct_init (xlator_t *this)
return ret;
}
-int32_t
+int
br_stub_bad_object_container_init (xlator_t *this, br_stub_private_t *priv)
{
pthread_attr_t w_attr;
- int32_t ret = -1;
+ int ret = -1;
ret = pthread_cond_init(&priv->container.bad_cond, NULL);
if (ret != 0) {
@@ -117,7 +117,7 @@ out:
int32_t
init (xlator_t *this)
{
- int32_t ret = 0;
+ int ret = 0;
char *tmp = NULL;
struct timeval tv = {0,};
br_stub_private_t *priv = NULL;
@@ -139,11 +139,14 @@ init (xlator_t *this)
GF_OPTION_INIT ("bitrot", priv->do_versioning, bool, free_mempool);
GF_OPTION_INIT ("export", tmp, str, free_mempool);
- strncpy (priv->export, tmp, PATH_MAX-1);
- priv->export[PATH_MAX-1] = '\0';
- (void) snprintf (priv->stub_basepath, sizeof (priv->stub_basepath),
- "%s/%s", priv->export, BR_STUB_QUARANTINE_DIR);
+ if (snprintf (priv->export, PATH_MAX, "%s", tmp) >= PATH_MAX)
+ goto free_mempool;
+
+ if (snprintf (priv->stub_basepath, sizeof (priv->stub_basepath),
+ "%s/%s", priv->export, BR_STUB_QUARANTINE_DIR) >=
+ sizeof (priv->stub_basepath))
+ goto free_mempool;
(void) gettimeofday (&tv, NULL);
@@ -1774,7 +1777,7 @@ br_stub_getxattr (call_frame_t *frame, xlator_t *this,
*/
if (name
&& (strncmp (name, GLUSTERFS_GET_BR_STUB_INIT_TIME,
- strlen (GLUSTERFS_GET_BR_STUB_INIT_TIME)) == 0)
+ sizeof (GLUSTERFS_GET_BR_STUB_INIT_TIME) - 1) == 0)
&& ((gf_uuid_compare (loc->gfid, rootgfid) == 0)
|| (gf_uuid_compare (loc->inode->gfid, rootgfid) == 0))) {
BR_STUB_RESET_LOCAL_NULL (frame);
@@ -1786,7 +1789,7 @@ br_stub_getxattr (call_frame_t *frame, xlator_t *this,
goto wind;
if (name && (strncmp (name, GLUSTERFS_GET_OBJECT_SIGNATURE,
- strlen (GLUSTERFS_GET_OBJECT_SIGNATURE)) == 0)) {
+ sizeof (GLUSTERFS_GET_OBJECT_SIGNATURE) - 1) == 0)) {
cookie = (void *) BR_STUB_REQUEST_COOKIE;
local = br_stub_alloc_local (this);
@@ -1854,7 +1857,7 @@ br_stub_fgetxattr (call_frame_t *frame, xlator_t *this,
*/
if (name
&& (strncmp (name, GLUSTERFS_GET_BR_STUB_INIT_TIME,
- strlen (GLUSTERFS_GET_BR_STUB_INIT_TIME)) == 0)
+ sizeof (GLUSTERFS_GET_BR_STUB_INIT_TIME) - 1) == 0)
&& (gf_uuid_compare (fd->inode->gfid, rootgfid) == 0)) {
BR_STUB_RESET_LOCAL_NULL (frame);
br_stub_send_stub_init_time (frame, this);
@@ -1865,7 +1868,7 @@ br_stub_fgetxattr (call_frame_t *frame, xlator_t *this,
goto wind;
if (name && (strncmp (name, GLUSTERFS_GET_OBJECT_SIGNATURE,
- strlen (GLUSTERFS_GET_OBJECT_SIGNATURE)) == 0)) {
+ sizeof (GLUSTERFS_GET_OBJECT_SIGNATURE) - 1) == 0)) {
cookie = (void *) BR_STUB_REQUEST_COOKIE;
local = br_stub_alloc_local (this);