bes  Updated for version 3.20.5
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 
101 int
102 BESRegex::match(const char* s, int len, int pos)
103 {
104  // TODO re-implement using auto_ptr or unique_ptr. jhrg 7/27/18
105  regmatch_t *pmatch = new regmatch_t[len+1];
106  string ss = s;
107 
108  int result = regexec(static_cast<regex_t*>(d_preg),
109  ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
110  int matchnum;
111  if (result == REG_NOMATCH)
112  matchnum = -1;
113  else
114  matchnum = pmatch[0].rm_eo - pmatch[0].rm_so;
115 
116  delete[] pmatch; pmatch = 0;
117 
118  return matchnum;
119 }
120 
131 int
132 BESRegex::search(const char* s, int len, int& matchlen, int pos)
133 {
134  // sanitize allocation
135  if (!BESScrub::size_ok(sizeof(regmatch_t), len+1))
136  return -1;
137 
138  // alloc space for len matches, which is theoretical max.
139  // Problem: If somehow 'len' is very large - say the size of a 32-bit int,
140  // then len+1 is a an integer overflow and this might be exploited by
141  // an attacker. It's not likely there will be more than a handful of
142  // matches, so I am going to limit this value to 32766. jhrg 3/4/09
143  if (len > 32766)
144  return -1;
145 
146  regmatch_t *pmatch = new regmatch_t[len+1];
147  string ss = s;
148 
149  int result = regexec(static_cast<regex_t*>(d_preg),
150  ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
151  if (result == REG_NOMATCH) {
152  delete[] pmatch; pmatch = 0;
153  return -1;
154  }
155 
156  // Match found, find the first one (pmatch lists the longest first)
157  int m = 0;
158  for (int i = 1; i < len; ++i)
159  if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
160  m = i;
161 
162  matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
163  int matchpos = pmatch[m].rm_so;
164 
165  delete[] pmatch; pmatch = 0;
166  return matchpos;
167 }
168 
BESRegex(const char *t)
Definition: BESRegex.cc:83
exception thrown if inernal error encountered
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: BESRegex.cc:102
int search(const char *s, int len, int &matchlen, int pos=0)
How much of the string does the pattern matche.
Definition: BESRegex.cc:132
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:66