[med-svn] [cnrun] 06/10: check for return value of regcomp; use range-based for

andrei zavada hmmr-guest at alioth.debian.org
Sun Sep 22 17:19:15 UTC 2013


This is an automated email from the git hooks/post-receive script.

hmmr-guest pushed a commit to branch master
in repository cnrun.

commit e49a4ec3a949588645b9f8cbfee171023e2dd0e7
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Sun Sep 22 15:31:09 2013 +0300

    check for return value of regcomp; use range-based for
---
 upstream/src/libcn/model-struct.cc |  146 +++++++++++++++++++++---------------
 upstream/src/runner-interpreter.cc |   46 +++++++-----
 2 files changed, 114 insertions(+), 78 deletions(-)

diff --git a/upstream/src/libcn/model-struct.cc b/upstream/src/libcn/model-struct.cc
index d9c82c3..8feecd0 100644
--- a/upstream/src/libcn/model-struct.cc
+++ b/upstream/src/libcn/model-struct.cc
@@ -380,7 +380,7 @@ void
 CNRun::CModel::
 unregister_listener( C_BaseUnit *u)
 {
-	auto U = find( lisn_unit_list.begin(), lisn_unit_list.end(), u);
+	const auto& U = find( lisn_unit_list.begin(), lisn_unit_list.end(), u);
 	if ( U != lisn_unit_list.end() )
 		lisn_unit_list.erase( U);
 }
