From 071146cf108d84119e4cd825834fd00f14b83497 Mon Sep 17 00:00:00 2001 From: Gal Salomon Date: Fri, 1 Mar 2024 14:44:54 +0200 Subject: [PATCH] replace assert with an exception to avoid crashing the process Signed-off-by: Gal Salomon --- include/s3select.h | 17 +++++---- include/s3select_csv_parser.h | 67 +++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/include/s3select.h b/include/s3select.h index 311783c4..95f57c7a 100644 --- a/include/s3select.h +++ b/include/s3select.h @@ -2730,24 +2730,29 @@ class csv_object : public base_s3object m_error_description = "escaped_char_missing failure while csv parsing"; return -1; } - catch(io::error::escaped_string_not_closed& err) + catch(io::error::escaped_string_not_closed& err) { m_error_description = "escaped_string_not_closed failure while csv parsing"; return -1; } - catch(io::error::line_length_limit_exceeded& err) + catch(io::error::line_length_limit_exceeded& err) { m_error_description = "line_length_limit_exceeded failure while csv parsing"; return -1; } - catch(io::error::with_file_name& err) + catch(io::error::missmatch_of_begin_end& err) { - m_error_description = "with_file_name failure while csv parsing"; + m_error_description = "missmatch_of_begin_end failure while csv parsing" + std::string(err.what()); return -1; } - catch(io::error::with_file_line& err) + catch(io::error::missmatch_end& err) { - m_error_description = "with_file_line failure while csv parsing"; + m_error_description = "missmatch_end failure while csv parsing" + std::string(err.what()); + return -1; + } + catch(io::error::with_file_name& err) + { + m_error_description = "with_file_name failure while csv parsing"; return -1; } catch(std::exception& e) diff --git a/include/s3select_csv_parser.h b/include/s3select_csv_parser.h index dab2e4ef..28e8117a 100644 --- a/include/s3select_csv_parser.h +++ b/include/s3select_csv_parser.h @@ -13,6 +13,53 @@ namespace io{ , file_line, file_name); } }; + + struct missmatch_of_begin_end : + base, + with_file_name, + with_file_line{ + int begin=-1,end=-1; + void set_begin_end(int b,int e){ + begin=b; + end=e; + } + + void format_error_message()const override{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "***missmatch_of_begin_end*** Line number %d in file \"%s\" begin{%d} > end{%d}" + ,file_line, file_name,begin,end); + } + }; + + struct missmatch_end : + base, + with_file_name, + with_file_line{ + int end=-1; + int block_size=-1; + void set_end_block(int e,int b){ + end = e; + block_size = b; + } + void format_error_message()const override{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "***missmatch_end*** Line number %d in file \"%s\" end{%d} block{%d}" + ,file_line, file_name, end, block_size); + } + }; + + + struct line_is_null : + base, + with_file_name, + with_file_line{ + void format_error_message()const override{ + std::snprintf(error_message_buffer, sizeof(error_message_buffer), + "***line is NULL*** Line number %d in file \"%s\"" + ,file_line, file_name); + } + }; + } namespace detail{ @@ -133,7 +180,11 @@ namespace io{ void chop_next_column(char*&line, char*&col_begin, char*&col_end, char& col_delimiter, char& quote, char& escape_char) { - assert(line != nullptr); + if(line == NULL) + { + io::error::line_is_null err; + throw err; + } col_begin = line; // the col_begin + (... - col_begin) removes the constness @@ -312,8 +363,18 @@ class CSVParser ++file_line; - assert(data_begin < data_end); - assert(data_end <= block_len*2); + if(data_begin > data_end) + { + io::error::missmatch_of_begin_end err; + err.set_begin_end(data_begin,data_end); + throw err; + } + if(data_end > block_len*2) + { + io::error::missmatch_end err; + err.set_end_block(data_end,block_len*2); + throw err; + } if(data_begin >= block_len) {