Permalink
Browse files

Issue #287: made CSR/CSC readers more robust against invalid input (c…

…ase #1).
  • Loading branch information...
hfp committed Nov 26, 2018
1 parent dd7d460 commit 151481489192e6d1997f8bde52c5c425ea41741d
@@ -78,7 +78,9 @@ static void libxsmm_sparse_csr_reader( const char* i_csr_file_in,
} else {
/* if we are the first line after comment header, we allocate our data structures */
if ( l_header_read == 0 ) {
if ( sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) == 3 ) {
if (3 == sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) &&
0 != *o_row_count && 0 != *o_column_count && 0 != *o_element_count)
{
/* allocate CSC datastructure matching mtx file */
*o_column_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_element_count));
*o_row_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_row_count + 1));
@@ -62,7 +62,9 @@ void edge_sparse_csr_reader_double( const char* i_csr_file_in,
} else {
/* if we are the first line after comment header, we allocate our data structures */
if ( l_header_read == 0 ) {
if ( sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) == 3 ) {
if (3 == sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) &&
0 != *o_row_count && 0 != *o_column_count && 0 != *o_element_count)
{
/* allocate CSC datastructure matching mtx file */
*o_column_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_element_count));
*o_row_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_row_count + 1));
@@ -75,7 +75,9 @@ int my_csr_reader( const char* i_csr_file_in,
} else {
/* if we are the first line after comment header, we allocate our data structures */
if ( l_header_read == 0 ) {
if ( sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) == 3 ) {
if (3 == sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) &&
0 != *o_row_count && 0 != *o_column_count && 0 != *o_element_count)
{
/* allocate CSC datastructure matching mtx file */
*o_column_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_element_count));
*o_row_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_row_count + 1));
@@ -117,7 +117,9 @@ void libxsmm_sparse_csc_reader( libxsmm_generated_code* io_generated_code,
} else {
/* if we are the first line after comment header, we allocate our data structures */
if ( l_header_read == 0 ) {
if ( sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) == 3 ) {
if (3 == sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) &&
0 != *o_row_count && 0 != *o_column_count && 0 != *o_element_count)
{
/* allocate CSC data structure matching mtx file */
*o_row_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_element_count));
*o_column_idx = (unsigned int*) malloc(sizeof(unsigned int) * ((size_t)(*o_column_count) + 1));
@@ -168,8 +170,8 @@ void libxsmm_sparse_csc_reader( libxsmm_generated_code* io_generated_code,
return;
}
/* adjust numbers to zero termination */
l_row--;
l_column--;
LIBXSMM_ASSERT(0 != l_row && 0 != l_column);
l_row--; l_column--;
/* add these values to row and value structure */
(*o_row_idx)[l_i] = l_row;
(*o_values)[l_i] = l_value;
@@ -205,4 +207,3 @@ void libxsmm_sparse_csc_reader( libxsmm_generated_code* io_generated_code,
}
}


@@ -78,7 +78,9 @@ void libxsmm_sparse_csr_reader( libxsmm_generated_code* io_generated_code,
} else {
/* if we are the first line after comment header, we allocate our data structures */
if ( l_header_read == 0 ) {
if ( sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) == 3 ) {
if (3 == sscanf(l_line, "%u %u %u", o_row_count, o_column_count, o_element_count) &&
0 != *o_row_count && 0 != *o_column_count && 0 != *o_element_count)
{
/* allocate CSC data-structure matching mtx file */
*o_column_idx = (unsigned int*) malloc(sizeof(unsigned int) * (*o_element_count));
*o_row_idx = (unsigned int*) malloc(sizeof(unsigned int) * ((size_t)(*o_row_count) + 1));
@@ -129,8 +131,8 @@ void libxsmm_sparse_csr_reader( libxsmm_generated_code* io_generated_code,
return;
}
/* adjust numbers to zero termination */
l_row--;
l_column--;
LIBXSMM_ASSERT(0 != l_row && 0 != l_column);
l_row--; l_column--;
/* add these values to row and value structure */
(*o_column_idx)[l_i] = l_column;
(*o_values)[l_i] = l_value;
@@ -1 +1 @@
master-1.10-47
master-1.10-48

0 comments on commit 1514814

Please sign in to comment.