ECC-381: Palamida scan: Forensic Similarities to code from StackOverflow

This commit is contained in:
Shahram Najm 2016-11-22 11:06:00 +00:00
parent 0c95334754
commit f7379a7945
4 changed files with 14 additions and 13 deletions

View File

@ -249,7 +249,7 @@ static grib_trie* load_bufr_elements_table(grib_accessor* a, int* err)
dictionary=grib_trie_new(c); dictionary=grib_trie_new(c);
while(fgets(line,sizeof(line)-1,f)) { while(fgets(line,sizeof(line)-1,f)) {
list=string_split(line,'|'); list=string_split(line, "|");
grib_trie_insert(dictionary,list[0],list); grib_trie_insert(dictionary,list[0],list);
} }
@ -260,7 +260,7 @@ static grib_trie* load_bufr_elements_table(grib_accessor* a, int* err)
if (!f) {*err=GRIB_IO_PROBLEM; dictionary=NULL; goto the_end;} if (!f) {*err=GRIB_IO_PROBLEM; dictionary=NULL; goto the_end;}
while(fgets(line,sizeof(line)-1,f)) { while(fgets(line,sizeof(line)-1,f)) {
list=string_split(line,'|'); list=string_split(line, "|");
grib_trie_insert(dictionary,list[0],list); grib_trie_insert(dictionary,list[0],list);
} }

View File

@ -1421,7 +1421,7 @@ int is_index_file(const char *filename);
char get_dir_separator_char(void); char get_dir_separator_char(void);
const char *extract_filename(const char *filepath); const char *extract_filename(const char *filepath);
char *codes_getenv(const char *name); char *codes_getenv(const char *name);
char** string_split(char* inputString, const char delimiterChar); char** string_split(char* inputString, const char* delimiter);
/* bufr_util.c */ /* bufr_util.c */
int compute_bufr_key_rank(grib_handle* h, grib_string_list* keys, const char* key); int compute_bufr_key_rank(grib_handle* h, grib_string_list* keys, const char* key);

View File

@ -1682,19 +1682,22 @@ char* codes_getenv(const char* name)
return result; return result;
} }
/* Returns an array of strings the last of which is NULL */ /* Returns an array of strings the last of which is NULL */
char** string_split(char* inputString, const char delimiterChar) char** string_split(char* inputString, const char* delimiter)
{ {
char** result = NULL; char** result = NULL;
char* p = inputString; char* p = inputString;
char* lastDelimiter = NULL; char* lastDelimiter = NULL;
char* aToken = NULL; char* aToken = NULL;
char delimiterString[2] = {0, 0};
size_t numTokens = 0; size_t numTokens = 0;
size_t strLength = 0; size_t strLength = 0;
size_t index = 0; size_t index = 0;
char delimiterChar = 0;
Assert(inputString); DebugAssert(inputString);
DebugAssert( delimiter && (strlen(delimiter)==1) );
delimiterChar = delimiter[0];
while (*p) { while (*p) {
const char ctmp = *p; const char ctmp = *p;
if (ctmp == delimiterChar) { if (ctmp == delimiterChar) {
@ -1712,14 +1715,12 @@ char** string_split(char* inputString, const char delimiterChar)
result = (char**)malloc(numTokens * sizeof(char*)); result = (char**)malloc(numTokens * sizeof(char*));
Assert(result); Assert(result);
delimiterString[0] = delimiterChar;
delimiterString[1] = '\0';
/* Start tokenizing */ /* Start tokenizing */
aToken = strtok(inputString, delimiterString); aToken = strtok(inputString, delimiter);
while (aToken) { while (aToken) {
Assert(index < numTokens); Assert(index < numTokens);
*(result + index++) = strdup(aToken); *(result + index++) = strdup(aToken);
aToken = strtok(NULL, delimiterString); aToken = strtok(NULL, delimiter);
} }
Assert(index == numTokens - 1); Assert(index == numTokens - 1);
*(result + index) = '\0'; *(result + index) = '\0';

View File

@ -1371,7 +1371,7 @@ void test_string_splitting()
int i=0; int i=0;
char input[80] = "Born|To|Be|Wild"; char input[80] = "Born|To|Be|Wild";
char** list=0; char** list=0;
list = string_split(input, '|'); list = string_split(input, "|");
for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */ for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */
assert(i == 4); assert(i == 4);
if ( strcmp(list[0], "Born")!=0 ) assert(0); if ( strcmp(list[0], "Born")!=0 ) assert(0);
@ -1380,14 +1380,14 @@ void test_string_splitting()
if ( strcmp(list[3], "Wild")!=0 ) assert(0); if ( strcmp(list[3], "Wild")!=0 ) assert(0);
strcpy(input, "12345|a gap|"); strcpy(input, "12345|a gap|");
list = string_split(input, '|'); list = string_split(input, "|");
for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */ for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */
assert(i == 2); assert(i == 2);
if ( strcmp(list[0], "12345")!=0 ) assert(0); if ( strcmp(list[0], "12345")!=0 ) assert(0);
if ( strcmp(list[1], "a gap")!=0 ) assert(0); if ( strcmp(list[1], "a gap")!=0 ) assert(0);
strcpy(input, "Steppenwolf"); strcpy(input, "Steppenwolf");
list = string_split(input, '|'); list = string_split(input, ",");
for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */ for(i=0; list[i] != NULL; ++i) {} /* count how many tokens */
assert(i == 1); assert(i == 1);
if ( strcmp(list[0], "Steppenwolf")!=0 ) assert(0); if ( strcmp(list[0], "Steppenwolf")!=0 ) assert(0);