Skip to content

Commit

Permalink
Merge pull request #8144 from devreal/fix_opal_add_to_env_str_alloc
Browse files Browse the repository at this point in the history
OPAL: fix string buffer allocation for large env variables
  • Loading branch information
jsquyres authored Oct 30, 2020
2 parents 25e4741 + 320a9a1 commit dca2058
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions opal/util/keyval_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,33 +276,50 @@ static int save_param_name (void)

static int add_to_env_str(char *var, char *val)
{
int sz, varsz, valsz;
int sz, varsz, valsz, new_envsize;
void *tmp;

if (NULL == var) {
return OPAL_ERR_BAD_PARAM;
}

varsz = strlen(var);
if (NULL != val) {
valsz = strlen(val);
/* account for '=' */
valsz += 1;
}
sz = 0;
if (NULL != env_str) {
varsz = strlen(var);
valsz = (NULL != val) ? strlen(val) : 0;
sz = strlen(env_str)+varsz+valsz+2;
if (envsize <= sz) {
envsize *=2;
sz = strlen(env_str);
/* account for ';' */
sz += 1;
}
/* add required new size incl NUL byte */
sz += varsz+valsz+1;

tmp = realloc(env_str, envsize);
/* make sure we have sufficient space */
new_envsize = envsize;
while (new_envsize <= sz) {
new_envsize *=2;
}

if (NULL != env_str) {
if (new_envsize > envsize) {
tmp = realloc(env_str, new_envsize);
if (NULL == tmp) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
env_str = tmp;
}
strcat(env_str, ";");
} else {
env_str = calloc(1, envsize);
env_str = calloc(1, new_envsize);
if (NULL == env_str) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
}
envsize = new_envsize;

strcat(env_str, var);
if (NULL != val) {
Expand Down

0 comments on commit dca2058

Please sign in to comment.