This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Fix parsing of envvars in MCA files #973
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
* University of Stuttgart. All rights reserved. | ||
* Copyright (c) 2004-2005 The Regents of the University of California. | ||
* All rights reserved. | ||
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights | ||
* Copyright (c) 2015-2016 Los Alamos National Security, LLC. All rights | ||
* reserved. | ||
* $COPYRIGHT$ | ||
* | ||
|
@@ -27,6 +27,7 @@ | |
#include "opal/util/output.h" | ||
#include "opal/threads/mutex.h" | ||
#include <string.h> | ||
#include <ctype.h> | ||
|
||
int opal_util_keyval_parse_lineno = 0; | ||
|
||
|
@@ -200,53 +201,58 @@ int opal_util_keyval_save_internal_envars(opal_keyval_parse_fn_t callback) | |
return OPAL_SUCCESS; | ||
} | ||
|
||
static int trim_name(char **buffer, const char* prefix, const char* suffix) | ||
static void trim_name(char *buffer, const char* prefix, const char* suffix) | ||
{ | ||
char *pchr, *echr, *tmp; | ||
int size; | ||
if (NULL == *buffer) { | ||
return 1; | ||
char *pchr, *echr; | ||
size_t buffer_len; | ||
|
||
if (NULL == buffer) { | ||
return; | ||
} | ||
pchr = *buffer; | ||
|
||
buffer_len = strlen (buffer); | ||
|
||
pchr = buffer; | ||
if (NULL != prefix) { | ||
pchr = strstr(*buffer, prefix); | ||
if (NULL != pchr) { | ||
pchr += strlen(prefix); | ||
} else { | ||
pchr = *buffer; | ||
size_t prefix_len = strlen (prefix); | ||
|
||
if (0 == strncmp (buffer, prefix, prefix_len)) { | ||
pchr += prefix_len; | ||
} | ||
} | ||
|
||
/* trim spaces at the beginning */ | ||
while (' ' == *pchr || '\t' == *pchr) { | ||
while (isspace (*pchr)) { | ||
pchr++; | ||
} | ||
|
||
/* trim spaces at the end */ | ||
echr = *buffer+strlen(*buffer)-1; | ||
while (' ' == *echr || '\t' == *echr || '\n' == *echr) { | ||
echr = buffer + buffer_len - 1; | ||
while (isspace (*echr)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't there be a check here to ensure that you don't run past the beginning of the buffer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, will fix that and update the PR. |
||
echr--; | ||
} | ||
echr++; | ||
*echr = '\0'; | ||
echr[1] = '\0'; | ||
|
||
if (NULL != suffix) { | ||
if (!strncmp(echr-strlen(suffix), suffix, strlen(suffix))) { | ||
echr -= strlen(suffix)+1; | ||
while (' ' == *echr || '\t' == *echr) { | ||
size_t suffix_len = strlen (suffix); | ||
|
||
echr -= suffix_len - 1; | ||
|
||
if (0 == strncmp (echr, suffix, strlen(suffix))) { | ||
do { | ||
echr--; | ||
} | ||
echr++; | ||
*echr = '\0'; | ||
} while (isspace (*echr)); | ||
echr[1] = '\0'; | ||
} | ||
} | ||
size = strlen(pchr)+1; | ||
tmp = malloc(size); | ||
strncpy(tmp, pchr, size); | ||
*buffer = realloc(*buffer, size); | ||
strncpy(*buffer, tmp, size); | ||
free(tmp); | ||
return 0; | ||
|
||
if (buffer != pchr) { | ||
/* move the trimmed string to the beginning of the buffer */ | ||
memmove (buffer, pchr, strlen (pchr) + 1); | ||
} | ||
} | ||
|
||
static int save_param_name(const char* prefix, const char* suffix) | ||
static int save_param_name (void) | ||
{ | ||
if (key_buffer_len < strlen(opal_util_keyval_yytext) + 1) { | ||
char *tmp; | ||
|
@@ -261,8 +267,8 @@ static int save_param_name(const char* prefix, const char* suffix) | |
key_buffer = tmp; | ||
} | ||
|
||
strncpy(key_buffer, opal_util_keyval_yytext, key_buffer_len); | ||
trim_name(&key_buffer, prefix, suffix); | ||
strncpy (key_buffer, opal_util_keyval_yytext, key_buffer_len); | ||
|
||
return OPAL_SUCCESS; | ||
} | ||
|
||
|
@@ -309,18 +315,26 @@ static int parse_line_new(opal_keyval_parse_state_t first_val) | |
{ | ||
opal_keyval_parse_state_t val; | ||
char *tmp; | ||
int rc; | ||
|
||
val = first_val; | ||
while (OPAL_UTIL_KEYVAL_PARSE_NEWLINE != val && OPAL_UTIL_KEYVAL_PARSE_DONE != val) { | ||
rc = save_param_name (); | ||
if (OPAL_SUCCESS != rc) { | ||
return rc; | ||
} | ||
|
||
if (OPAL_UTIL_KEYVAL_PARSE_MCAVAR == val) { | ||
save_param_name("-mca", NULL); | ||
trim_name (key_buffer, "-mca", NULL); | ||
trim_name (key_buffer, "--mca", NULL); | ||
|
||
val = opal_util_keyval_yylex(); | ||
if (OPAL_UTIL_KEYVAL_PARSE_VALUE == val) { | ||
if (NULL != opal_util_keyval_yytext) { | ||
tmp = strdup(opal_util_keyval_yytext); | ||
if ('\'' == tmp[0] || '\"' == tmp[0]) { | ||
trim_name(&tmp, "\'", "\'"); | ||
trim_name(&tmp, "\"", "\""); | ||
trim_name (tmp, "\'", "\'"); | ||
trim_name (tmp, "\"", "\""); | ||
} | ||
keyval_callback(key_buffer, tmp); | ||
free(tmp); | ||
|
@@ -330,7 +344,9 @@ static int parse_line_new(opal_keyval_parse_state_t first_val) | |
return OPAL_ERROR; | ||
} | ||
} else if (OPAL_UTIL_KEYVAL_PARSE_ENVEQL == val) { | ||
save_param_name("-x", "="); | ||
trim_name (key_buffer, "-x", "="); | ||
trim_name (key_buffer, "--x", NULL); | ||
|
||
val = opal_util_keyval_yylex(); | ||
if (OPAL_UTIL_KEYVAL_PARSE_VALUE == val) { | ||
add_to_env_str(key_buffer, opal_util_keyval_yytext); | ||
|
@@ -339,7 +355,8 @@ static int parse_line_new(opal_keyval_parse_state_t first_val) | |
return OPAL_ERROR; | ||
} | ||
} else if (OPAL_UTIL_KEYVAL_PARSE_ENVVAR == val) { | ||
save_param_name("-x", "="); | ||
trim_name (key_buffer, "-x", "="); | ||
trim_name (key_buffer, "--x", NULL); | ||
add_to_env_str(key_buffer, NULL); | ||
} else { | ||
/* we got something unexpected. Bonk! */ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize that this is the way it was before you fixed it -- but shouldn't the prefix check come after the beginning-of-string white space trim?
I ask because the suffix check comes after the end-of-string white space trim.