>From 0475d8f5feae03c43b54cc0f6787d259a3008da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20B=C5=99ezina?= Date: Mon, 12 May 2014 22:54:01 +0200 Subject: [PATCH 6/8] SBUS: Return empty string if a string getter returns NULL In line with getters never returning errors, a getter should return an empty string instead of NULL in case a string-like property in SSSD is not set. --- src/sbus/sbus_codegen | 5 ++++- src/tests/sbus_codegen_tests.c | 13 +++++++++++++ src/tests/sbus_codegen_tests.xml | 3 +++ src/tests/sbus_codegen_tests_generated.c | 11 ++++++++++- src/tests/sbus_codegen_tests_generated.h | 2 ++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/sbus/sbus_codegen b/src/sbus/sbus_codegen index a8f91b784ffaa5a17620792df7285df4b508749f..b671e939fcc7c29a2ba424e785e967111d32323c 100755 --- a/src/sbus/sbus_codegen +++ b/src/sbus/sbus_codegen @@ -318,7 +318,10 @@ def source_getter_invoker(prop): out(");") out("") - out(" out_val = prop_val;") + if prop.type == "s": + out(" out_val = prop_val == NULL ? \"\" : prop_val;") + else: + out(" out_val = prop_val;") if prop.is_array: out(" return sbus_request_return_array_as_variant(dbus_req, %s, (uint8_t*)out_val, prop_len, sizeof(%s));", prop.dbus_constant, prop.sssd_type) else: diff --git a/src/tests/sbus_codegen_tests.c b/src/tests/sbus_codegen_tests.c index 57ba5bebc965426bf6b2f34bd79a38baf023c4b6..cb0f9207bcc3d0cc8623d447414be6355b5203c6 100644 --- a/src/tests/sbus_codegen_tests.c +++ b/src/tests/sbus_codegen_tests.c @@ -434,6 +434,13 @@ void pilot_get_objpath_handler(struct sbus_request *dbus_req, *path_val = pilot_path; } +void pilot_get_null_string_handler(struct sbus_request *dbus_req, + void *instance_data, + const char **string_val) +{ + *string_val = NULL; +} + #define array_getter_body(in, out, outlen) do { \ ck_assert(dbus_req != NULL); \ ck_assert(out != NULL); \ @@ -540,6 +547,7 @@ struct test_pilot pilot_iface = { .test_pilot_get_double = pilot_get_double_handler, .test_pilot_get_string = pilot_get_string_handler, .test_pilot_get_object_path = pilot_get_objpath_handler, + .test_pilot_get_null_string = pilot_get_null_string_handler, .test_pilot_get_byte_array = pilot_get_byte_array_handler, .test_pilot_get_int16_array = pilot_get_int16_array_handler, @@ -844,6 +852,11 @@ START_TEST(test_get_basic_types) call_get(client, "/test/leela", test_pilot_meta.name, "object_path", DBUS_TYPE_OBJECT_PATH, &path_val); ck_assert_str_eq(path_val, pilot_path); + + /* If a string getter returns NULL, the caller should receive "" */ + call_get(client, "/test/leela", test_pilot_meta.name, "null_string", + DBUS_TYPE_STRING, &string_val); + ck_assert_str_eq(string_val, ""); } END_TEST diff --git a/src/tests/sbus_codegen_tests.xml b/src/tests/sbus_codegen_tests.xml index 74037cc4002198f8c8d4b6a8c12edbd34fe338ef..c44f38305a9a2e6eb1fb402a45ce6002e103639d 100755 --- a/src/tests/sbus_codegen_tests.xml +++ b/src/tests/sbus_codegen_tests.xml @@ -120,6 +120,9 @@ + + + diff --git a/src/tests/sbus_codegen_tests_generated.c b/src/tests/sbus_codegen_tests_generated.c index 4ace0365044ac1edec3c8774da2231c82e054017..211ad8967f4c3cf3d00c6b53004c42dd15fbe715 100644 --- a/src/tests/sbus_codegen_tests_generated.c +++ b/src/tests/sbus_codegen_tests_generated.c @@ -363,6 +363,15 @@ const struct sbus_property_meta test_pilot__properties[] = { NULL, /* no invoker */ }, { + "null_string", /* name */ + "s", /* type */ + SBUS_PROPERTY_READABLE, + offsetof(struct test_pilot, test_pilot_get_null_string), + invoke_get_s, + 0, /* not writable */ + NULL, /* no invoker */ + }, + { "byte_array", /* name */ "ay", /* type */ SBUS_PROPERTY_READABLE, @@ -764,7 +773,7 @@ static int invoke_get_s(struct sbus_request *dbus_req, void *function_ptr) (handler)(dbus_req, dbus_req->intf->instance_data, &prop_val); - out_val = prop_val; + out_val = prop_val == NULL ? "" : prop_val; return sbus_request_return_as_variant(dbus_req, DBUS_TYPE_STRING, &out_val); } diff --git a/src/tests/sbus_codegen_tests_generated.h b/src/tests/sbus_codegen_tests_generated.h index 9599091dd0627c7af84fb211858d918d9128ecf0..7dc817edf9bac331ec8d5647721cb4fd9e737d55 100644 --- a/src/tests/sbus_codegen_tests_generated.h +++ b/src/tests/sbus_codegen_tests_generated.h @@ -35,6 +35,7 @@ #define TEST_PILOT_DOUBLE "double" #define TEST_PILOT_STRING "string" #define TEST_PILOT_OBJECT_PATH "object_path" +#define TEST_PILOT_NULL_STRING "null_string" #define TEST_PILOT_BYTE_ARRAY "byte_array" #define TEST_PILOT_INT16_ARRAY "int16_array" #define TEST_PILOT_UINT16_ARRAY "uint16_array" @@ -96,6 +97,7 @@ struct test_pilot { void (*test_pilot_get_double)(struct sbus_request *, void *data, double *); void (*test_pilot_get_string)(struct sbus_request *, void *data, const char * *); void (*test_pilot_get_object_path)(struct sbus_request *, void *data, const char * *); + void (*test_pilot_get_null_string)(struct sbus_request *, void *data, const char * *); void (*test_pilot_get_byte_array)(struct sbus_request *, void *data, uint8_t * *, int *); void (*test_pilot_get_int16_array)(struct sbus_request *, void *data, int16_t * *, int *); void (*test_pilot_get_uint16_array)(struct sbus_request *, void *data, uint16_t * *, int *); -- 1.9.0