Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPAL: fix string buffer allocation for large env variables #8144

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, ";");
jsquyres marked this conversation as resolved.
Show resolved Hide resolved
} 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