58
58
#include " absl/strings/ascii.h"
59
59
#include " absl/strings/escaping.h"
60
60
#include " absl/strings/str_cat.h"
61
+ #include " absl/strings/str_format.h"
61
62
#include " google/protobuf/stubs/stringprintf.h"
62
63
#include " absl/strings/str_join.h"
63
64
#include " absl/strings/str_split.h"
@@ -7675,6 +7676,27 @@ bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet(
7675
7676
return true ;
7676
7677
}
7677
7678
7679
+ namespace {
7680
+ // Helpers for method below
7681
+
7682
+ template <typename T> std::string ValueOutOfRange (
7683
+ absl::string_view type_name, absl::string_view option_name) {
7684
+ return absl::StrFormat (
7685
+ " Value out of range, %d to %d, for %s option \" %s\" ." , \
7686
+ std::numeric_limits<T>::min (), std::numeric_limits<T>::max (),
7687
+ type_name, option_name);
7688
+ }
7689
+
7690
+ template <typename T> std::string ValueMustBeInt (
7691
+ absl::string_view type_name, absl::string_view option_name) {
7692
+ return absl::StrFormat (
7693
+ " Value must be integer, from %d to %d, for %s option \" %s\" ." , \
7694
+ std::numeric_limits<T>::min (), std::numeric_limits<T>::max (),
7695
+ type_name, option_name);
7696
+ }
7697
+
7698
+ } // namespace
7699
+
7678
7700
bool DescriptorBuilder::OptionInterpreter::SetOptionValue (
7679
7701
const FieldDescriptor* option_field, UnknownFieldSet* unknown_fields) {
7680
7702
// We switch on the CppType to validate.
@@ -7683,8 +7705,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
7683
7705
if (uninterpreted_option_->has_positive_int_value ()) {
7684
7706
if (uninterpreted_option_->positive_int_value () >
7685
7707
static_cast <uint64_t >(std::numeric_limits<int32_t >::max ())) {
7686
- return AddValueError (" Value out of range for int32 option \" " +
7687
- option_field->full_name () + " \" ." );
7708
+ return AddValueError (ValueOutOfRange<int32_t >(" int32" , option_field->full_name ()));
7688
7709
} else {
7689
7710
SetInt32 (option_field->number (),
7690
7711
uninterpreted_option_->positive_int_value (),
@@ -7693,25 +7714,22 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
7693
7714
} else if (uninterpreted_option_->has_negative_int_value ()) {
7694
7715
if (uninterpreted_option_->negative_int_value () <
7695
7716
static_cast <int64_t >(std::numeric_limits<int32_t >::min ())) {
7696
- return AddValueError (" Value out of range for int32 option \" " +
7697
- option_field->full_name () + " \" ." );
7717
+ return AddValueError (ValueOutOfRange<int32_t >(" int32" , option_field->full_name ()));
7698
7718
} else {
7699
7719
SetInt32 (option_field->number (),
7700
7720
uninterpreted_option_->negative_int_value (),
7701
7721
option_field->type (), unknown_fields);
7702
7722
}
7703
7723
} else {
7704
- return AddValueError (" Value must be integer for int32 option \" " +
7705
- option_field->full_name () + " \" ." );
7724
+ return AddValueError (ValueMustBeInt<int32_t >(" int32" , option_field->full_name ()));
7706
7725
}
7707
7726
break ;
7708
7727
7709
7728
case FieldDescriptor::CPPTYPE_INT64:
7710
7729
if (uninterpreted_option_->has_positive_int_value ()) {
7711
7730
if (uninterpreted_option_->positive_int_value () >
7712
7731
static_cast <uint64_t >(std::numeric_limits<int64_t >::max ())) {
7713
- return AddValueError (" Value out of range for int64 option \" " +
7714
- option_field->full_name () + " \" ." );
7732
+ return AddValueError (ValueOutOfRange<int64_t >(" int64" , option_field->full_name ()));
7715
7733
} else {
7716
7734
SetInt64 (option_field->number (),
7717
7735
uninterpreted_option_->positive_int_value (),
@@ -7722,27 +7740,22 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
7722
7740
uninterpreted_option_->negative_int_value (),
7723
7741
option_field->type (), unknown_fields);
7724
7742
} else {
7725
- return AddValueError (" Value must be integer for int64 option \" " +
7726
- option_field->full_name () + " \" ." );
7743
+ return AddValueError (ValueMustBeInt<int64_t >(" int64" , option_field->full_name ()));
7727
7744
}
7728
7745
break ;
7729
7746
7730
7747
case FieldDescriptor::CPPTYPE_UINT32:
7731
7748
if (uninterpreted_option_->has_positive_int_value ()) {
7732
7749
if (uninterpreted_option_->positive_int_value () >
7733
7750
std::numeric_limits<uint32_t >::max ()) {
7734
- return AddValueError (" Value out of range for uint32 option \" " +
7735
- option_field->name () + " \" ." );
7751
+ return AddValueError (ValueOutOfRange<uint32_t >(" uint32" , option_field->full_name ()));
7736
7752
} else {
7737
7753
SetUInt32 (option_field->number (),
7738
7754
uninterpreted_option_->positive_int_value (),
7739
7755
option_field->type (), unknown_fields);
7740
7756
}
7741
7757
} else {
7742
- return AddValueError (
7743
- " Value must be non-negative integer for uint32 "
7744
- " option \" " +
7745
- option_field->full_name () + " \" ." );
7758
+ return AddValueError (ValueMustBeInt<uint32_t >(" uint32" , option_field->full_name ()));
7746
7759
}
7747
7760
break ;
7748
7761
@@ -7752,10 +7765,7 @@ bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
7752
7765
uninterpreted_option_->positive_int_value (),
7753
7766
option_field->type (), unknown_fields);
7754
7767
} else {
7755
- return AddValueError (
7756
- " Value must be non-negative integer for uint64 "
7757
- " option \" " +
7758
- option_field->full_name () + " \" ." );
7768
+ return AddValueError (ValueMustBeInt<uint64_t >(" uint64" , option_field->full_name ()));
7759
7769
}
7760
7770
break ;
7761
7771
0 commit comments