Skip to content

Commit c5b7063

Browse files
committed
Merge branch 'master' of github.com:piql/afs into cmake-experiments
2 parents 5ad41a8 + bc7752e commit c5b7063

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

src/tocfiles.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,12 @@ DBOOL afs_toc_files_load_xml(afs_toc_files * toc_files, mxml_node_t* node)
975975
return DTRUE;
976976
}
977977

978+
/**
979+
* FIXME: This is kind of a hack, both `afs_toc_files_save_as_table`and
980+
* `afs_toc_files_save_as_metadata_table` need rewriting
981+
*/
982+
static const size_t table_allocation_epsilon = 1024 * 1024;
983+
978984

979985
//----------------------------------------------------------------------------
980986
/*!
@@ -1009,10 +1015,14 @@ char * afs_toc_files_save_as_table(afs_toc_files * toc_files)
10091015
lengths.format_id_length + lengths.file_name_length + 5;
10101016
size_t table1_length = header_length + table1_width * tocs_count + 1;
10111017

1012-
char * return_string = malloc(table1_length + 1);
1018+
size_t current_string_remaining_size = table1_length + 1 + table_allocation_epsilon;
1019+
char * return_string = malloc(current_string_remaining_size);
10131020
char * current_string = return_string;
1014-
1015-
current_string += sprintf(current_string, "%s\n", header);
1021+
1022+
int n = snprintf(current_string, "%s\n", header);
1023+
assert(n >= 0 && n < (int)current_string_remaining_size);
1024+
current_string_remaining_size -= n;
1025+
current_string += n;
10161026

10171027
for (size_t i = 0; i < tocs_count; i++)
10181028
{
@@ -1042,8 +1052,14 @@ char * afs_toc_files_save_as_table(afs_toc_files * toc_files)
10421052
sprintf(file_format_string, "%s", "N");
10431053
}
10441054

1045-
current_string += sprintf(current_string, "%0*d %-*s %s %-*s %s\n", lengths.id_length, toc_file->id, 36, toc_file->unique_id,
1046-
parent_id_string, lengths.format_id_length, file_format_string, toc_file->name);
1055+
n = snprintf(current_string, current_string_remaining_size,
1056+
"%0*d %-*s %s %-*s %s\n", lengths.id_length, toc_file->id,
1057+
36, toc_file->unique_id, parent_id_string,
1058+
lengths.format_id_length, file_format_string,
1059+
toc_file->name);
1060+
assert(n >= 0 && n < (int)current_string_remaining_size);
1061+
current_string_remaining_size -= n;
1062+
current_string += n;
10471063
}
10481064

10491065
return return_string;
@@ -1233,10 +1249,7 @@ char * afs_toc_files_save_as_metadata_table(afs_toc_files * toc_files)
12331249
lengths.source_id_length + lengths.source_format_id_length + lengths.source_data_length + (unsigned int)5 + (unsigned int)strlen("\n");
12341250
unsigned int metadata_table_length = metadata_header_length + metadata_table_width * lengths.metadata_sources_count + 1;
12351251

1236-
const size_t metadata_table_allocation_epsilon = 1024 * 1024;
1237-
1238-
// FIXME: This is kind of a hack, maybe just do a resizing allocation as prints get added
1239-
size_t current_string_remaining_size = metadata_table_length + 1 + metadata_table_allocation_epsilon;
1252+
size_t current_string_remaining_size = metadata_table_length + 1 + table_allocation_epsilon;
12401253
char * return_string = malloc(current_string_remaining_size);
12411254
char * current_string = return_string;
12421255

0 commit comments

Comments
 (0)