Skip to content

Commit

Permalink
Merge pull request #37 from andrewjpage/read_line_memory_leak
Browse files Browse the repository at this point in the history
Read line memory leak
  • Loading branch information
andrewjpage committed Jun 12, 2012
2 parents 6646d3f + da27ac7 commit 8f983f7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/alignment_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,19 @@ int read_first_few_characters_of_line(char sequence[], FILE * pFilePtr, int max_



int read_line(char sequence[], FILE * pFilePtr)
char * read_line(char sequence[], FILE * pFilePtr)
{

strcpy(sequence, "");
char *pcRes = NULL;
int lineLength = 0;
long lineLength = 0;
char current_line_buffer[MAX_READ_BUFFER] = {0};


while((pcRes = fgets(current_line_buffer, sizeof(current_line_buffer), pFilePtr)) != NULL){
if( strlen(current_line_buffer) > MAX_READ_BUFFER - 10 )
{
realloc(sequence, strlen(sequence) + MAX_READ_BUFFER + 10 );
}

if(strlen(sequence) > 0)
{
sequence = realloc(sequence, strlen(sequence) + strlen(current_line_buffer) + 2 );
}
strcat(sequence, current_line_buffer);
strcpy(current_line_buffer, "");
lineLength = strlen(sequence) - 1;
Expand All @@ -269,7 +268,7 @@ int read_line(char sequence[], FILE * pFilePtr)
}


return 1;
return sequence;
}


Expand Down
4 changes: 2 additions & 2 deletions src/alignment_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ int build_reference_sequence(char reference_sequence[], char filename[]);
void advance_to_sequence(FILE * alignment_file_pointer);
void advance_to_sequence_name(FILE * alignment_file_pointer);
int genome_length(char filename[]);
int read_line(char sequence[], FILE * pFilePtr);
char * read_line(char sequence[], FILE * pFilePtr);
int number_of_sequences_in_file(char filename[]);
void get_sample_names_for_header(char filename[], char ** sequence_names, int number_of_samples);
char filter_invalid_characters(char input_char);
void get_bases_for_each_snp(char filename[], int snp_locations[], char ** bases_for_snps, int length_of_genome, int number_of_snps);
int read_first_few_characters_of_line(char sequence[], FILE * pFilePtr, int max_characters);


#define MAX_READ_BUFFER 262144
#define MAX_READ_BUFFER 65536
#define MAX_READ_BUFFER_SMALL 1024
#define MAX_SAMPLE_NAME_SIZE 1024

Expand Down
9 changes: 6 additions & 3 deletions src/parse_phylip.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void load_sequences_from_phylib(FILE * phylip_file_pointer)

// The first line contains the number of samples and snps
strcpy(line_buffer,"");
read_line(line_buffer, phylip_file_pointer);
line_buffer = read_line(line_buffer, phylip_file_pointer);

num_samples = get_number_of_samples_from_phylip(line_buffer);
num_snps = get_number_of_snps_from_phylip(line_buffer);
Expand All @@ -286,7 +286,10 @@ void load_sequences_from_phylib(FILE * phylip_file_pointer)

do{
strcpy(line_buffer,"");
read_line(line_buffer, phylip_file_pointer);
free(line_buffer);
line_buffer = (char *) malloc(MAX_READ_BUFFER*sizeof(char));

line_buffer = read_line(line_buffer, phylip_file_pointer);

if(line_buffer[0] == '\0')
{
Expand Down Expand Up @@ -325,7 +328,7 @@ void load_sequences_from_phylib(FILE * phylip_file_pointer)
sample_counter++;

}while(line_buffer[0] != '\0');

free(line_buffer);
initialise_statistics();
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse_phylip.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void set_number_of_blocks_for_sample(char * sample_name,int num_blocks);
sample_statistics ** get_sample_statistics();
int number_of_snps_in_phylib();

#define MAX_READ_BUFFER 262144
#define MAX_READ_BUFFER 65536
#define MAX_SAMPLE_NAME_SIZE 1024


Expand Down
7 changes: 5 additions & 2 deletions src/parse_vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int get_number_of_columns_from_file(FILE * vcf_file_pointer)
do{
strcpy(szBuffer,"");
// check the first character of the line to see if its in the header
read_line(szBuffer, vcf_file_pointer);
szBuffer = read_line(szBuffer, vcf_file_pointer);
if(szBuffer[0] == '\0' || szBuffer[0] != '#')
{
break;
Expand Down Expand Up @@ -204,7 +204,9 @@ void get_column_names(FILE * vcf_file_pointer, char ** column_names, int number_
do{
strcpy(szBuffer,"");
// check the first character of the line to see if its in the header
read_line(szBuffer, vcf_file_pointer);
free(szBuffer);
szBuffer = (char *) malloc(MAX_READ_BUFFER*sizeof(char));
szBuffer = read_line(szBuffer, vcf_file_pointer);

if(szBuffer[0] == '\0' || szBuffer[0] != '#')
{
Expand All @@ -223,6 +225,7 @@ void get_column_names(FILE * vcf_file_pointer, char ** column_names, int number_
}

}while(szBuffer[0] != '\0');
free(szBuffer);
}

// Assume the sample names are unique
Expand Down
2 changes: 1 addition & 1 deletion src/parse_vcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ void get_column_names(FILE * vcf_file_pointer, char ** column_names, int number_
int column_number_for_column_name(char ** column_names, char * column_name, int number_of_columns);

void get_integers_from_column_in_vcf(FILE * vcf_file_pointer, int * integer_values, int number_of_snps, int column_number);
#define MAX_READ_BUFFER 262144
#define MAX_READ_BUFFER 65536

#endif

0 comments on commit 8f983f7

Please sign in to comment.