From de4f5516b7aa941075e8c633110ce181d0e5016a Mon Sep 17 00:00:00 2001 From: James Woodcock Date: Sat, 31 Oct 2020 09:46:00 +0000 Subject: [PATCH] Handle errors from mdb_bind_column() --- src/libmdb/data.c | 12 ++++++++++-- src/util/mdb-export.c | 7 ++++++- src/util/mdb-json.c | 7 ++++++- src/util/sargtest.c | 6 +++++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/libmdb/data.c b/src/libmdb/data.c index 54984e5..a9c6574 100644 --- a/src/libmdb/data.c +++ b/src/libmdb/data.c @@ -488,16 +488,24 @@ mdb_fetch_row(MdbTableDef *table) void mdb_data_dump(MdbTableDef *table) { unsigned int i; + int ret; char *bound_values[MDB_MAX_COLS]; for (i=0;inum_cols;i++) { bound_values[i] = (char *) g_malloc(256); - mdb_bind_column(table, i+1, bound_values[i], NULL); + ret = mdb_bind_column(table, i+1, bound_values[i], NULL); + if (ret == -1) { + fprintf(stdout, "error binding column %d\n", i+1); + g_free(bound_values[i]); + bound_values[i] = NULL; + } } mdb_rewind_table(table); while (mdb_fetch_row(table)) { for (i=0;inum_cols;i++) { - fprintf(stdout, "column %d is %s\n", i+1, bound_values[i]); + if (bound_values[i]) { + fprintf(stdout, "column %d is %s\n", i+1, bound_values[i]); + } } } for (i=0;inum_cols;i++) { diff --git a/src/util/mdb-export.c b/src/util/mdb-export.c index 1732b10..a230dc8 100755 --- a/src/util/mdb-export.c +++ b/src/util/mdb-export.c @@ -51,6 +51,7 @@ main(int argc, char **argv) int bin_mode = MDB_BINEXPORT_RAW; char *value; size_t length; + int ret; GOptionEntry entries[] = { {"no-header", 'H', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &header_row, "Suppress header row.", NULL}, @@ -172,7 +173,11 @@ main(int argc, char **argv) for (i = 0; i < table->num_cols; i++) { /* bind columns */ bound_values[i] = (char *) g_malloc0(EXPORT_BIND_SIZE); - mdb_bind_column(table, i + 1, bound_values[i], &bound_lens[i]); + ret = mdb_bind_column(table, i + 1, bound_values[i], &bound_lens[i]); + if (ret == -1) { + fprintf(stderr, "Failed to bind column %d\n", i + 1); + exit(1); + } } if (header_row) { for (i = 0; i < table->num_cols; i++) { diff --git a/src/util/mdb-json.c b/src/util/mdb-json.c index 5b1f081..fa0ac2c 100644 --- a/src/util/mdb-json.c +++ b/src/util/mdb-json.c @@ -112,6 +112,7 @@ main(int argc, char **argv) char *shortdate_fmt = NULL; char *value; size_t length; + int ret; GOptionEntry entries[] = { {"date-format", 'D', 0, G_OPTION_ARG_STRING, &shortdate_fmt, "Set the date format (see strftime(3) for details)", "format"}, @@ -166,7 +167,11 @@ main(int argc, char **argv) for (i=0;inum_cols;i++) { /* bind columns */ bound_values[i] = (char *) g_malloc0(EXPORT_BIND_SIZE); - mdb_bind_column(table, i+1, bound_values[i], &bound_lens[i]); + ret = mdb_bind_column(table, i+1, bound_values[i], &bound_lens[i]); + if (ret == -1) { + fprintf(stderr, "Failed to bind column %d\n", i + 1); + exit(1); + } } while(mdb_fetch_row(table)) { diff --git a/src/util/sargtest.c b/src/util/sargtest.c index 1b0a852..0b29647 100644 --- a/src/util/sargtest.c +++ b/src/util/sargtest.c @@ -64,7 +64,11 @@ MdbSarg sarg; for (j=0;jnum_cols;j++) { bound_values[j] = (char *) g_malloc(MDB_BIND_SIZE); bound_values[j][0] = '\0'; - mdb_bind_column(table, j+1, bound_values[j], NULL); + ret = mdb_bind_column(table, j+1, bound_values[j], NULL); + if (ret == -1) { + fprintf(stderr, "Failed to bind column %d\n", i + 1); + exit(1); + } } /* print header */