Skip to content

Commit f653e07

Browse files
committed
Add PCRE2 library #3
Move the oscap_get_substring into the oscap_pcre.c module and rename it into oscap_pcre_get_substring. The function imposes implicit dependencies on PCRE/PCRE2 symbols even for utils.c users that won't use PCRE at all (SCE library).
1 parent b34bcbb commit f653e07

File tree

7 files changed

+86
-87
lines changed

7 files changed

+86
-87
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ endif()
530530

531531
if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
532532
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -W -Wall -Wnonnull -Wshadow -Wformat -Wundef -Wno-unused-parameter -Wmissing-prototypes -Wno-unknown-pragmas -Wno-int-conversion -Werror=implicit-function-declaration -D_GNU_SOURCE -std=c99")
533+
add_link_options(-Wl,-z,now)
533534
endif()
534535
if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
535536
add_link_options(-lkvm -lm -lprocstat)

src/OVAL/probes/independent/textfilecontent54_probe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static int process_file(const char *prefix, const char *path, const char *file,
216216
want_instance = 0;
217217

218218
SEXP_free(next_inst);
219-
substr_cnt = oscap_get_substrings(buf, &ofs, pfd->compiled_regex, want_instance, &substrs);
219+
substr_cnt = oscap_pcre_get_substrings(buf, &ofs, pfd->compiled_regex, want_instance, &substrs);
220220

221221
if (substr_cnt < 0) {
222222
SEXP_t *msg;

src/OVAL/probes/independent/textfilecontent_probe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static int process_file(const char *prefix, const char *path, const char *filena
196196
int ofs = 0;
197197

198198
while (fgets(line, sizeof(line), fp) != NULL) {
199-
substr_cnt = oscap_get_substrings(line, &ofs, re, 1, &substrs);
199+
substr_cnt = oscap_pcre_get_substrings(line, &ofs, re, 1, &substrs);
200200
if (substr_cnt > 0) {
201201
int k;
202202
SEXP_t *item;

src/common/oscap_pcre.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include <memory.h>
2828

29+
#define OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT 3500
30+
2931
#ifdef HAVE_PCRE2
3032
#define PCRE2_CODE_UNIT_WIDTH 8
3133
#define PCRE2_ERR_BUF_SIZE 127
@@ -243,6 +245,75 @@ void oscap_pcre_free(oscap_pcre_t *opcre)
243245
}
244246
}
245247

248+
int oscap_pcre_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_substrs, char ***substrings) {
249+
int i, ret, rc;
250+
int ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]);
251+
char **substrs;
252+
253+
// todo: max match count check
254+
255+
for (i = 0; i < ovector_len; ++i) {
256+
ovector[i] = -1;
257+
}
258+
259+
unsigned long limit = OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT;
260+
char *limit_str = getenv("OSCAP_PCRE_EXEC_RECURSION_LIMIT");
261+
if (limit_str != NULL)
262+
if (sscanf(limit_str, "%lu", &limit) <= 0)
263+
dW("Unable to parse OSCAP_PCRE_EXEC_RECURSION_LIMIT value");
264+
oscap_pcre_set_match_limit_recursion(re, limit);
265+
size_t str_len = strlen(str);
266+
#if defined(OS_SOLARIS)
267+
rc = oscap_pcre_exec(re, str, str_len, *ofs, OSCAP_PCRE_OPTS_NO_UTF8_CHECK, ovector, ovector_len);
268+
#else
269+
rc = oscap_pcre_exec(re, str, str_len, *ofs, 0, ovector, ovector_len);
270+
#endif
271+
272+
if (rc < OSCAP_PCRE_ERR_NOMATCH) {
273+
if (str_len < 100)
274+
dE("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%s'.", rc, str);
275+
else
276+
dE("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%.100s' (truncated, showing first 100 characters).", rc, str);
277+
return rc;
278+
} else if (rc == OSCAP_PCRE_ERR_NOMATCH) {
279+
/* no match */
280+
return 0;
281+
}
282+
283+
*ofs = (*ofs == ovector[1]) ? ovector[1] + 1 : ovector[1];
284+
285+
if (!want_substrs) {
286+
/* just report successful match */
287+
return 1;
288+
}
289+
290+
ret = 0;
291+
if (rc == 0) {
292+
/* vector too small */
293+
// todo: report partial results
294+
rc = ovector_len / 3;
295+
}
296+
297+
substrs = malloc(rc * sizeof (char *));
298+
for (i = 0; i < rc; ++i) {
299+
int len;
300+
char *buf;
301+
302+
if (ovector[2 * i] == -1) {
303+
continue;
304+
}
305+
len = ovector[2 * i + 1] - ovector[2 * i];
306+
buf = malloc(len + 1);
307+
memcpy(buf, str + ovector[2 * i], len);
308+
buf[len] = '\0';
309+
substrs[ret] = buf;
310+
++ret;
311+
}
312+
313+
*substrings = substrs;
314+
315+
return ret;
316+
}
246317

