Commit fc04185d authored by Daniel Elstner's avatar Daniel Elstner
Browse files

:Pattern): Let's be paranoid and use g_new0() rather than plain g_new() to

* src/pcreshell.cc (Pattern::Pattern): Let's be paranoid and
use g_new0() rather than plain g_new() to allocate ovector_.
(Pattern::get_substring_bounds): More paranoia: assert that
the index argument is within the allowed range.
(Pattern::get_substring): Call get_substring_bounds() to get the
boundary offsets in order to take advantage of the error checking.
parent 59efcbe4
2004-05-28 Daniel Elstner <daniel.elstner@gmx.net>
* src/pcreshell.cc (Pattern::Pattern): Let's be paranoid and
use g_new0() rather than plain g_new() to allocate ovector_.
(Pattern::get_substring_bounds): More paranoia: assert that
the index argument is within the allowed range.
(Pattern::get_substring): Call get_substring_bounds() to get the
boundary offsets in order to take advantage of the error checking.
2004-05-28 Daniel Elstner <daniel.elstner@gmx.net>
* src/stringutils.cc (convert_to_ascii): Replace non-ASCII
......
......@@ -127,7 +127,7 @@ Pattern::Pattern(const Glib::ustring& regex, CompileOptions options)
g_assert(capture_count >= 0);
ovecsize_ = 3 * (capture_count + 1);
ovector_ = g_new(int, ovecsize_);
ovector_ = g_new0(int, ovecsize_);
}
Pattern::~Pattern()
......@@ -153,18 +153,19 @@ int Pattern::match(const Glib::ustring& subject, int offset, MatchOptions option
std::pair<int,int> Pattern::get_substring_bounds(int index) const
{
return std::pair<int,int>(ovector_[2 * index], ovector_[2 * index + 1]);
g_return_val_if_fail(3 * index < ovecsize_, std::make_pair(-1, -1));
return std::make_pair(ovector_[2 * index], ovector_[2 * index + 1]);
}
Glib::ustring Pattern::get_substring(const Glib::ustring& subject, int index) const
{
const int begin = ovector_[2 * index];
const int end = ovector_[2 * index + 1];
const std::pair<int,int> bounds = get_substring_bounds(index);
if (begin >= 0 && begin < end)
if (bounds.first >= 0 && bounds.first < bounds.second)
{
const char *const data = subject.data();
return Glib::ustring(data + begin, data + end);
return Glib::ustring(data + bounds.first, data + bounds.second);
}
return Glib::ustring();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment