From 3afb857208386966fb1d52826ea8f265e2d41c47 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Thu, 2 Jan 2020 21:55:42 +0100 Subject: [PATCH] fix: null tag caused crash --- models/controller_dbo.cc | 8 +++++++- models/relay_dbo.cc | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/models/controller_dbo.cc b/models/controller_dbo.cc index b4775e7..c96b9ee 100644 --- a/models/controller_dbo.cc +++ b/models/controller_dbo.cc @@ -43,6 +43,7 @@ static controller_dbo* controller_db_select_mapper(sqlite3_stmt *stmt) { auto *new_controller = new controller_dbo(); + const char* new_tag; for(int i = 0; i < sqlite3_column_count(stmt); i++) { const char *name = sqlite3_column_name(stmt, i); @@ -74,7 +75,12 @@ controller_db_select_mapper(sqlite3_stmt *stmt) new_controller->relay_count = sqlite3_column_int(stmt, i); break; case 't': // tag - strncpy(new_controller->tag, (const char*)sqlite3_column_text(stmt, i), 63); + new_tag = (const char*)sqlite3_column_text(stmt, i); + new_controller->tag[0] = '\0'; + if(new_tag) + { + strncpy(new_controller->tag, (const char*)sqlite3_column_text(stmt, i), 63); + } break; default: // ignore columns not implemented break; diff --git a/models/relay_dbo.cc b/models/relay_dbo.cc index 8847f43..f83ff93 100644 --- a/models/relay_dbo.cc +++ b/models/relay_dbo.cc @@ -34,6 +34,7 @@ static relay_dbo* relay_db_select_mapper(sqlite3_stmt *stmt) { auto *new_relay = new relay_dbo(); + const char* new_tag; for(int i = 0; i < sqlite3_column_count(stmt); i++) { const char *name = sqlite3_column_name(stmt, i); @@ -60,8 +61,14 @@ relay_db_select_mapper(sqlite3_stmt *stmt) default: break; } + break; case 't': // tag - strncpy(new_relay->tag, (const char*)sqlite3_column_text(stmt, i), 63); + new_tag = (const char*)sqlite3_column_text(stmt, i); + new_relay->tag[0] = '\0'; + if(new_tag) + { + strncpy(new_relay->tag, (const char*)sqlite3_column_text(stmt, i), 63); + } break; default: // ignore columns not implemented break; @@ -258,4 +265,4 @@ relay_dbo::free_list(relay_dbo **relays_list) delete relays_list[i]; } free(relays_list); -} \ No newline at end of file +}