From c202d0634bb8dbc256841c7b5514357e6674f00a Mon Sep 17 00:00:00 2001 From: Magnus Lundborg Date: Fri, 29 Aug 2014 11:32:36 +0200 Subject: Improved check if ZLIB is available before using it. Alos check that there are bonds in a molecule before trying to reserve memory for them. Change-Id: I3b541f32e2945f7b2bbe6a9c10bf47feb0a8094f diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index db49089..0bd0417 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -5479,6 +5479,16 @@ static tng_function_status tng_particle_data_read &tng_data->current_trajectory_frame_set; char block_type_flag, *contents; + /* This must be caught early to avoid creating a data block if not necessary. */ +#ifndef USE_ZLIB + if(codec_id == TNG_GZIP_COMPRESSION) + { + fprintf(stderr, "TNG library: Cannot uncompress data block. %s: %d\n", __FILE__, + __LINE__); + return(TNG_FAILURE); + } +#endif + switch(datatype) { case TNG_CHAR_DATA: @@ -6488,6 +6498,16 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, /* fprintf(stderr, "TNG library: %s\n", block->name);*/ + /* This must be caught early to avoid creating a data block if not necessary. */ +#ifndef USE_ZLIB + if(codec_id == TNG_GZIP_COMPRESSION) + { + fprintf(stderr, "TNG library: Cannot uncompress data block. %s: %d\n", __FILE__, + __LINE__); + return(TNG_FAILURE); + } +#endif + switch(datatype) { case TNG_CHAR_DATA: @@ -8342,21 +8362,24 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_system_copy(tng_trajectory_t } } molecule_temp->n_bonds = molecule->n_bonds; - bond_temp = realloc(molecule_temp->bonds, sizeof(struct tng_bond) * - molecule->n_bonds); - if(!bond_temp) - { - fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", - sizeof(struct tng_bond) * molecule->n_bonds, - __FILE__, __LINE__); - free(molecule_temp->bonds); - molecule_temp->n_bonds = 0; - return(TNG_CRITICAL); - } - molecule_temp->bonds = bond_temp; - for(j = 0; j < molecule->n_bonds; j++) + if(molecule->n_bonds > 0) { - molecule_temp->bonds[j] = molecule->bonds[j]; + bond_temp = realloc(molecule_temp->bonds, sizeof(struct tng_bond) * + molecule->n_bonds); + if(!bond_temp) + { + fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", + sizeof(struct tng_bond) * molecule->n_bonds, + __FILE__, __LINE__); + free(molecule_temp->bonds); + molecule_temp->n_bonds = 0; + return(TNG_CRITICAL); + } + molecule_temp->bonds = bond_temp; + for(j = 0; j < molecule->n_bonds; j++) + { + molecule_temp->bonds[j] = molecule->bonds[j]; + } } stat = tng_molecule_cnt_set(tng_data_dest, molecule_temp, tng_data_src->molecule_cnt_list[i]); -- cgit v0.10.1