From e630062cc258e5f9341b05a730e6ed82e3c6c521 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Fri, 24 Jul 2009 18:49:56 +0000 Subject: [PATCH] Attempt to fix SQLite seg fault problem git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@9098 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/cats/sql.c | 31 +++++++++++++++++++++++++------ bacula/src/cats/sqlite.c | 16 ++++++++++------ bacula/technotes | 2 ++ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/bacula/src/cats/sql.c b/bacula/src/cats/sql.c index 324d01760c..00a0a9d43d 100644 --- a/bacula/src/cats/sql.c +++ b/bacula/src/cats/sql.c @@ -1,7 +1,7 @@ /* Bacula® - The Network Backup Solution - Copyright (C) 2000-2008 Free Software Foundation Europe e.V. + Copyright (C) 2000-2009 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -561,6 +561,21 @@ void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname) Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname); } +/* + * Set maximum field length to something reasonable + */ +static int max_length(int max_length) +{ + int max_len = max_length; + /* Sanity check */ + if (max_len < 0) { + max_len = 2; + } else if (max_len > 100) { + max_len = 100; + } + return max_len; +} + /* * List dashes as part of header for listing SQL results in a table */ @@ -569,6 +584,7 @@ list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx) { SQL_FIELD *field; int i, j; + int len; sql_field_seek(mdb, 0); send(ctx, "+"); @@ -577,7 +593,8 @@ list_dashes(B_DB *mdb, DB_LIST_HANDLER *send, void *ctx) if (!field) { break; } - for (j = 0; j < (int)field->max_length + 2; j++) { + len = max_length(field->max_length + 2); + for (j = 0; j < len; j++) { send(ctx, "-"); } send(ctx, "+"); @@ -646,7 +663,8 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t if (!field) { break; } - bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, field->name); + max_len = max_length(field->max_length); + bsnprintf(buf, sizeof(buf), " %-*s |", max_len, field->name); send(ctx, buf); } send(ctx, "\n"); @@ -661,13 +679,14 @@ list_result(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *send, void *ctx, e_list_type t if (!field) { break; } + max_len = max_length(field->max_length); if (row[i] == NULL) { - bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, "NULL"); + bsnprintf(buf, sizeof(buf), " %-*s |", max_len, "NULL"); } else if (IS_NUM(field->type) && !jcr->gui && is_an_integer(row[i])) { - bsnprintf(buf, sizeof(buf), " %*s |", (int)field->max_length, + bsnprintf(buf, sizeof(buf), " %*s |", max_len, add_commas(row[i], ewc)); } else { - bsnprintf(buf, sizeof(buf), " %-*s |", (int)field->max_length, row[i]); + bsnprintf(buf, sizeof(buf), " %-*s |", max_len, row[i]); } send(ctx, buf); } diff --git a/bacula/src/cats/sqlite.c b/bacula/src/cats/sqlite.c index b6e78359de..98a970deae 100644 --- a/bacula/src/cats/sqlite.c +++ b/bacula/src/cats/sqlite.c @@ -121,7 +121,7 @@ db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char mdb = (B_DB *) malloc(sizeof(B_DB)); memset(mdb, 0, sizeof(B_DB)); mdb->db_name = bstrdup(db_name); - mdb->have_insert_id = TRUE; + mdb->have_insert_id = true; mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */ *mdb->errmsg = 0; mdb->cmd = get_pool_memory(PM_EMSG); /* get command buffer */ @@ -445,18 +445,22 @@ void my_sqlite_field_seek(B_DB *mdb, int field) mdb->fields[i]->type = 0; mdb->fields[i]->flags = 1; /* not null */ } - mdb->fields_defined = TRUE; + mdb->fields_defined = true; } - if (field > sql_num_fields(mdb)) { - field = sql_num_fields(mdb); + if (field > sql_num_fields(mdb) - 1) { + field = sql_num_fields(mdb) - 1; } mdb->field = field; - } SQL_FIELD *my_sqlite_fetch_field(B_DB *mdb) { - return mdb->fields[mdb->field++]; + if (mdb->fields_defined && mdb->field < sql_num_fields(mdb)) { + return mdb->fields[mdb->field++]; + } else { + mdb->field = 0; + return NULL; + } } #ifdef HAVE_BATCH_FILE_INSERT diff --git a/bacula/technotes b/bacula/technotes index e10b91368a..b8bb6a4b5d 100644 --- a/bacula/technotes +++ b/bacula/technotes @@ -2,6 +2,8 @@ General: +24Jul09 +kes Attempt to fix SQLite seg fault problem 23Jul09 kes Fix int/int32_t problem in accurate_add_file reported by "Eli Shemer" -- 2.39.5