From 96782fa5f76076a0d8e6d9a8932d5fe7b8d5352d Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 10 Oct 2017 23:20:26 +0200 Subject: fix db_subscr_get_by_*(): clear output data; test in db_test.c db_subscr_get_by_*() failed to clear the out-param struct, meaning that data could remain in a struct even though it is not present in the database. Always zero out the struct before writing to it. Adjust the db_test to catch this error by writing "-invalid-data-" to each struct before running db get functions. Change-Id: I038bd437452c87841d709fcdd5ac30ab1356b2db --- src/db_hlr.c | 2 ++ tests/db/db_test.c | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/db_hlr.c b/src/db_hlr.c index 612de86..97cd7ed 100644 --- a/src/db_hlr.c +++ b/src/db_hlr.c @@ -378,6 +378,8 @@ static int db_sel(struct db_context *dbc, sqlite3_stmt *stmt, struct hlr_subscri if (!subscr) goto out; + *subscr = (struct hlr_subscriber){}; + /* obtain the various columns */ subscr->id = sqlite3_column_int64(stmt, 0); SL3_TXT(subscr->imsi, stmt, 1); diff --git a/tests/db/db_test.c b/tests/db/db_test.c index db3318c..d49983e 100644 --- a/tests/db/db_test.c +++ b/tests/db/db_test.c @@ -34,6 +34,19 @@ #define comment(fmt, args...) fprintf(stderr, "\n--- " fmt "\n\n", ## args); #define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__); +#define fill_invalid(x) _fill_invalid(&x, sizeof(x)) +static void _fill_invalid(void *dest, size_t size) +{ + uint8_t *pos = dest; + size_t remain = size; + int wrote = 0; + do { + remain -= wrote; + pos += wrote; + wrote = snprintf((void*)pos, remain, "-invalid-data"); + } while (wrote <= remain); +} + /* Perform a function call and verbosely assert that its return value is as expected. * The return code is then available in g_rc. */ #define ASSERT_RC(call, expect_rc) \ @@ -53,7 +66,7 @@ #define ASSERT_SEL(by, val, expect_rc) \ do { \ int rc; \ - g_subscr = (struct hlr_subscriber){}; \ + fill_invalid(g_subscr); \ fprintf(stderr, "db_subscr_get_by_" #by "(dbc, " #val ", &g_subscr) --> " \ #expect_rc "\n"); \ rc = db_subscr_get_by_##by(dbc, val, &g_subscr); \ @@ -71,8 +84,8 @@ * The results are then available in g_aud2g and g_aud3g. */ #define ASSERT_SEL_AUD(imsi, expect_rc, expect_id) \ do { \ - g_aud2g = (struct osmo_sub_auth_data){}; \ - g_aud3g = (struct osmo_sub_auth_data){}; \ + fill_invalid(g_aud2g); \ + fill_invalid(g_aud3g); \ g_id = 0; \ ASSERT_RC(db_get_auth_data(dbc, imsi, &g_aud2g, &g_aud3g, &g_id), expect_rc); \ if (!g_rc) { \ -- cgit v1.2.3