@@ -667,7 +667,7 @@ add_synapse_species( TUnitType ytype, C_BaseNeuron *src, C_BaseNeuron *tgt,
 
       // consider cloning
 	if ( !(_status & CN_MDL_DONT_COALESCE) && allow_clone && src->_axonal_harbour.size() )
-		for ( auto &L : src->_axonal_harbour )
+		for ( auto& L : src->_axonal_harbour )
 			if ( L->_type == ytype &&
 			     L->is_not_altered() )
 				return L->clone_to_target( tgt, g);
@@ -922,16 +922,19 @@ int
 CNRun::CModel::
 process_listener_tags( const list<STagGroupListener> &Listeners)
 {
-	for ( auto P = Listeners.begin(); P != Listeners.end(); P++ ) {
-		regex_t RE;
-		regcomp( &RE, P->pattern.c_str(), REG_EXTENDED);
+	regex_t RE;
+	for ( auto& P : Listeners ) {
+		if (0 != regcomp( &RE, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB)) {
+			fprintf( stderr, "Invalid regexp in process_listener_tags: \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
 		for_all_units (U) {
 			if ( regexec( &RE, (*U)->_label, 0, 0, 0) == 0 ) {
-				if ( P->enable ) {
-					(*U) -> start_listening( P->bits);
+				if ( P.enable ) {
+					(*U) -> start_listening( P.bits);
 					if ( verbosely > 3 )
 						printf( " (unit \"%s\" listening%s)\n",
-							(*U)->_label, P->bits & CN_ULISTENING_1VARONLY ? ", to one var only" :"");
+							(*U)->_label, P.bits & CN_ULISTENING_1VARONLY ? ", to one var only" :"");
 				} else {
 					(*U) -> stop_listening();
 					if ( verbosely > 3 )
@@ -949,14 +952,17 @@ int
 CNRun::CModel::
 process_spikelogger_tags( const list<STagGroupSpikelogger> &Spikeloggers)
 {
-	for ( auto P = Spikeloggers.begin(); P != Spikeloggers.end(); P++ ) {
-		regex_t RE;
-		regcomp( &RE, P->pattern.c_str(), REG_EXTENDED);
+	regex_t RE;
+	for ( auto& P : Spikeloggers ) {
+		if (0 != regcomp( &RE, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB)) {
+			fprintf( stderr, "Invalid regexp in process_spikelogger_tags: \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
 		for_all_standalone_neurons (N) {
 			if ( regexec( &RE, (*N)->_label, 0, 0, 0) == 0 ) {
-				if ( P->enable ) {
-					bool log_sdf = !(P->period == 0. || P->sigma == 0.);
-					if ( ( log_sdf && !(*N)->enable_spikelogging_service( P->period, P->sigma, P->from))
+				if ( P.enable ) {
+					bool log_sdf = !(P.period == 0. || P.sigma == 0.);
+					if ( ( log_sdf && !(*N)->enable_spikelogging_service( P.period, P.sigma, P.from))
 					     ||
 					     (!log_sdf && !(*N)->enable_spikelogging_service()) ) {
 						fprintf( stderr, "Cannot have \"%s\" log spikes because it is not a conductance-based neuron (of type %s)\n",
@@ -968,14 +974,14 @@ process_spikelogger_tags( const list<STagGroupSpikelogger> &Spikeloggers)
 
 				if ( verbosely > 3 )
 					printf( " (%sabling spike logging for standalone neuron \"%s\")\n",
-						P->enable ? "en" : "dis", (*N)->_label);
+						P.enable ? "en" : "dis", (*N)->_label);
 			}
 		}
 		for_all_hosted_neurons (N) {
 			if ( regexec( &RE, (*N)->_label, 0, 0, 0) == 0 ) {
-				if ( P->enable ) {
-					bool log_sdf = !(P->period == 0. || P->sigma == 0.);
-					if ( ( log_sdf && !(*N)->enable_spikelogging_service( P->period, P->sigma, P->from))
+				if ( P.enable ) {
+					bool log_sdf = !(P.period == 0. || P.sigma == 0.);
+					if ( ( log_sdf && !(*N)->enable_spikelogging_service( P.period, P.sigma, P.from))
 					     ||
 					     (!log_sdf && !(*N)->enable_spikelogging_service()) ) {
 						fprintf( stderr, "Cannot have \"%s\" log spikes because it is not a conductance-based neuron (of type %s)\n",
@@ -987,7 +993,7 @@ process_spikelogger_tags( const list<STagGroupSpikelogger> &Spikeloggers)
 
 				if ( verbosely > 3 )
 					printf( " (%sabling spike logging for hosted neuron \"%s\")\n",
-						P->enable ? "en" : "dis", (*N)->_label);
+						P.enable ? "en" : "dis", (*N)->_label);
 			}
 		}
 	}
@@ -1001,9 +1007,12 @@ CNRun::CModel::
 process_putout_tags( const list<STagGroup> &ToRemove)
 {
       // execute some
-	for ( auto P = ToRemove.begin(); P != ToRemove.end(); P++ ) {
-		regex_t RE;
-		regcomp( &RE, P->pattern.c_str(), REG_EXTENDED);
+	regex_t RE;
+	for ( auto& P : ToRemove ) {
+		if (0 != regcomp( &RE, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB)) {
+			fprintf( stderr, "Invalid regexp in process_putout_tags: \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
 		for_all_units (U) {
 			if ( regexec( &RE, (*U)->_label, 0, 0, 0) == 0 ) {
 				if ( verbosely > 2 )
@@ -1029,9 +1038,12 @@ CNRun::CModel::
 process_decimate_tags( const list<STagGroupDecimate> &ToDecimate)
 {
       // decimate others
-	for ( auto P = ToDecimate.begin(); P != ToDecimate.end(); P++ ) {
-		regex_t RE;
-		regcomp( &RE, P->pattern.c_str(), REG_EXTENDED);
+	regex_t RE;
+	for ( auto& P : ToDecimate ) {
+		if (0 != regcomp( &RE, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB)) {
+			fprintf( stderr, "Invalid regexp in process_decimate_tags: \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
 
 	      // collect group
 		vector<C_BaseUnit*> dcmgroup;
@@ -1041,12 +1053,12 @@ process_decimate_tags( const list<STagGroupDecimate> &ToDecimate)
 		random_shuffle( dcmgroup.begin(), dcmgroup.end());
 
 	      // execute
-		size_t	to_execute = rint( dcmgroup.size() * P->fraction), n = to_execute;
+		size_t	to_execute = rint( dcmgroup.size() * P.fraction), n = to_execute;
 		while ( n-- )
 			delete dcmgroup[n];
 
 		if ( verbosely > 3 )
-			printf( " (decimated %4.1f%% (%zu units) of %s)\n", P->fraction*100, to_execute, P->pattern.c_str());
+			printf( " (decimated %4.1f%% (%zu units) of %s)\n", P.fraction*100, to_execute, P.pattern.c_str());
 
 	}
 
@@ -1064,9 +1076,12 @@ int
 CNRun::CModel::
 process_paramset_static_tags( const list<STagGroupNeuronParmSet> &tags)
 {
-	for ( auto P = tags.begin(); P != tags.end(); P++ ) {
-		regex_t RE;
-		regcomp( &RE, P->pattern.c_str(), REG_EXTENDED);
+	regex_t RE;
+	for ( auto& P : tags ) {
+		if (0 != regcomp( &RE, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB)) {
+			fprintf( stderr, "Invalid regexp in process_paramset_static_tags: \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
 
 		vector<string> current_tag_assigned_labels;
 
@@ -1078,23 +1093,23 @@ process_paramset_static_tags( const list<STagGroupNeuronParmSet> &tags)
 
 			int p_d = -1;
 			C_BaseUnit::TSinkType kind = (C_BaseUnit::TSinkType)-1;
-			if ( (p_d = (*U)->param_idx_by_sym( P->parm.c_str())) > -1 )
+			if ( (p_d = (*U)->param_idx_by_sym( P.parm.c_str())) > -1 )
 				kind = C_BaseUnit::SINK_PARAM;
-			else if ( (p_d = (*U)->var_idx_by_sym( P->parm.c_str())) > -1 )
+			else if ( (p_d = (*U)->var_idx_by_sym( P.parm.c_str())) > -1 )
 				kind = C_BaseUnit::SINK_VAR;
 			if ( p_d == -1 ) {
 				fprintf( stderr, "%s \"%s\" (type \"%s\") has no parameter or variable named \"%s\"\n",
-					 (*U)->class_name(), (*U)->label(), (*U)->species(), P->parm.c_str());
+					 (*U)->class_name(), (*U)->label(), (*U)->species(), P.parm.c_str());
 				continue;
 			}
 
 			switch ( kind ) {
 			case C_BaseUnit::SINK_PARAM:
-				(*U)->param_value(p_d) = P->enable ? P->value : __CNUDT[(*U)->type()].stock_param_values[p_d];
+				(*U)->param_value(p_d) = P.enable ? P.value : __CNUDT[(*U)->type()].stock_param_values[p_d];
 				(*U)->param_changed_hook();
 			    break;
 			case C_BaseUnit::SINK_VAR:
-				(*U)->  var_value(p_d) = P->value;
+				(*U)->  var_value(p_d) = P.value;
 			    break;
 			}
 
@@ -1102,7 +1117,7 @@ process_paramset_static_tags( const list<STagGroupNeuronParmSet> &tags)
 		}
 
 		if ( current_tag_assigned_labels.empty() ) {
-			fprintf( stderr, "No neuron labelled matching \"%s\"\n", P->pattern.c_str());
+			fprintf( stderr, "No neuron labelled matching \"%s\"\n", P.pattern.c_str());
 			return -2;
 		}
 
@@ -1111,7 +1126,7 @@ process_paramset_static_tags( const list<STagGroupNeuronParmSet> &tags)
 			for ( auto S = current_tag_assigned_labels.begin(); S != current_tag_assigned_labels.end(); S++ )
 				printf( "%s%s",
 					(S == current_tag_assigned_labels.begin()) ? "" : ", ", S->c_str());
-			printf( " {%s} = %g\n", P->parm.c_str(), P->value);
+			printf( " {%s} = %g\n", P.parm.c_str(), P.value);
 		}
 	}
 	return 0;
@@ -1125,17 +1140,23 @@ int
 CNRun::CModel::
 process_paramset_static_tags( const list<STagGroupSynapseParmSet> &tags)
 {
-	for ( list<STagGroupSynapseParmSet>::const_iterator P = tags.begin(); P != tags.end(); P++ ) {
+	for ( auto& P : tags ) {
 		regex_t REsrc, REtgt;
-		regcomp( &REsrc, P->pattern.c_str(), REG_EXTENDED);   // P->pattern acting as src
-		regcomp( &REtgt, P->target.c_str(), REG_EXTENDED);
+		if (0 != regcomp( &REsrc, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB) ) {  // P->pattern acting as src
+			fprintf( stderr, "Invalid regexp in process_paramset_static_tags (src): \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
+		if (0 != regcomp( &REtgt, P.target.c_str(), REG_EXTENDED | REG_NOSUB) ) {
+			fprintf( stderr, "Invalid regexp in process_paramset_static_tags (tgt): \"%s\"\n", P.target.c_str());
+			return -1;
+		}
 
 		vector<string> current_tag_assigned_labels;
 
-		bool do_gsyn = (P->parm == "gsyn");
+		bool do_gsyn = (P.parm == "gsyn");
 
 		if ( verbosely > 5 )
-			printf( "== setting %s -> %s {%s} = %g...\n", P->pattern.c_str(), P->target.c_str(), P->parm.c_str(), P->value);
+			printf( "== setting %s -> %s {%s} = %g...\n", P.pattern.c_str(), P.target.c_str(), P.parm.c_str(), P.value);
 
 		for_all_neurons (Us) {
 			if ( regexec( &REsrc, (*Us)->label(), 0, 0, 0) == 0 )
@@ -1149,20 +1170,20 @@ process_paramset_static_tags( const list<STagGroupSynapseParmSet> &tags)
 					continue;
 
 				if ( do_gsyn ) {
-					y->set_g_on_target( *static_cast<C_BaseNeuron*>(*Ut), P->value);
+					y->set_g_on_target( *static_cast<C_BaseNeuron*>(*Ut), P.value);
 					current_tag_assigned_labels.push_back( y->label());
 					continue;
 				}
 
 				int p_d = -1;
 				C_BaseUnit::TSinkType kind = (C_BaseUnit::TSinkType)-1;
-				if ( (p_d = y->param_idx_by_sym( P->parm.c_str())) > -1 )
+				if ( (p_d = y->param_idx_by_sym( P.parm.c_str())) > -1 )
 					kind = C_BaseUnit::SINK_PARAM;
-				else if ( (p_d = y->var_idx_by_sym( P->parm.c_str())) > -1 )
+				else if ( (p_d = y->var_idx_by_sym( P.parm.c_str())) > -1 )
 					kind = C_BaseUnit::SINK_VAR;
 				if ( p_d == -1 ) {
 					fprintf( stderr, "%s \"%s\" (type \"%s\") has no parameter or variable named \"%s\"\n",
-						 y->class_name(), y->label(), y->species(), P->parm.c_str());
+						 y->class_name(), y->label(), y->species(), P.parm.c_str());
 					continue;
 				}
 
@@ -1171,11 +1192,11 @@ process_paramset_static_tags( const list<STagGroupSynapseParmSet> &tags)
 					if ( y->_targets.size() > 1 ) {
 						y = y->make_clone_independent( static_cast<C_BaseNeuron*>(*Ut));  // lest brethren synapses to other targets be clobbered
 					}
-					y->param_value(p_d) = P->enable ? P->value : __CNUDT[y->type()].stock_param_values[p_d];
+					y->param_value(p_d) = P.enable ? P.value : __CNUDT[y->type()].stock_param_values[p_d];
 					y->param_changed_hook();
 				    break;
 				case C_BaseUnit::SINK_VAR:
-					y->  var_value(p_d) = P->value;
+					y->  var_value(p_d) = P.value;
 				    break;
 				}
 
@@ -1183,7 +1204,7 @@ process_paramset_static_tags( const list<STagGroupSynapseParmSet> &tags)
 			}
 		}
 		if ( current_tag_assigned_labels.empty() ) {
-			fprintf( stderr, "No synapse connecting any of \"%s\" to \"%s\"\n", P->pattern.c_str(), P->target.c_str());
+			fprintf( stderr, "No synapse connecting any of \"%s\" to \"%s\"\n", P.pattern.c_str(), P.target.c_str());
 			return -2;
 		}
 
@@ -1192,7 +1213,7 @@ process_paramset_static_tags( const list<STagGroupSynapseParmSet> &tags)
 			for ( auto S = current_tag_assigned_labels.begin(); S != current_tag_assigned_labels.end(); S++ )
 				printf( "%s%s",
 					(S == current_tag_assigned_labels.begin()) ? "" : ", ", S->c_str());
-			printf( " {%s} = %g\n", P->parm.c_str(), P->value);
+			printf( " {%s} = %g\n", P.parm.c_str(), P.value);
 		}
 	}
 
@@ -1242,9 +1263,12 @@ int
 CNRun::CModel::
 process_paramset_source_tags( const list<STagGroupSource> &tags)
 {
-	for ( list<STagGroupSource>::const_iterator P = tags.begin(); P != tags.end(); P++ ) {
-		regex_t RE;
-		regcomp( &RE, P->pattern.c_str(), REG_EXTENDED);
+	regex_t RE;
+	for ( auto& P : tags ) {
+		if (0 != regcomp( &RE, P.pattern.c_str(), REG_EXTENDED | REG_NOSUB)) {
+			fprintf( stderr, "Invalid regexp in process_paramset_source_tags: \"%s\"\n", P.pattern.c_str());
+			return -1;
+		}
 
 		for_all_units (U) {
 			if ( regexec( &RE, (*U)->label(), 0, 0, 0) == 0 )
@@ -1252,26 +1276,26 @@ process_paramset_source_tags( const list<STagGroupSource> &tags)
 
 			int p_d = -1;
 			C_BaseUnit::TSinkType kind = (C_BaseUnit::TSinkType)-1;
-			if ( (p_d = (*U)->param_idx_by_sym( P->parm.c_str())) > -1 )
+			if ( (p_d = (*U)->param_idx_by_sym( P.parm.c_str())) > -1 )
 				kind = C_BaseUnit::SINK_PARAM;
-			else if ( (p_d = (*U)->var_idx_by_sym( P->parm.c_str())) > -1 )
+			else if ( (p_d = (*U)->var_idx_by_sym( P.parm.c_str())) > -1 )
 				kind = C_BaseUnit::SINK_VAR;
 			if ( p_d == -1 ) {
 				fprintf( stderr, "%s \"%s\" (type \"%s\") has no parameter or variable named \"%s\"\n",
-					 (*U)->class_name(), (*U)->label(), (*U)->species(), P->parm.c_str());
+					 (*U)->class_name(), (*U)->label(), (*U)->species(), P.parm.c_str());
 				continue;
 			}
 
-			if ( P->enable ) {
-				(*U) -> attach_source( P->source, kind, p_d);
+			if ( P.enable ) {
+				(*U) -> attach_source( P.source, kind, p_d);
 				if ( verbosely > 3 )
 					printf( "Connected source \"%s\" to \"%s\"{%s}\n",
-						P->source->name.c_str(), (*U)->label(), P->parm.c_str());
+						P.source->name.c_str(), (*U)->label(), P.parm.c_str());
 			} else {
-				(*U) -> detach_source( P->source, kind, p_d);
+				(*U) -> detach_source( P.source, kind, p_d);
 				if ( verbosely > 3 )
 					printf( "Disconnected source \"%s\" from \"%s\"{%s}\n",
-						P->source->name.c_str(), (*U)->label(), P->parm.c_str());
+						P.source->name.c_str(), (*U)->label(), P.parm.c_str());
 			}
 		}
 	}
diff --git a/upstream/src/runner-interpreter.cc b/upstream/src/runner-interpreter.cc
index ee4b895..610cac7 100644
--- a/upstream/src/runner-interpreter.cc
+++ b/upstream/src/runner-interpreter.cc
@@ -280,8 +280,11 @@ do_single_cmd( const char* raw,
 		CHECK_MODEL;
 		if ( !operand )
 			operand = const_cast<char*>(".*");
-        regex_t RE;
-        regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB);
+		regex_t RE;
+		if ( 0 != regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB) ) {
+			LOG( fname, lineno, -1, "Invalid regexp for `reset_state_units' arg");
+			return CN_INTERP_PARSEERROR;
+		}
 		size_t cnt = 0;
 		for_model_units (Model,U)
 			if ( regexec( &RE, (*U)->label(), 0, 0, 0) == 0 ) {
@@ -686,8 +689,11 @@ do_single_cmd( const char* raw,
 		if ( !operand )
 			operand = const_cast<char*>(".*");
 
-        regex_t RE;
-        regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB);
+		regex_t RE;
+		if ( 0 != regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB) ) {
+			LOG( fname, lineno, -1, "Invalid regexp for `show_units' arg");
+			return CN_INTERP_PARSEERROR;
+		}
 		size_t cnt = 0;
 		for_model_units (Model,U)
 			if ( regexec( &RE, (*U)->label(), 0, 0, 0) == 0 ) {
@@ -724,18 +730,21 @@ do_single_cmd( const char* raw,
 	} else if ( strcmp( cmd, cnrun_cmd[CNCMD_show_vars]) == 0 ) {
 		if ( !operand )
 			operand = const_cast<char*>(".*");
-        regex_t RE;
-        regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB);
+		regex_t RE;
+		if ( 0 != regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB) ) {
+			LOG( fname, lineno, -1, "Invalid regexp for `show_vars' arg");
+			return CN_INTERP_PARSEERROR;
+		}
 		size_t	cnt = 0;
 		size_t	longest_id = 0;
-		for ( list<SVariable>::iterator V = varlist.begin(); V != varlist.end(); V++ )
-            if ( regexec( &RE, V->name, 0, 0, 0) == 0 )
-				if ( longest_id < strlen( V->name) )
-					longest_id = strlen( V->name);
-		for ( list<SVariable>::iterator V = varlist.begin(); V != varlist.end(); V++ )
-            if ( regexec( &RE, V->name, 0, 0, 0) == 0 ) {
-				Log->msg( 0, nullptr, "  %*s = %g", longest_id, V->name, V->value);
-				cnt++;
+		for ( auto& V : varlist )
+			if ( regexec( &RE, V.name, 0, 0, 0) == 0 )
+				if ( longest_id < strlen( V.name) )
+					longest_id = strlen( V.name);
+		for ( auto& V : varlist )
+			if ( regexec( &RE, V.name, 0, 0, 0) == 0 ) {
+				Log->msg( 0, nullptr, "  %*s = %g", longest_id, V.name, V.value);
+				++cnt;
 			}
 		if ( cnt > 1 )
 			Log->msg_( 0, nullptr, "---------- %u variables\n", cnt);
@@ -745,10 +754,13 @@ do_single_cmd( const char* raw,
 		if ( !operand )
 			varlist.clear();
 		else {
-            regex_t RE;
-            regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB);
+			regex_t RE;
+			if ( 0 != regcomp( &RE, operand, REG_EXTENDED | REG_NOSUB) ) {
+				LOG( fname, lineno, -1, "Invalid regexp for `clear_vars' arg");
+				return CN_INTERP_PARSEERROR;
+			}
 			for ( list<SVariable>::iterator V = varlist.begin(); V != varlist.end(); V++ )
-                if ( regexec( &RE, V->name, 0, 0, 0) == 0 ) {
+				if ( regexec( &RE, V->name, 0, 0, 0) == 0 ) {
 					varlist.erase( V);
 					break;
 				}

-- 
Alioth's /git/debian-med/git-commit-notice on /srv/git.debian.org/git/debian-med/cnrun.git



More information about the debian-med-commit mailing list