bes  Updated for version 3.20.6
BESRegex.cc
1 // BESRegex.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 // and James Gallagher <jgallagher@gso.uri.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // pwest Patrick West <pwest@ucar.edu>
32 // jgarcia Jose Garcia <jgarcia@ucar.edu>
33 // jimg James Gallagher <jgallagher@gso.uri.edu>
34 
35 #include <config.h>
36 
37 #ifndef WIN32
38 #include <alloca.h>
39 #endif
40 
41 #include <sys/types.h>
42 #include <regex.h>
43 
44 #include <cstdlib>
45 #include <new>
46 #include <string>
47 #include <stdexcept>
48 
49 #include "BESRegex.h"
50 #include "BESInternalError.h"
51 #include "BESScrub.h"
52 
53 using namespace std;
54 
55 void
56 BESRegex::init(const char *t)
57 {
58  d_preg = static_cast<void*>(new regex_t);
59  int result = regcomp(static_cast<regex_t*>(d_preg), t, REG_EXTENDED);
60 
61  if (result != 0) {
62  size_t msg_len = regerror(result, static_cast<regex_t*>(d_preg), static_cast<char*>(NULL),
63  static_cast<size_t>(0));
64  char *msg = new char[msg_len + 1];
65  regerror(result, static_cast<regex_t*>(d_preg), msg, msg_len);
66  string err = string("BESRegex error: ") + string(msg);
67  BESInternalError e(err, __FILE__, __LINE__);
68  delete[] msg;
69  throw e;
70  }
71 }
72 
73 BESRegex::~BESRegex()
74 {
75  regfree(static_cast<regex_t*>(d_preg));
76  delete static_cast<regex_t*>(d_preg); d_preg = 0;
77 
78 }
79 
83 BESRegex::BESRegex(const char* t)
84 {
85  init(t);
86 }
87 
90 BESRegex::BESRegex(const char* t, int)
91 {
92  init(t);
93 }
94 
104 int
105 BESRegex::match(const char* s, int len, int pos)
106 {
107  // TODO re-implement using auto_ptr or unique_ptr. jhrg 7/27/18
108  regmatch_t *pmatch = new regmatch_t[len+1];
109  string ss = s;
110 
111  int result = regexec(static_cast<regex_t*>(d_preg),
112  ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
113  int matchnum;
114  if (result == REG_NOMATCH)
115  matchnum = -1; //returns -1 due to function being able to match strings of 0 length
116  else
117  matchnum = pmatch[0].rm_eo - pmatch[0].rm_so;
118 
119  delete[] pmatch; pmatch = 0;
120 
121  return matchnum;
122 }
123 
134 int
135 BESRegex::search(const char* s, int len, int& matchlen, int pos)
136 {
137  // sanitize allocation
138  if (!BESScrub::size_ok(sizeof(regmatch_t), len+1))
139  return -1;
140 
141  // alloc space for len matches, which is theoretical max.
142  // Problem: If somehow 'len' is very large - say the size of a 32-bit int,
143  // then len+1 is a an integer overflow and this might be exploited by
144  // an attacker. It's not likely there will be more than a handful of
145  // matches, so I am going to limit this value to 32766. jhrg 3/4/09
146  if (len > 32766)
147  return -1;
148 
149  regmatch_t *pmatch = new regmatch_t[len+1];
150  string ss = s;
151 
152  int result = regexec(static_cast<regex_t*>(d_preg),
153  ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
154  if (result == REG_NOMATCH) {
155  delete[] pmatch; pmatch = 0;
156  return -1;
157  }
158 
159  // Match found, find the first one (pmatch lists the longest first)
160  int m = 0;
161  for (int i = 1; i < len; ++i)
162  if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
163  m = i;
164 
165  matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
166  int matchpos = pmatch[m].rm_so;
167 
168  delete[] pmatch; pmatch = 0;
169  return matchpos;
170 }
171 
BESScrub::size_ok
static bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array.
Definition: BESScrub.cc:68
BESRegex::BESRegex
BESRegex(const char *t)
Definition: BESRegex.cc:83
BESRegex::search
int search(const char *s, int len, int &matchlen, int pos=0)
How much of the string does the pattern matche.
Definition: BESRegex.cc:135
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESRegex::match
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: BESRegex.cc:105