diff --git a/sPuReMD/src/analyze.c b/sPuReMD/src/analyze.c index 9fcd54f757bdb0682cb3a327bd9d8770124c9354..675e900718cdf06c193cc1e7a7bfd6f1aad90ff1 100644 --- a/sPuReMD/src/analyze.c +++ b/sPuReMD/src/analyze.c @@ -21,6 +21,8 @@ #include "analyze.h" +#include <ctype.h> + #include "box.h" #include "list.h" #include "tool_box.h" @@ -580,8 +582,12 @@ void Analyze_Fragments( reax_system *system, control_params *control, /* output the results of fragment analysis */ for ( i = 0; i < num_fragment_types; ++i ) - if ( strlen(fragments[i]) ) + { + if ( strnlen(fragments[i], MAX_ATOM_TYPES) ) + { fprintf( fout, "%d of %s\n", fragment_count[i], fragments[i] ); + } + } fprintf( fout, "\n" ); fflush( fout ); diff --git a/sPuReMD/src/ffield.c b/sPuReMD/src/ffield.c index 7aab5c5235d7e6e221c91f9d90c66949ec861551..1fb05c45f755e49b7d756f0a36d243dce6e63f6e 100644 --- a/sPuReMD/src/ffield.c +++ b/sPuReMD/src/ffield.c @@ -167,7 +167,7 @@ char Read_Force_Field( FILE* fp, reax_interaction* reax ) fgets( s, MAX_LINE, fp ); Tokenize( s, &tmp ); - for ( j = 0; j < strlen( tmp[0] ); ++j ) + for ( j = 0; j < strnlen( tmp[0], MAX_TOKEN_LEN ); ++j ) reax->sbp[i].name[j] = toupper( tmp[0][j] ); val = atof(tmp[1]); diff --git a/sPuReMD/src/geo_tools.c b/sPuReMD/src/geo_tools.c index 740a9648fdcb8986720d643eca2d6bc62d019b26..549d29ffce7efe6e64fa889215c7b3f131170441 100644 --- a/sPuReMD/src/geo_tools.c +++ b/sPuReMD/src/geo_tools.c @@ -365,7 +365,7 @@ char Read_PDB( const char * const pdb_file, reax_system* system, control_params pdb_serial = (int) strtod( &serial[0], &endptr ); workspace->orig_id[top] = pdb_serial; - Trim_Spaces( element ); + Trim_Spaces( element, 9 ); atom->type = Get_Atom_Type( &(system->reaxprm), element ); strcpy( atom->name, atom_name ); @@ -492,7 +492,7 @@ char Write_PDB( reax_system* system, reax_list* bonds, simulation_data *data, { p_atom = &(system->atoms[i]); strncpy(name, p_atom->name, 8); - Trim_Spaces(name); + Trim_Spaces( name, 8 ); snprintf( line, MAX_STR, PDB_ATOM_FORMAT_O, "ATOM ", workspace->orig_id[i], p_atom->name, ' ', "REX", ' ', 1, ' ', p_atom->x[0], p_atom->x[1], p_atom->x[2], @@ -711,7 +711,7 @@ char Read_BGF( const char * const bgf_file, reax_system* system, control_params /* atom name and type */ strcpy( system->atoms[atom_cnt].name, atom_name ); - Trim_Spaces( element ); + Trim_Spaces( element, 10 ); system->atoms[atom_cnt].type = Get_Atom_Type( &(system->reaxprm), element ); diff --git a/sPuReMD/src/init_md.c b/sPuReMD/src/init_md.c index 93fed761a7f4233da33ee03fefce7e3320947c14..c071a46f18e6df9a36ea0ee96a84d64ad5b43132 100644 --- a/sPuReMD/src/init_md.c +++ b/sPuReMD/src/init_md.c @@ -773,11 +773,11 @@ void Init_Out_Controls( reax_system *system, control_params *control, /* Init molecular analysis file */ if ( control->molec_anal ) { - snprintf( temp, TEMP_SIZE + 4, "%s.mol", control->sim_name ); + snprintf( temp, TEMP_SIZE, "%.*s.mol", TEMP_SIZE - 5, control->sim_name ); out_control->mol = fopen( temp, "w" ); if ( control->num_ignored ) { - snprintf( temp, TEMP_SIZE + 4, "%s.ign", control->sim_name ); + snprintf( temp, TEMP_SIZE, "%.*s.ign", TEMP_SIZE - 5, control->sim_name ); out_control->ign = fopen( temp, "w" ); } } diff --git a/sPuReMD/src/lin_alg.c b/sPuReMD/src/lin_alg.c index 110c43a3f747ac15e20bb0dc7958a9354963dbfd..5c5b7f47ad7ed7c559e51707f696494c1ec03b04 100644 --- a/sPuReMD/src/lin_alg.c +++ b/sPuReMD/src/lin_alg.c @@ -3294,6 +3294,12 @@ int GMRES_HouseHolder( const static_storage * const workspace, reduction(+: t_ortho, t_pa, t_spmv, t_ts, t_vops) #endif { + t_ortho = 0.0; + t_pa = 0.0; + t_spmv = 0.0; + t_ts = 0.0; + t_vops = 0.0; + t_start = Get_Time( ); bnorm = Norm( b, N ); t_vops += Get_Timing_Info( t_start ); diff --git a/sPuReMD/src/print_utils.c b/sPuReMD/src/print_utils.c index 383fdb8941e1f3c012191a965dc6aa1b05790b6a..d37fe5ea32a8f1829c43d3a86fbe85978f3c6d09 100644 --- a/sPuReMD/src/print_utils.c +++ b/sPuReMD/src/print_utils.c @@ -406,7 +406,7 @@ void Print_Near_Neighbors( reax_system *system, control_params *control, FILE *fout; reax_list *near_nbrs = &((*lists)[NEAR_NBRS]); - snprintf( fname, MAX_STR, "%s.near_nbrs", control->sim_name ); + snprintf( fname, MAX_STR, "%.*s.near_nbrs", MAX_STR - 11, control->sim_name ); fout = fopen( fname, "w" ); for ( i = 0; i < system->N; ++i ) @@ -440,7 +440,7 @@ void Print_Near_Neighbors2( reax_system *system, control_params *control, FILE *fout; reax_list *near_nbrs = &((*lists)[NEAR_NBRS]); - snprintf( fname, MAX_STR, "%s.near_nbrs_lgj", control->sim_name ); + snprintf( fname, MAX_STR, "%.*s.near_nbrs_lgj", MAX_STR - 15, control->sim_name ); fout = fopen( fname, "w" ); for ( i = 0; i < system->N; ++i ) @@ -475,7 +475,7 @@ void Print_Far_Neighbors( reax_system *system, control_params *control, FILE *fout; reax_list *far_nbrs = &((*lists)[FAR_NBRS]); - snprintf( fname, MAX_STR, "%s.far_nbrs", control->sim_name ); + snprintf( fname, MAX_STR, "%.*s.far_nbrs", MAX_STR - 10, control->sim_name ); fout = fopen( fname, "w" ); for ( i = 0; i < system->N; ++i ) @@ -520,7 +520,7 @@ void Print_Far_Neighbors2( reax_system *system, control_params *control, FILE *fout; reax_list *far_nbrs = &((*lists)[FAR_NBRS]); - snprintf( fname, MAX_STR, "%s.far_nbrs_lgj", control->sim_name ); + snprintf( fname, MAX_STR, "%.*s.far_nbrs_lgj", MAX_STR - 14, control->sim_name ); fout = fopen( fname, "w" ); int num = 0; int temp[500]; @@ -553,7 +553,7 @@ void Print_Total_Force( reax_system *system, control_params *control, int i; #if !defined(TEST_FORCES) char temp[1000]; - snprintf( temp, 1000, "%s.ftot", control->sim_name ); + snprintf( temp, 1000, "%.*s.ftot", 994, control->sim_name ); out_control->ftot = fopen( temp, "w" ); #endif @@ -735,7 +735,7 @@ void Print_Linear_System( reax_system *system, control_params *control, sparse_matrix *H; FILE *out; - snprintf( fname, 100, "%s.state%d.out", control->sim_name, step ); + snprintf( fname, 100, "%.*s.state%10d.out", 79, control->sim_name, step ); out = fopen( fname, "w" ); for ( i = 0; i < system->N_cm; i++ ) @@ -753,7 +753,7 @@ void Print_Linear_System( reax_system *system, control_params *control, // fprintf( out, "%g\n", workspace->s_t[i+system->N] ); // fclose( out ); - snprintf( fname, 100, "%s.H%d.out", control->sim_name, step ); + snprintf( fname, 100, "%.*s.H%10d.out", 83, control->sim_name, step ); out = fopen( fname, "w" ); H = workspace->H; @@ -776,7 +776,7 @@ void Print_Linear_System( reax_system *system, control_params *control, fclose( out ); - snprintf( fname, 100, "%s.H_sp%d.out", control->sim_name, step ); + snprintf( fname, 100, "%.*s.H_sp%10d.out", 80, control->sim_name, step ); out = fopen( fname, "w" ); H = workspace->H_sp; @@ -799,13 +799,13 @@ void Print_Linear_System( reax_system *system, control_params *control, fclose( out ); - /*snprintf( fname, 100, "%s.b_s%d", control->sim_name, step ); + /*snprintf( fname, 100, "%.*s.b_s%10d", 84, control->sim_name, step ); out = fopen( fname, "w" ); for( i = 0; i < system->N; i++ ) fprintf( out, "%12.7f\n", workspace->b_s[i] ); fclose( out ); - snprintf( fname, 100, "%s.b_t%d", control->sim_name, step ); + snprintf( fname, 100, "%.*s.b_t%10d", 84, control->sim_name, step ); out = fopen( fname, "w" ); for( i = 0; i < system->N; i++ ) fprintf( out, "%12.7f\n", workspace->b_t[i] ); @@ -820,7 +820,7 @@ void Print_Charges( reax_system *system, control_params *control, char fname[100]; FILE *fout; - snprintf( fname, 100, "%s.q%d", control->sim_name, step ); + snprintf( fname, 100, "%.*s.q%10d", 87, control->sim_name, step ); fout = fopen( fname, "w" ); for ( i = 0; i < system->N; ++i ) diff --git a/sPuReMD/src/tool_box.c b/sPuReMD/src/tool_box.c index 3853c27044896e2884de84e3cbaf2893ee56e9fa..c0a61f51d633b63a5c3d8d360d08659cda3e9a80 100644 --- a/sPuReMD/src/tool_box.c +++ b/sPuReMD/src/tool_box.c @@ -19,7 +19,6 @@ <http://www.gnu.org/licenses/>. ----------------------------------------------------------------------*/ -#include "mytypes.h" #include "tool_box.h" #include <ctype.h> @@ -273,13 +272,22 @@ int Check_Input_Range( int val, int lo, int hi, char *message ) } -void Trim_Spaces( char *element ) +void Trim_Spaces( char * const element, const size_t size ) { - int i, j; + int i, j, n; + + n = strnlen( element, size ); + + /* buffer not NULL-terminated, abort */ + if ( n == size ) + { + return; + } - for ( i = 0; element[i] == ' '; ++i ); // skip initial space chars + for ( i = 0; element[i] == ' '; ++i ) + ; // skip initial space chars - for ( j = i; j < (int)(strlen(element)) && element[j] != ' '; ++j ) + for ( j = i; j < n && element[j] != ' '; ++j ) { element[j - i] = toupper( element[j] ); // make uppercase, offset to 0 } diff --git a/sPuReMD/src/tool_box.h b/sPuReMD/src/tool_box.h index d1b15f64bd2a8adb7ee3a0ee7c40d40113b81f48..29781e00dc637633ae8775dd7ac1d1237711986e 100644 --- a/sPuReMD/src/tool_box.h +++ b/sPuReMD/src/tool_box.h @@ -56,7 +56,7 @@ int is_Valid_Serial( static_storage*, int ); int Check_Input_Range( int, int, int, char* ); -void Trim_Spaces( char* ); +void Trim_Spaces( char * const, const size_t ); /* from system_props.h */ real Get_Time( ); diff --git a/sPuReMD/src/traj.c b/sPuReMD/src/traj.c index 14e2a36b0cc028d974211a51671d52ec4123d044..7972d0868c47fc992f909d9be457c3d454743e44 100644 --- a/sPuReMD/src/traj.c +++ b/sPuReMD/src/traj.c @@ -79,7 +79,7 @@ int Write_Custom_Header( reax_system *system, control_params *control, control->molec_anal, control->freq_molec_anal ); - control_block_len = strlen( control_block ); + control_block_len = strnlen( control_block, SIZE1 ); snprintf( frame_format, SIZE1, "Frame Format: %d\n%s\n%s\n", @@ -123,7 +123,7 @@ int Write_Custom_Header( reax_system *system, control_params *control, } strcat( frame_format, angle_format ); - frame_format_len = strlen( frame_format ); + frame_format_len = strnlen( frame_format, SIZE1 ); header_len = HEADER_INIT_LEN + (control_block_len + SIZE_INFO_LEN2) + @@ -309,7 +309,7 @@ int Append_Custom_Frame( reax_system *system, control_params *control, data->E_Ang, data->E_Pen, data->E_Coa, data->E_HB, data->E_Tor, data->E_Con, data->E_vdW, data->E_Ele, data->E_Pol ); - frame_globals_len = strlen( buffer ); + frame_globals_len = strnlen( buffer, SIZE ); frame_len = frame_globals_len + write_atoms * SIZE_INFO_LEN3 + system->N * atom_line_len + @@ -507,7 +507,7 @@ void Read_Traj( output_controls *out_control, char *traj_name ) fprintf( stderr, "read line\n" ); - if ( strlen( size_buffer ) >= SIZE_INFO_LEN3 ) + if ( strnlen( size_buffer, 50 ) >= SIZE_INFO_LEN3 ) { sscanf( size_buffer, "%d %d %d", &skip_all, &skip_part, &n ); }