From 4cad95ec182ab9f3d28f37cfab9fb28ccb596cd7 Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Tue, 21 Feb 2023 14:32:42 +0100 Subject: actions: change pointer to offset --- actions.c | 12 +++++------- gluten.h | 2 +- tests/unit/test_actions.c | 22 +++++----------------- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/actions.c b/actions.c index 6a7f55c..bb603af 100644 --- a/actions.c +++ b/actions.c @@ -211,14 +211,12 @@ int do_actions(void *data, struct action actions[], unsigned int n_actions, int resp.id = id; resp.flags = 0; resp.error = 0; - if (actions[i].ret.type == IMMEDIATE) { + if (actions[i].ret.type == IMMEDIATE) resp.val = actions[i].ret.value; - } else if (actions[i].ret.value_p != NULL) { - resp.val = *(actions[i].ret.value_p); - } else { - fprintf(stderr, "empty reference for the return value"); - return -1; - } + else + memcpy(&resp.val, (uint16_t *)data + + actions[i].ret.value_off, + sizeof(resp.val)); if (send_target(&resp, notifyfd) == -1) return -1; diff --git a/gluten.h b/gluten.h index c004220..760f82b 100644 --- a/gluten.h +++ b/gluten.h @@ -77,7 +77,7 @@ struct act_return { enum value_type type; union { int64_t value; - int64_t *value_p; + uint16_t value_off; }; }; diff --git a/tests/unit/test_actions.c b/tests/unit/test_actions.c index 2bf92d4..e648063 100644 --- a/tests/unit/test_actions.c +++ b/tests/unit/test_actions.c @@ -253,33 +253,22 @@ END_TEST START_TEST(test_act_return_ref) { int64_t v = 2; + uint16_t offset = 4; struct action actions[] = { { .type = A_RETURN, - .ret = { .type = REFERENCE, .value_p = &v }, + .ret = { .type = REFERENCE, .value_off = offset }, }, }; - int ret = do_actions(NULL, actions, sizeof(actions) / sizeof(actions[0]), -1, + memcpy((uint16_t *)&tmp_data + offset, &v, sizeof(v)); + + int ret = do_actions(&tmp_data, actions, sizeof(actions) / sizeof(actions[0]), -1, notifyfd, req.id); ck_assert_msg(ret == 0, strerror(errno)); check_target_result(v, 0, false); } END_TEST -START_TEST(test_act_return_empty_ref) -{ - struct action actions[] = { - { - .type = A_RETURN, - .ret = { .type = REFERENCE, .value_p = NULL }, - }, - }; - int ret = do_actions(NULL, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); - ck_assert_int_eq(ret, -1); -} -END_TEST - START_TEST(test_act_call) { struct action actions[] = { @@ -365,7 +354,6 @@ Suite *action_call_suite(void) tcase_set_timeout(ret, timeout); tcase_add_test(ret, test_act_return); tcase_add_test(ret, test_act_return_ref); - tcase_add_test(ret, test_act_return_empty_ref); suite_add_tcase(s, ret); block = tcase_create("a_block"); -- cgit v1.2.3