247318
void oscap_pcre_err_free(char *err)
248319
{

src/common/oscap_pcre.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ void oscap_pcre_set_match_limit_recursion(oscap_pcre_t *opcre, unsigned long lim
9696
*/
9797
void oscap_pcre_optimize(oscap_pcre_t *opcre);
9898

99+
/**
100+
* Match a regular expression and return substrings.
101+
* Caller is responsible for freeing the returned array.
102+
* @param str subject string
103+
* @param ofs starting offset in str
104+
* @param re compiled regular expression
105+
* @param want_substrs if non-zero, substrings will be returned
106+
* @param substrings contains returned substrings
107+
* @return count of matched substrings, 0 if no match
108+
* negative value on failure
109+
*/
110+
int oscap_pcre_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_substrs, char ***substrings);
99111

100112
/**
101113
* Free the error message returned by oscap_pcre_compile. DON'T USE REGULAR free()!

src/common/util.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#endif
5050

5151
#define PATH_SEPARATOR '/'
52-
#define OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT 3500
5352

5453
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str)
5554
{
@@ -361,76 +360,6 @@ char *oscap_path_join(const char *path1, const char *path2)
361360
return joined_path;
362361
}
363362

364-
int oscap_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_substrs, char ***substrings) {
365-
int i, ret, rc;
366-
int ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]);
367-
char **substrs;
368-
369-
// todo: max match count check
370-
371-
for (i = 0; i < ovector_len; ++i) {
372-
ovector[i] = -1;
373-
}
374-
375-
unsigned long limit = OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT;
376-
char *limit_str = getenv("OSCAP_PCRE_EXEC_RECURSION_LIMIT");
377-
if (limit_str != NULL)
378-
if (sscanf(limit_str, "%lu", &limit) <= 0)
379-
dW("Unable to parse OSCAP_PCRE_EXEC_RECURSION_LIMIT value");
380-
oscap_pcre_set_match_limit_recursion(re, limit);
381-
size_t str_len = strlen(str);
382-
#if defined(OS_SOLARIS)
383-
rc = oscap_pcre_exec(re, str, str_len, *ofs, OSCAP_PCRE_OPTS_NO_UTF8_CHECK, ovector, ovector_len);
384-
#else
385-
rc = oscap_pcre_exec(re, str, str_len, *ofs, 0, ovector, ovector_len);
386-
#endif
387-
388-
if (rc < OSCAP_PCRE_ERR_NOMATCH) {
389-
if (str_len < 100)
390-
dE("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%s'.", rc, str);
391-
else
392-
dE("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%.100s' (truncated, showing first 100 characters).", rc, str);
393-
return rc;
394-
} else if (rc == OSCAP_PCRE_ERR_NOMATCH) {
395-
/* no match */
396-
return 0;
397-
}
398-
399-
*ofs = (*ofs == ovector[1]) ? ovector[1] + 1 : ovector[1];
400-
401-
if (!want_substrs) {
402-
/* just report successful match */
403-
return 1;
404-
}
405-
406-
ret = 0;
407-
if (rc == 0) {
408-
/* vector too small */
409-
// todo: report partial results
410-
rc = ovector_len / 3;
411-
}
412-
413-
substrs = malloc(rc * sizeof (char *));
414-
for (i = 0; i < rc; ++i) {
415-
int len;
416-
char *buf;
417-
418-
if (ovector[2 * i] == -1) {
419-
continue;
420-
}
421-
len = ovector[2 * i + 1] - ovector[2 * i];
422-
buf = malloc(len + 1);
423-
memcpy(buf, str + ovector[2 * i], len);
424-
buf[len] = '\0';
425-
substrs[ret] = buf;
426-
++ret;
427-
}
428-
429-
*substrings = substrs;
430-
431-
return ret;
432-
}
433-
434363
#ifndef OS_WINDOWS
435364
FILE *oscap_fopen_with_prefix(const char *prefix, const char *path)
436365
{

src/common/util.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -487,20 +487,6 @@ int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
487487
*/
488488
char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
489489

490-
/**
491-
* Match a regular expression and return substrings.
492-
* Caller is responsible for freeing the returned array.
493-
* @param str subject string
494-
* @param ofs starting offset in str
495-
* @param re compiled regular expression
496-
* @param want_substrs if non-zero, substrings will be returned
497-
* @param substrings contains returned substrings
498-
* @return count of matched substrings, 0 if no match
499-
* negative value on failure
500-
*/
501-
int oscap_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_substrs, char ***substrings);
502-
503-
504490
#ifndef OS_WINDOWS
505491
/**
506492
* Open file for reading with prefix added to its name.

0 commit comments

Comments
 (0)