From a263449463e2faa5d4e13afa3286ab489df9186c Mon Sep 17 00:00:00 2001 From: Magnus Lundborg Date: Fri, 29 Aug 2014 11:37:44 +0200 Subject: [PATCH] Fix data block length when uncompressing TNG data. Also, if there are no bonds in a molecule do not reserve memory for them (to avoid a returned NULL pointer). Change-Id: I06f9ad71711174017a229faec53d6dca08ee99d0 --- src/external/tng_io/src/lib/tng_io.c | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/external/tng_io/src/lib/tng_io.c b/src/external/tng_io/src/lib/tng_io.c index e09c1b7383..92721fab40 100644 --- a/src/external/tng_io/src/lib/tng_io.c +++ b/src/external/tng_io/src/lib/tng_io.c @@ -5057,6 +5057,7 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, const int64_t uncompressed_len) { char *temp, *temp_data_contents; + int64_t compressed_len; double *d_dest = 0; float *f_dest = 0; int64_t offset; @@ -5078,7 +5079,8 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, return(TNG_FAILURE); } - temp_data_contents = malloc(uncompressed_len); + compressed_len = block->block_contents_size - (int64_t)((char *)start_pos - (char *)block->block_contents); + temp_data_contents = malloc(compressed_len); if(!temp_data_contents) { fprintf(stderr, "TNG library: Cannot allocate memory (%"PRId64" bytes). %s: %d\n", @@ -5086,7 +5088,7 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, return(TNG_CRITICAL); } - memcpy(temp_data_contents, (char *)start_pos, uncompressed_len); + memcpy(temp_data_contents, (char *)start_pos, compressed_len); if(type == TNG_FLOAT_DATA) { @@ -8365,21 +8367,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]); -- 2.22.0