Commit 36d302a5a784d1684b5264457e9464095415f543

Authored by Jürgen Knödlseder
1 parent baa16045

Check parameter validity before returning a value.

The parameter validity is checked before returning a value. This ensures that invalid values are not inadvertedly passed to the executable. The isvalid() method can be used to ensure that a parameter is valid before accessing it.
include/GPar.hpp
... ... @@ -113,6 +113,7 @@ protected:
113 113 void query(void);
114 114 void stop_query(void);
115 115 std::string par_type_string(const std::string& type) const;
  116 + std::string par_status_string(void) const;
116 117  
117 118 // Protected data members
118 119 bool m_update; //!< Signal value updating
... ...
src/app/GPar.cpp
... ... @@ -461,6 +461,13 @@ std::string GPar::string(void)
461 461 // Query parameter
462 462 query();
463 463  
  464 + // Check if parameter is valid
  465 + if (m_status != ST_VALID) {
  466 + throw GException::par_error(G_STRING_GET, name(),
  467 + "is "+par_status_string()+". Please specify"
  468 + " a valid parameter value.");
  469 + }
  470 +
464 471 // Return value
465 472 return m_value;
466 473 }
... ... @@ -489,6 +496,13 @@ std::string GPar::filename(void)
489 496 // Query parameter
490 497 query();
491 498  
  499 + // Check if parameter is valid
  500 + if (m_status != ST_VALID) {
  501 + throw GException::par_error(G_FILENAME_GET, name(),
  502 + "is "+par_status_string()+". Please specify"
  503 + " a valid parameter value.");
  504 + }
  505 +
492 506 // Return value
493 507 return (expand_env(m_value));
494 508 }
... ... @@ -516,6 +530,13 @@ bool GPar::boolean(void)
516 530 // Query parameter
517 531 query();
518 532  
  533 + // Check if parameter is valid
  534 + if (m_status != ST_VALID) {
  535 + throw GException::par_error(G_BOOLEAN_GET, name(),
  536 + "is "+par_status_string()+". Please specify"
  537 + " a valid parameter value.");
  538 + }
  539 +
519 540 // Convert boolean value to upper case
520 541 std::string uvalue = toupper(m_value);
521 542  
... ... @@ -550,6 +571,13 @@ int GPar::integer(void)
550 571 // Query parameter
551 572 query();
552 573  
  574 + // Check if parameter is valid
  575 + if (m_status != ST_VALID) {
  576 + throw GException::par_error(G_INTEGER_GET, name(),
  577 + "is "+par_status_string()+". Please specify"
  578 + " a valid parameter value.");
  579 + }
  580 +
553 581 // Set result
554 582 int result = toint(m_value);
555 583  
... ... @@ -580,6 +608,13 @@ double GPar::real(void)
580 608 // Query parameter
581 609 query();
582 610  
  611 + // Check if parameter is valid
  612 + if (m_status != ST_VALID) {
  613 + throw GException::par_error(G_REAL_GET, name(),
  614 + "is "+par_status_string()+". Please specify"
  615 + " a valid parameter value.");
  616 + }
  617 +
583 618 // Set result
584 619 double result = todouble(m_value);
585 620  
... ... @@ -643,6 +678,9 @@ bool GPar::isfilename(void) const
643 678 ***************************************************************************/
644 679 bool GPar::isvalid(void) const
645 680 {
  681 + // Query parameter
  682 + query();
  683 +
646 684 // Return validity
647 685 return (m_status == ST_VALID);
648 686 }
... ... @@ -653,6 +691,9 @@ bool GPar::isvalid(void) const
653 691 ***************************************************************************/
654 692 bool GPar::isundefined(void) const
655 693 {
  694 + // Query parameter
  695 + query();
  696 +
656 697 // Return validity
657 698 return (m_status == ST_UNDEFINED);
658 699 }
... ... @@ -663,6 +704,9 @@ bool GPar::isundefined(void) const
663 704 ***************************************************************************/
664 705 bool GPar::isnotanumber(void) const
665 706 {
  707 + // Query parameter
  708 + query();
  709 +
666 710 // Return validity
667 711 return (m_status == ST_NAN);
668 712 }
... ... @@ -992,7 +1036,7 @@ void GPar::set_value(const std::string&amp; value)
992 1036 lvalue == "none" ||
993 1037 lvalue == "undef" ||
994 1038 lvalue == "undefined") {
995   - m_value = "0";
  1039 + m_value = value;
996 1040 m_status = ST_UNDEFINED;
997 1041 }
998 1042 else if (lvalue == "inf" ||
... ... @@ -1015,13 +1059,13 @@ void GPar::set_value(const std::string&amp; value)
1015 1059 lvalue == "none" ||
1016 1060 lvalue == "undef" ||
1017 1061 lvalue == "undefined") {
1018   - m_value = "0";
  1062 + m_value = value;
1019 1063 m_status = ST_UNDEFINED;
1020 1064 }
1021 1065 else if (lvalue == "inf" ||
1022 1066 lvalue == "infinity" ||
1023 1067 lvalue == "nan") {
1024   - m_value = "0.0";
  1068 + m_value = value;
1025 1069 m_status = ST_NAN;
1026 1070 }
1027 1071 else {
... ... @@ -1052,8 +1096,6 @@ void GPar::set_value(const std::string&amp; value)
1052 1096 *
1053 1097 * This method queries the parameter from the stardard input if it is needed
1054 1098 * to be input by the user.
1055   - *
1056   - * @todo Add environment variable substitution.
1057 1099 ***************************************************************************/
1058 1100 void GPar::query(void)
1059 1101 {
... ... @@ -1093,7 +1135,8 @@ void GPar::query(void)
1093 1135  
1094 1136 // Update value if value is not the default
1095 1137 if (value.length() > 0) {
1096   - m_value = value;
  1138 + //m_value = value;
  1139 + set_value(value);
1097 1140 m_update = true;
1098 1141 }
1099 1142  
... ... @@ -1160,3 +1203,38 @@ std::string GPar::par_type_string(const std::string&amp; type) const
1160 1203 // Return type string
1161 1204 return type_string;
1162 1205 }
  1206 +
  1207 +
  1208 +/***********************************************************************//**
  1209 + * @brief Return status string
  1210 + *
  1211 + * @return Returns the parameter status in human readable form.
  1212 + ***************************************************************************/
  1213 +std::string GPar::par_status_string(void) const
  1214 +{
  1215 + // Allocate status string
  1216 + std::string status;
  1217 +
  1218 + // Set status
  1219 + switch (m_status) {
  1220 + case ST_VALID:
  1221 + status.append("valid");
  1222 + break;
  1223 + case ST_UNDEFINED:
  1224 + status.append("undefined");
  1225 + break;
  1226 + case ST_NAN:
  1227 + status.append("NaN");
  1228 + break;
  1229 + case ST_UNDERFLOW:
  1230 + status.append("underflow");
  1231 + break;
  1232 + case ST_OVERFLOW:
  1233 + status.append("overflow");
  1234 + break;
  1235 + }
  1236 +
  1237 + // Return status
  1238 + return status;
  1239 +}
  1240 +
... ...