stack: Reduce stack usage for local variables to store tmpfile names

This patch moves stack based PATH_MAX allocations for tmpfile
names, to heap allocated names instead. Reducing the impact on
stack space used and accruing benefits thereof.

Change-Id: I646d9cb091018de6768b3523902788fa2ba14d96
Updates: bz#1193929
Signed-off-by: ShyamsundarR <srangana@redhat.com>
This commit is contained in:
ShyamsundarR 2018-07-26 13:32:52 -04:00 committed by Amar Tumballi
parent 46a2cbfb73
commit 405c6e8a8a
4 changed files with 62 additions and 22 deletions

View File

@ -555,14 +555,13 @@ glusterfs_graph_construct (FILE *fp)
int tmp_fd = -1;
glusterfs_graph_t *graph = NULL;
FILE *tmp_file = NULL;
char template[PATH_MAX] = {0};
char template[] = "/tmp/tmp.XXXXXX";
static pthread_mutex_t graph_mutex = PTHREAD_MUTEX_INITIALIZER;
graph = glusterfs_graph_new ();
if (!graph)
goto err;
strcpy (template, "/tmp/tmp.XXXXXX");
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
tmp_fd = mkstemp (template);
if (-1 == tmp_fd)

View File

@ -90,7 +90,7 @@ glusterd_is_gfproxyd_enabled (glusterd_volinfo_t *volinfo)
static int
glusterd_svc_get_gfproxyd_volfile (glusterd_volinfo_t *volinfo, char *svc_name,
char *orgvol, char *tmpvol, int path_len)
char *orgvol, char **tmpvol, int path_len)
{
int tmp_fd = -1;
int ret = -1;
@ -99,23 +99,31 @@ glusterd_svc_get_gfproxyd_volfile (glusterd_volinfo_t *volinfo, char *svc_name,
glusterd_svc_build_gfproxyd_volfile_path (volinfo, orgvol,
path_len);
snprintf (tmpvol, path_len, "/tmp/g%s-XXXXXX", svc_name);
ret = gf_asprintf(tmpvol, "/tmp/g%s-XXXXXX", svc_name);
if (ret < 0) {
goto out;
}
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
tmp_fd = mkstemp (tmpvol);
tmp_fd = mkstemp (*tmpvol);
if (tmp_fd < 0) {
gf_msg ("glusterd", GF_LOG_WARNING, errno,
GD_MSG_FILE_OP_FAILED, "Unable to create temp file"
" %s:(%s)", tmpvol, strerror (errno));
" %s:(%s)", *tmpvol, strerror (errno));
ret = -1;
goto out;
}
need_unlink = 1;
ret = glusterd_build_gfproxyd_volfile (volinfo, tmpvol);
ret = glusterd_build_gfproxyd_volfile (volinfo, *tmpvol);
out:
if (need_unlink && ret < 0)
sys_unlink (tmpvol);
sys_unlink (*tmpvol);
if ((ret < 0) && (*tmpvol != NULL)) {
GF_FREE(*tmpvol);
*tmpvol = NULL;
}
if (tmp_fd >= 0)
sys_close (tmp_fd);
@ -129,14 +137,14 @@ glusterd_svc_check_gfproxyd_volfile_identical (char *svc_name,
gf_boolean_t *identical)
{
char orgvol[PATH_MAX] = {0,};
char tmpvol[PATH_MAX] = {0,};
char *tmpvol = NULL;
int ret = -1;
int need_unlink = 0;
GF_VALIDATE_OR_GOTO ("glusterd", identical, out);
ret = glusterd_svc_get_gfproxyd_volfile (volinfo, svc_name, orgvol,
tmpvol, PATH_MAX);
&tmpvol, PATH_MAX);
if (ret)
goto out;
@ -150,6 +158,9 @@ out:
if (need_unlink)
sys_unlink (tmpvol);
if (tmpvol != NULL)
GF_FREE (tmpvol);
return ret;
}
@ -159,14 +170,14 @@ glusterd_svc_check_gfproxyd_topology_identical (char *svc_name,
gf_boolean_t *identical)
{
char orgvol[PATH_MAX] = {0,};
char tmpvol[PATH_MAX] = {0,};
char *tmpvol = NULL;
int ret = -1;
int tmpclean = 0;
GF_VALIDATE_OR_GOTO ("glusterd", identical, out);
ret = glusterd_svc_get_gfproxyd_volfile (volinfo, svc_name, orgvol,
tmpvol, PATH_MAX);
&tmpvol, PATH_MAX);
if (ret)
goto out;
@ -178,6 +189,10 @@ glusterd_svc_check_gfproxyd_topology_identical (char *svc_name,
out:
if (tmpclean)
sys_unlink (tmpvol);
if (tmpvol != NULL)
GF_FREE (tmpvol);
return ret;
}

View File

