diff --git a/lib/proxy/db.c b/lib/proxy/db.c index 2a556e0..a0bfe34 100644 --- a/lib/proxy/db.c +++ b/lib/proxy/db.c @@ -38,7 +38,8 @@ int proxy_db_exec_stmt(pool *p, const char *stmt, const char **errstr) { char *ptr = NULL; unsigned int nretries = 0; - if (stmt == NULL) { + if (p == NULL || + stmt == NULL) { errno = EINVAL; return -1; } @@ -141,7 +142,8 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type, sqlite3_stmt *pstmt; int res; - if (stmt == NULL) { + if (p == NULL || + stmt == NULL) { errno = EINVAL; return -1; } @@ -169,6 +171,11 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type, case PROXY_DB_BIND_TYPE_INT: { int i; + if (data == NULL) { + errno = EINVAL; + return -1; + } + i = *((int *) data); res = sqlite3_bind_int(pstmt, idx, i); if (res != SQLITE_OK) { @@ -182,6 +189,11 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type, case PROXY_DB_BIND_TYPE_LONG: { long l; + if (data == NULL) { + errno = EINVAL; + return -1; + } + l = *((long *) data); res = sqlite3_bind_int(pstmt, idx, l); if (res != SQLITE_OK) { @@ -195,6 +207,11 @@ int proxy_db_bind_stmt(pool *p, const char *stmt, int idx, int type, case PROXY_DB_BIND_TYPE_TEXT: { const char *text; + if (data == NULL) { + errno = EINVAL; + return -1; + } + text = (const char *) data; res = sqlite3_bind_text(pstmt, idx, text, -1, NULL); if (res != SQLITE_OK) { @@ -228,7 +245,8 @@ int proxy_db_finish_stmt(pool *p, const char *stmt) { sqlite3_stmt *pstmt; int res; - if (stmt == NULL) { + if (p == NULL || + stmt == NULL) { errno = EINVAL; return -1; } @@ -265,7 +283,8 @@ array_header *proxy_db_exec_prepared_stmt(pool *p, const char *stmt, int readonly = FALSE, res; array_header *results = NULL; - if (stmt == NULL) { + if (p == NULL || + stmt == NULL) { errno = EINVAL; return NULL; } @@ -441,6 +460,11 @@ int proxy_db_open(pool *p, const char *table_path) { } int proxy_db_close(pool *p) { + if (p == NULL) { + errno = EINVAL; + return -1; + } + if (proxy_dbh != NULL) { sqlite3_stmt *pstmt; int res; diff --git a/t/api/conn.c b/t/api/conn.c index 034efff..a0bec62 100644 --- a/t/api/conn.c +++ b/t/api/conn.c @@ -143,7 +143,7 @@ START_TEST (conn_get_hostport_test) { } END_TEST -START_TEST (conn_get_url_test) { +START_TEST (conn_get_uri_test) { struct proxy_conn *pconn; const char *pconn_url, *url; @@ -176,7 +176,7 @@ Suite *tests_get_conn_suite(void) { tcase_add_test(testcase, conn_create_test); tcase_add_test(testcase, conn_get_addr_test); tcase_add_test(testcase, conn_get_hostport_test); - tcase_add_test(testcase, conn_get_url_test); + tcase_add_test(testcase, conn_get_uri_test); suite_add_tcase(suite, testcase); return suite; diff --git a/t/api/db.c b/t/api/db.c index 9e78685..22ff2aa 100644 --- a/t/api/db.c +++ b/t/api/db.c @@ -58,26 +58,84 @@ START_TEST (db_open_test) { END_TEST START_TEST (db_close_test) { + int res; + + res = proxy_db_close(NULL); + fail_unless(res == -1, "Failed to handle null arguments"); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); } END_TEST START_TEST (db_prepare_stmt_test) { + int res; + + res = proxy_db_prepare_stmt(NULL, NULL); + fail_unless(res == -1, "Failed to handle null arguments"); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); } END_TEST START_TEST (db_finish_stmt_test) { + int res; + const char *stmt; + + res = proxy_db_finish_stmt(NULL, NULL); + fail_unless(res == -1, "Failed to handle null arguments"); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); + + stmt = "SELECT COUNT(*) FROM table"; + res = proxy_db_finish_stmt(p, stmt); + fail_unless(res == -1, "Failed to handle unprepared statement"); + fail_unless(errno == ENOENT, "Failed to set errno to ENOENT, got %s (%d)", + strerror(errno), errno); } END_TEST START_TEST (db_bind_stmt_test) { + int res; + const char *stmt; + int idx, type; + + res = proxy_db_bind_stmt(NULL, NULL, -1, -1, NULL); + fail_unless(res == -1, "Failed to handle null arguments"); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); + + stmt = "SELECT COUNT(*) FROM table"; + idx = -1; + res = proxy_db_bind_stmt(p, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL); + fail_unless(res == -1, "Failed to handle invalid index %d", idx); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); + + idx = 1; + res = proxy_db_bind_stmt(p, stmt, idx, PROXY_DB_BIND_TYPE_INT, NULL); + fail_unless(res == -1, "Failed to handle unprepared statement"); + fail_unless(errno == ENOENT, "Failed to set errno to ENOENT, got %s (%d)", + strerror(errno), errno); } END_TEST START_TEST (db_exec_stmt_test) { + int res; + + res = proxy_db_exec_stmt(NULL, NULL, NULL); + fail_unless(res == -1, "Failed to handle null arguments"); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); } END_TEST START_TEST (db_exec_prepared_stmt_test) { + array_header *res; + + res = proxy_db_exec_prepared_stmt(NULL, NULL, NULL); + fail_unless(res == NULL, "Failed to handle null arguments"); + fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %s (%d)", + strerror(errno), errno); } END_TEST