Skip to content

Commit

Permalink
Add find_all to eregmatch() nasl function
Browse files Browse the repository at this point in the history
The current implementation only returns the first match in the string.
Now it will return all matches if the option `find_all: TRUE` is passed to the function.
For backward compatibility, if FALSE is passed or no option, it will return the first match as before.
  • Loading branch information
jjnicola committed Sep 14, 2021
1 parent 662946e commit fd8d730
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add nasl function sftp_enabled_check() to check if sftp subsystem is enabled in the target.
- [#853](https://github.com/greenbone/openvas/pull/853)
- [#862](https://github.com/greenbone/openvas/pull/862)
- Add `find_all` to eregmatch() nasl function. Backport PR #875. [#876](https://github.com/greenbone/openvas/pull/876)

### Changed
- function script_bugtraq_id getting skipped, linter warns. [#724](https://github.com/greenbone/openvas/pull/724)
Expand Down
68 changes: 54 additions & 14 deletions nasl/nasl_text_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ nasl_eregmatch (lex_ctxt *lexic)
char *pattern = get_str_var_by_name (lexic, "pattern");
char *string = get_str_var_by_name (lexic, "string");
int icase = get_int_var_by_name (lexic, "icase", 0);
int copt = 0, i;
int find_all = get_int_var_by_name (lexic, "find_all", 0);
int copt = 0;
tree_cell *retc;
regex_t re;
regmatch_t subs[NS];
Expand All @@ -814,24 +815,63 @@ nasl_eregmatch (lex_ctxt *lexic)
pattern);
return NULL;
}
retc = alloc_typed_cell (DYN_ARRAY);
retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));

if (regexec (&re, string, (size_t) NS, subs, 0) != 0)
if (find_all == 0)
{
regfree (&re);
return NULL;
if (regexec (&re, string, (size_t) NS, subs, 0) != 0)
{
regfree (&re);
return NULL;
}

int i;
for (i = 0; i < NS; i++)
if (subs[i].rm_so != -1)
{
v.var_type = VAR2_DATA;
v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
v.v.v_str.s_val = (unsigned char *) string + subs[i].rm_so;
(void) add_var_to_list (a, i, &v);
}
}
else
{
int index = 0;
char *current_pos;
current_pos = string;
while (1)
{
if (regexec (&re, current_pos, (size_t) NS, subs, 0) != 0)
{
regfree (&re);
break;
}

retc = alloc_typed_cell (DYN_ARRAY);
retc->x.ref_val = a = g_malloc0 (sizeof (nasl_array));
unsigned int offset = 0, i = 0;
for (i = 0; i < NS; i++)
{
char current_pos_cp[strlen (current_pos) + 1];

for (i = 0; i < NS; i++)
if (subs[i].rm_so != -1)
{
v.var_type = VAR2_DATA;
v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
v.v.v_str.s_val = (unsigned char *) string + subs[i].rm_so;
(void) add_var_to_list (a, i, &v);
}
if (subs[i].rm_so == -1)
break;

if (i == 0)
offset = subs[i].rm_eo;

strcpy (current_pos_cp, current_pos);
current_pos_cp[subs[i].rm_eo] = 0;
v.var_type = VAR2_DATA;
v.v.v_str.s_siz = subs[i].rm_eo - subs[i].rm_so;
v.v.v_str.s_val =
(unsigned char *) current_pos_cp + subs[i].rm_so;
(void) add_var_to_list (a, index, &v);
index++;
}
current_pos += offset;
}
}

regfree (&re);
return retc;
Expand Down

0 comments on commit fd8d730

Please sign in to comment.