@ -162,7 +162,7 @@ glusterd_svc_check_volfile_identical (char *svc_name,
gf_boolean_t *identical)
{
char orgvol[PATH_MAX] = {0,};
char tmpvol[PATH_MAX] = {0,};
char *tmpvol = NULL;
glusterd_conf_t *conf = NULL;
xlator_t *this = NULL;
int ret = -1;
@ -178,7 +178,10 @@ glusterd_svc_check_volfile_identical (char *svc_name,
glusterd_svc_build_volfile_path (svc_name, conf->workdir,
orgvol, sizeof (orgvol));
snprintf (tmpvol, sizeof (tmpvol), "/tmp/g%s-XXXXXX", svc_name);
ret = gf_asprintf(&tmpvol, "/tmp/g%s-XXXXXX", svc_name);
if (ret < 0) {
goto out;
}
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
tmp_fd = mkstemp (tmpvol);
@ -186,6 +189,7 @@ glusterd_svc_check_volfile_identical (char *svc_name,
gf_msg (this->name, GF_LOG_WARNING, errno,
GD_MSG_FILE_OP_FAILED, "Unable to create temp file"
" %s:(%s)", tmpvol, strerror (errno));
ret = -1;
goto out;
}
@ -196,11 +200,13 @@ glusterd_svc_check_volfile_identical (char *svc_name,
goto out;
ret = glusterd_check_files_identical (orgvol, tmpvol, identical);
out:
if (need_unlink)
sys_unlink (tmpvol);
if (tmpvol != NULL)
GF_FREE(tmpvol);
if (tmp_fd >= 0)
sys_close (tmp_fd);
@ -213,7 +219,7 @@ glusterd_svc_check_topology_identical (char *svc_name,
gf_boolean_t *identical)
{
char orgvol[PATH_MAX] = {0,};
char tmpvol[PATH_MAX] = {0,};
char *tmpvol = NULL;
glusterd_conf_t *conf = NULL;
xlator_t *this = THIS;
int ret = -1;
@ -231,13 +237,18 @@ glusterd_svc_check_topology_identical (char *svc_name,
orgvol, sizeof (orgvol));
/* Create the temporary volfile */
snprintf (tmpvol, sizeof (tmpvol), "/tmp/g%s-XXXXXX", svc_name);
ret = gf_asprintf(&tmpvol, "/tmp/g%s-XXXXXX", svc_name);
if (ret < 0) {
goto out;
}
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
tmpfd = mkstemp (tmpvol);
if (tmpfd < 0) {
gf_msg (this->name, GF_LOG_WARNING, errno,
GD_MSG_FILE_OP_FAILED, "Unable to create temp file"
" %s:(%s)", tmpvol, strerror (errno));
ret = -1;
goto out;
}
@ -256,5 +267,7 @@ out:
sys_close (tmpfd);
if (tmpclean)
sys_unlink (tmpvol);
if (tmpvol != NULL)
GF_FREE(tmpvol);
return ret;
}

View File

@ -89,7 +89,7 @@ glusterd_svc_check_tier_volfile_identical (char *svc_name,
gf_boolean_t *identical)
{
char orgvol[PATH_MAX] = {0,};
char tmpvol[PATH_MAX] = {0,};
char *tmpvol = NULL;
xlator_t *this = NULL;
int ret = -1;
int need_unlink = 0;
@ -103,7 +103,10 @@ glusterd_svc_check_tier_volfile_identical (char *svc_name,
glusterd_svc_build_tierd_volfile_path (volinfo, orgvol,
sizeof (orgvol));
snprintf (tmpvol, sizeof (tmpvol), "/tmp/g%s-XXXXXX", svc_name);
ret = gf_asprintf(&tmpvol, "/tmp/g%s-XXXXXX", svc_name);
if (ret < 0) {
goto out;
}
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
tmp_fd = mkstemp (tmpvol);
@ -111,6 +114,7 @@ glusterd_svc_check_tier_volfile_identical (char *svc_name,
gf_msg (this->name, GF_LOG_WARNING, errno,
GD_MSG_FILE_OP_FAILED, "Unable to create temp file"
" %s:(%s)", tmpvol, strerror (errno));
ret = -1;
goto out;
}
@ -128,6 +132,9 @@ out:
if (need_unlink)
sys_unlink (tmpvol);
if (tmpvol != NULL)
GF_FREE(tmpvol);
if (tmp_fd >= 0)
sys_close (tmp_fd);
@ -140,7 +147,7 @@ glusterd_svc_check_tier_topology_identical (char *svc_name,
gf_boolean_t *identical)
{
char orgvol[PATH_MAX] = {0,};
char tmpvol[PATH_MAX] = {0,};
char *tmpvol = NULL;
glusterd_conf_t *conf = NULL;
xlator_t *this = THIS;
int ret = -1;
@ -157,7 +164,10 @@ glusterd_svc_check_tier_topology_identical (char *svc_name,
glusterd_svc_build_tierd_volfile_path (volinfo, orgvol,
sizeof (orgvol));
snprintf (tmpvol, sizeof (tmpvol), "/tmp/g%s-XXXXXX", svc_name);
ret = gf_asprintf(&tmpvol, "/tmp/g%s-XXXXXX", svc_name);
if (ret < 0) {
goto out;
}
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
tmpfd = mkstemp (tmpvol);
@ -165,6 +175,7 @@ glusterd_svc_check_tier_topology_identical (char *svc_name,
gf_msg (this->name, GF_LOG_WARNING, errno,
GD_MSG_FILE_OP_FAILED, "Unable to create temp file"
" %s:(%s)", tmpvol, strerror (errno));
ret = -1;
goto out;
}
@ -181,5 +192,7 @@ out:
sys_close (tmpfd);
if (tmpclean)
sys_unlink (tmpvol);
if (tmpvol != NULL)
GF_FREE(tmpvol);
return ret;
}