@@ -1057,7 +1057,9 @@ const pb::Descriptor* QueryImpl::GetDesc(const descriptor_db::DescSet& desc_set,
1057
1057
std::string::size_type* query_start) {
1058
1058
std::string::size_type i = query.find (' :' , *query_start);
1059
1059
if (i == std::string::npos) {
1060
- throw BadQuery (" invalid protobuf query - expected: [<descriptor_set>:]<message_name>:<path>" );
1060
+ throw BadQuery (
1061
+ " invalid protobuf query - expected: "
1062
+ " [<descriptor_set>:]<message_name>:<path>" );
1061
1063
}
1062
1064
std::string desc_name (query.substr (*query_start, i - *query_start));
1063
1065
*query_start = i + 1 ;
@@ -1075,7 +1077,8 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
1075
1077
const pb::EnumDescriptor** ed,
1076
1078
const std::string& part) {
1077
1079
if (*desc == nullptr ) {
1078
- throw BadQuery (std::string (" query does not refer to a known field: " ) + part);
1080
+ throw BadQuery (std::string (" query does not refer to a known field: " ) +
1081
+ part);
1079
1082
}
1080
1083
1081
1084
if (part.empty ()) {
@@ -1128,8 +1131,8 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
1128
1131
if (fd->is_repeated ()) {
1129
1132
std::string rep_selector = part.substr (field_selector_end);
1130
1133
1131
- bool bracketed =
1132
- rep_selector[ 0 ] == ' [ ' && rep_selector[rep_selector.size () - 1 ] == ' ]' ;
1134
+ bool bracketed = rep_selector. size () > 0 && rep_selector. at ( 0 ) == ' [ ' &&
1135
+ rep_selector[rep_selector.size () - 1 ] == ' ]' ;
1133
1136
bool keys_selector = rep_selector == " |keys" ;
1134
1137
if (!bracketed && !keys_selector) {
1135
1138
throw BadQuery (std::string (
@@ -1207,12 +1210,18 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
1207
1210
1208
1211
if (rep_selector != " *" ) {
1209
1212
size_t end;
1210
- long n = std::stol (rep_selector.c_str (), &end, 10 );
1211
- if (end != rep_selector.size ()) {
1212
- throw BadQuery (std::string (" expected numeric indexer at: " ) + rep_selector);
1213
+ long n;
1214
+ try {
1215
+ n = std::stol (rep_selector.c_str (), &end, 10 );
1216
+ } catch (const std::invalid_argument& e) {
1217
+ throw BadQuery (std::string (" invalid numeric key: " ) + rep_selector);
1218
+ } catch (const std::out_of_range& e) {
1219
+ throw BadQuery (std::string (" numeric key out of range key type: " ) +
1220
+ rep_selector);
1213
1221
}
1214
- if (n < 0 || n > static_cast <long >(std::numeric_limits<int >::max ())) {
1215
- throw BadQuery (std::string (" index out of bounds: " + rep_selector));
1222
+ if (end != rep_selector.size ()) {
1223
+ throw BadQuery (std::string (" expected numeric indexer at: " ) +
1224
+ rep_selector);
1216
1225
}
1217
1226
1218
1227
field_selector->SetWantedIndex (static_cast <int >(n));
@@ -1229,16 +1238,16 @@ T ParseNum(const std::string& s) {
1229
1238
typename std::conditional<std::is_unsigned_v<T>, unsigned long long ,
1230
1239
signed long long >::type;
1231
1240
Tmp tmp;
1232
- if constexpr (std::is_unsigned_v<T>) {
1233
- tmp = std::stoull (s, &end);
1234
- } else {
1235
- tmp = std::stoll (s, &end);
1236
- }
1237
- if (tmp < std::numeric_limits<T>:: min ()) {
1238
- throw BadQuery ( std::string ( " numeric key too small for key type: " ) + s);
1239
- }
1240
- if (tmp > std::numeric_limits<T>:: max () ) {
1241
- throw BadQuery (std::string (" numeric key too large for key type: " ) + s);
1241
+ try {
1242
+ if constexpr ( std::is_unsigned_v<T>) {
1243
+ tmp = std::stoull (s, &end);
1244
+ } else {
1245
+ tmp = std::stoll (s, &end);
1246
+ }
1247
+ } catch ( const std::invalid_argument& e) {
1248
+ throw BadQuery ( std::string ( " invalid numeric key: " ) + s);
1249
+ } catch ( const std::out_of_range& e ) {
1250
+ throw BadQuery (std::string (" numeric key out of range key type: " ) + s);
1242
1251
}
1243
1252
if (end != s.size ()) {
1244
1253
throw BadQuery (std::string (" invalid numeric key: " ) + s);
0 commit comments