From dacba96a56a325d6aa431c64553dd34d5d99b642 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 14 Jun 2020 17:26:06 +0200 Subject: mehr Fehlerprüfungen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csv-test.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/csv-test.c b/csv-test.c index 3c9836d..68903de 100644 --- a/csv-test.c +++ b/csv-test.c @@ -188,6 +188,7 @@ int csv_init_opt(csv_t *csv, FILE *in, csv_options_t *csv_options) { assert(csv != NULL); + assert(in != NULL); csv->in = in; csv->csv_options = ( csv_options != NULL ) ? csv_options : &default_csv_options; @@ -233,8 +234,14 @@ const char * csv_field(csv_t *csv, int idx) { assert(csv != NULL); + assert(idx >= 0 && idx < csv->csv_field.pos); - return &csv->csv_string.str[csv->csv_field.fields[idx]]; + if ( idx >= 0 && idx < csv->csv_field.pos) { + return &csv->csv_string.str[csv->csv_field.fields[idx]]; + } + else { + return NULL; + } } int @@ -373,6 +380,10 @@ csv_read(csv_t *csv) /* ... und die Anzahl der Felder zurückliefern! */ return csv->csv_field.pos; + + default: + assert(!"this should never be happen..."); + break; } } /* NOT REACHED */ @@ -430,11 +441,12 @@ main(void) clock_t start = clock(); - csv_init(csv, stdin); - while ( (n = csv_read(csv)) != -1 ) { - ++line; + if ( csv_init(csv, stdin) == CSV_OK ) { + while ( (n = csv_read(csv)) != -1 ) { + ++line; + } + csv_free(csv); } - csv_free(csv); clock_t end = clock(); -- cgit v1.3