Make ...search_identifier safe against NULL arguments

Otherwise strstr will fail
This commit is contained in:
Holger Vogt 2019-09-08 09:34:15 +02:00
parent 0071ad8569
commit aa726c9864
1 changed files with 47 additions and 44 deletions

View File

@ -2389,7 +2389,7 @@ static void inp_stripcomments_deck(struct card *c, bool cf)
('$' outside of .control section is o.k. however). ('$' outside of .control section is o.k. however).
If the comaptibility mode is PS, LTPS or LTPSA, '$' is treated as a valid If the comaptibility mode is PS, LTPS or LTPSA, '$' is treated as a valid
character, not a s end-of-line comment delimiter, except for that it is character, not as end-of-line comment delimiter, except for that it is
located at the beginning of a line. If inside of a control section, located at the beginning of a line. If inside of a control section,
still '$ ' is read a an end-of-line comment delimiter.*/ still '$ ' is read a an end-of-line comment delimiter.*/
static void inp_stripcomments_line(char *s, bool cs) static void inp_stripcomments_line(char *s, bool cs)
@ -4452,74 +4452,77 @@ static bool b_transformation_wanted(const char *p)
char *search_identifier(char *str, const char *identifier, char *str_begin) char *search_identifier(char *str, const char *identifier, char *str_begin)
{ {
while ((str = strstr(str, identifier)) != NULL) { if (str && identifier) {
char before; while ((str = strstr(str, identifier)) != NULL) {
char before;
if (str > str_begin) if (str > str_begin)
before = str[-1]; before = str[-1];
else else
before = '\0'; before = '\0';
if (is_arith_char(before) || isspace_c(before) || if (is_arith_char(before) || isspace_c(before) ||
strchr("=,{", before)) { strchr("=,{", before)) {
char after = str[strlen(identifier)]; char after = str[strlen(identifier)];
if (is_arith_char(after) || isspace_c(after) || if (is_arith_char(after) || isspace_c(after) ||
strchr(",}", after)) strchr(",}", after))
return str; return str;
}
str++;
} }
str++;
} }
return NULL; return NULL;
} }
char *ya_search_identifier(char *str, const char *identifier, char *str_begin) char *ya_search_identifier(char *str, const char *identifier, char *str_begin)
{ {
while ((str = strstr(str, identifier)) != NULL) { if (str && identifier) {
char before; while ((str = strstr(str, identifier)) != NULL) {
char before;
if (str > str_begin) if (str > str_begin)
before = str[-1]; before = str[-1];
else else
before = '\0'; before = '\0';
if (is_arith_char(before) || isspace_c(before) || if (is_arith_char(before) || isspace_c(before) ||
(str <= str_begin)) { (str <= str_begin)) {
char after = str[strlen(identifier)]; char after = str[strlen(identifier)];
if ((is_arith_char(after) || isspace_c(after) || after == '\0')) if ((is_arith_char(after) || isspace_c(after) ||
break; after == '\0'))
break;
}
str++;
} }
str++;
} }
return str; return str;
} }
static char *search_plain_identifier(char *str, const char *identifier) static char *search_plain_identifier(char *str, const char *identifier)
{ {
char *str_begin = str; if (str && identifier) {
char *str_begin = str;
while ((str = strstr(str, identifier)) != NULL) {
char before;
while ((str = strstr(str, identifier)) != NULL) { if (str > str_begin)
char before; before = str[-1];
else
before = '\0';
if (str > str_begin) if (!before || !identifier_char(before)) {
before = str[-1]; char after = str[strlen(identifier)];
else if (!after || !identifier_char(after))
before = '\0'; return str;
}
if (!before || !identifier_char(before)) { str += strlen(identifier);
char after = str[strlen(identifier)];
if (!after || !identifier_char(after))
return str;
} }
str += strlen(identifier);
} }
return NULL; return NULL;
} }