bes  Updated for version 3.20.10
BESScrub.cc
1 // BESScrub.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 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <memory>
36 #include <limits.h>
37 
38 #include "BESScrub.h"
39 #include "BESRegex.h"
40 
41 using std::string;
42 using std::unique_ptr;
43 
46 
55 bool
56 BESScrub::command_line_arg_ok( const string &arg )
57 {
58  if( arg.length() > 255 )
59  return false ;
60 
61  return true ;
62 }
63 
69 bool
70 BESScrub::size_ok(unsigned int sz, unsigned int nelem)
71 {
72  return (sz > 0 && nelem < UINT_MAX / sz);
73 }
74 
91 bool
92 BESScrub::pathname_ok(const string &path, bool strict)
93 {
94  if (path.length() > 255)
95  return false;
96 
97  if (strict) {
98  const BESRegex name("[[:alpha:][:digit:]_./-]+");
99  return name.match(path) == static_cast<int>(path.length());
100  }
101  else {
102  const BESRegex name("[:print:]+");
103  return name.match(path) == static_cast<int>(path.length());
104  }
105 
106 #if 0
107  BESRegex name("[[:alpha:][:digit:]_./-]+");
108  if (!strict)
109  name = "[:print:]+";
110 
111  string::size_type len = path.length() ;
112  int ret = name.match( path.c_str(), len ) ;
113  if( ret != static_cast<int>(len) )
114  return false ;
115  return true ;
116 #endif
117 }
118 
120 
Regular expression matching.
Definition: BESRegex.h:53
int match(const char *s, int len, int pos=0) const
Does the pattern match.
Definition: BESRegex.cc:127
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:70
static bool pathname_ok(const std::string &path, bool strict)
Does the string name a potentailly valid pathname? Test the given pathname to verfiy that it is a val...
Definition: BESScrub.cc:92
static bool command_line_arg_ok(const std::string &arg)
sanitize command line arguments
Definition: BESScrub.cc:56