summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2017-10-10 23:20:26 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2017-10-15 05:52:39 +0200
commitb6837e36a34a1e9e7fafea822516e61285e3c09c (patch)
tree4c84e9a08d56e013bbb92df8a0b43e4b7f1cd4b9
parent2e86ab3a8704462c55245370ebdce9f6b134792d (diff)
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
-rw-r--r--src/db_hlr.c2
-rw-r--r--tests/db/db_test.c19
2 files changed, 18 insertions, 3 deletions
diff --git a/src/db_hlr.c b/src/db_hlr.c
index 71f682d..cf6e4f8 100644
--- a/src/db_hlr.c
+++ b/src/db_hlr.c
@@ -379,6 +379,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..591418b 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) { \