[Forensics-changes] [yara] 89/415: Fix bug in pcre_exec invocation
Hilko Bengen
bengen at moszumanska.debian.org
Thu Apr 3 05:42:49 UTC 2014
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to branch debian
in repository yara.
commit 031e8579922118e638feee59a262c67f745716db
Author: Victor M. Alvarez <plusvic at gmail.com>
Date: Tue Mar 22 13:32:36 2011 +0000
Fix bug in pcre_exec invocation
---
libyara/regex/regex-pcre.c | 138 +++++++++++++++++++++++++--------------------
libyara/regex/regex-re2.cc | 119 +++++++++++++++++++++-----------------
2 files changed, 142 insertions(+), 115 deletions(-)
diff --git a/libyara/regex/regex-pcre.c b/libyara/regex/regex-pcre.c
index 6000bfa..9cdbbde 100644
--- a/libyara/regex/regex-pcre.c
+++ b/libyara/regex/regex-pcre.c
@@ -20,48 +20,55 @@ GNU General Public License for more details.
#include "../yara.h"
-int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size) {
-
- int ovector[3];
- int result = -1;
- char *s;
-
- if (!regex || buffer_size == 0)
- return 0;
-
- result = pcre_exec((pcre*)regex->regexp, /* the compiled pattern */
- (pcre_extra*)regex->extra, /* extra data */
- (char*) buffer, /* the subject string */
- buffer_size, /* the length of the subject */
- 0, /* start at offset 0 in the subject */
- 0, /* default options */
- ovector, /* output vector for substring information */
- sizeof(ovector)); /* number of elements in the output vector */
- if (result >= 0) {
- result = pcre_get_substring(
- (char*) buffer, ovector, 1, 0, (const char**) &s);
- if (result != PCRE_ERROR_NOMEMORY && result != PCRE_ERROR_NOSUBSTRING) {
- pcre_free_substring(s);
- return result;
+int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size)
+{
+ int ovector[3];
+ int result = -1;
+ char *s;
+
+ if (!regex || buffer_size == 0)
+ return 0;
+
+ result = pcre_exec( (pcre*)regex->regexp, /* the compiled pattern */
+ (pcre_extra*)regex->extra, /* extra data */
+ (char*) buffer, /* the subject string */
+ buffer_size, /* the length of the subject */
+ 0, /* start at offset 0 in the subject */
+ 0, /* default options */
+ ovector, /* output vector for substring information */
+ sizeof(ovector)/sizeof(int)); /* number of elements in the output vector */
+
+ if (result >= 0)
+ {
+ result = pcre_get_substring((char*) buffer, ovector, 1, 0, (const char**) &s);
+
+ if (result != PCRE_ERROR_NOMEMORY && result != PCRE_ERROR_NOSUBSTRING)
+ {
+ pcre_free_substring(s);
+ return result;
+ }
}
- }
- return -1;
+
+ return -1;
}
-void regex_free(REGEXP* regex) {
- if (!regex)
- return;
+void regex_free(REGEXP* regex)
+{
+ if (!regex)
+ return;
- if (regex->regexp) {
- pcre_free((pcre*)regex->regexp);
- regex->regexp = NULL;
- }
+ if (regex->regexp)
+ {
+ pcre_free((pcre*)regex->regexp);
+ regex->regexp = NULL;
+ }
- if (regex->extra) {
- pcre_free((pcre_extra*)regex->extra);
- regex->extra = NULL;
- }
+ if (regex->extra)
+ {
+ pcre_free((pcre_extra*)regex->extra);
+ regex->extra = NULL;
+ }
}
@@ -71,34 +78,41 @@ int regex_compile(REGEXP* output,
int case_insensitive,
char* error_message,
size_t error_message_size,
- int* error_offset) {
+ int* error_offset)
+{
+
+ int pcre_options = 0;
+ char *pcre_error = NULL;
+
+ if (!output || !pattern)
+ return 0;
+
+ memset(output, '\0', sizeof(REGEXP));
+
+ if (anchored)
+ pcre_options |= PCRE_ANCHORED;
+
+ if (case_insensitive)
+ pcre_options |= PCRE_CASELESS;
+
+ output->regexp = (pcre*) pcre_compile(pattern, pcre_options, (const char **)&pcre_error, error_offset, NULL);
- int pcre_options = 0;
- char *pcre_error = NULL;
-
- if (!output || !pattern)
- return 0;
-
- memset(output, '\0', sizeof(REGEXP));
-
- if (anchored)
- pcre_options |= PCRE_ANCHORED;
- if (case_insensitive)
- pcre_options |= PCRE_CASELESS;
-
- output->regexp = (pcre*) pcre_compile(
- pattern, pcre_options, (const char **)&pcre_error, error_offset, NULL);
- if (output->regexp != NULL) {
- output->extra = (pcre_extra *)pcre_study(
+ if (output->regexp != NULL)
+ {
+ output->extra = (pcre_extra *)pcre_study(
output->regexp, 0, (const char **)error_message);
- } else {
- if (error_message && error_message_size) {
- strncpy(error_message, pcre_error, error_message_size - 1);
- error_message[error_message_size - 1] = '\0';
+ }
+ else
+ {
+ if (error_message && error_message_size)
+ {
+ strncpy(error_message, pcre_error, error_message_size - 1);
+ error_message[error_message_size - 1] = '\0';
+ }
+
+ // TODO: Handle fatal error here, consistently with how yara would.
+ return 0;
}
- // TODO: Handle fatal error here, consistently with how yara would.
- return 0;
- }
- return 1;
+ return 1;
}
diff --git a/libyara/regex/regex-re2.cc b/libyara/regex/regex-re2.cc
index f1f8ae8..9c4f316 100644
--- a/libyara/regex/regex-re2.cc
+++ b/libyara/regex/regex-re2.cc
@@ -20,35 +20,39 @@ GNU General Public License for more details.
#include <re2/stringpiece.h>
#include "yara.h"
+int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size)
+{
+ if (!regex || buffer_size == 0)
+ return 0;
-int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size) {
- if (!regex || buffer_size == 0)
- return 0;
+ re2::StringPiece data(buffer, buffer_size);
+ re2::StringPiece substring;
+ re2::RE2::Anchor anchor = re2::RE2::UNANCHORED;
- re2::StringPiece data(buffer, buffer_size);
- re2::StringPiece substring;
- re2::RE2::Anchor anchor = re2::RE2::UNANCHORED;
- if (regex->re2_anchored)
- anchor = re2::RE2::ANCHOR_START;
+ if (regex->re2_anchored)
+ anchor = re2::RE2::ANCHOR_START;
- re2::RE2* re_ptr = (re2::RE2*) regex->regexp;
+ re2::RE2* re_ptr = (re2::RE2*) regex->regexp;
- if (re_ptr->Match(data, 0, data.size()-1, anchor, &substring, 1)) {
- return substring.size();
- }
- return -1;
+ if (re_ptr->Match(data, 0, data.size()-1, anchor, &substring, 1))
+ {
+ return substring.size();
+ }
+
+ return -1;
}
-void regex_free(REGEXP* regex) {
- if (!regex)
- return;
-
- if (regex->regexp) {
- delete (re2::RE2*) regex->regexp;
- regex->regexp = NULL;
- }
+void regex_free(REGEXP* regex)
+{
+ if (!regex)
+ return;
+ if (regex->regexp)
+ {
+ delete (re2::RE2*) regex->regexp;
+ regex->regexp = NULL;
+ }
}
@@ -58,37 +62,46 @@ int regex_compile(REGEXP* output,
int case_insensitive,
char* error_message,
size_t error_message_size,
- int* error_offset) {
- if (!output || !pattern)
- return 0;
-
- memset(output, '\0', sizeof(REGEXP));
-
- RE2::Options options;
- options.set_log_errors(false);
-
- if (case_insensitive)
- options.set_case_sensitive(false);
- if (anchored)
- output->re2_anchored = anchored;
-
- re2::StringPiece string_piece_pattern(pattern);
- output->regexp = (void *)new RE2(string_piece_pattern, options);
- if (output->regexp == NULL) {
- // TODO: Handle fatal error here, consistently with how yara would.
- return 0;
- }
-
- re2::RE2* re_ptr = (re2::RE2*)output->regexp;
- if (!re_ptr->ok()) {
- if (error_message && error_message_size) {
- strncpy(error_message, re_ptr->error().c_str(), error_message_size - 1);
- error_message[error_message_size - 1] = '\0';
+ int* error_offset)
+{
+ if (!output || !pattern)
+ return 0;
+
+ memset(output, '\0', sizeof(REGEXP));
+
+ RE2::Options options;
+ options.set_log_errors(false);
+
+ if (case_insensitive)
+ options.set_case_sensitive(false);
+
+ if (anchored)
+ output->re2_anchored = anchored;
+
+ re2::StringPiece string_piece_pattern(pattern);
+ output->regexp = (void *)new RE2(string_piece_pattern, options);
+
+ if (output->regexp == NULL)
+ {
+ // TODO: Handle fatal error here, consistently with how yara would.
+ return 0;
+ }
+
+ re2::RE2* re_ptr = (re2::RE2*)output->regexp;
+
+ if (!re_ptr->ok())
+ {
+ if (error_message && error_message_size)
+ {
+ strncpy(error_message, re_ptr->error().c_str(), error_message_size - 1);
+ error_message[error_message_size - 1] = '\0';
+ }
+
+ *error_offset = re_ptr->error().find(pattern);
+ delete re_ptr;
+ output->regexp = NULL;
+ return 0;
}
- *error_offset = re_ptr->error().find(pattern);
- delete re_ptr;
- output->regexp = NULL;
- return 0;
- }
- return 1;
+
+ return 1;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git
More information about the forensics-changes
mailing list