From 7fdf709d14347f6e937d5c0f97ee6c0f5958b30d Mon Sep 17 00:00:00 2001 From: Vinicius Costa Gomes Date: Thu, 15 Apr 2010 15:40:54 -0300 Subject: [PATCH] obexd: simplify the finding of a service We can use memcmp0() when searching for a specific attribute without worrying about NULL pointers. --- obexd/src/mimetype.c | 12 ------------ obexd/src/obex.c | 11 +++++++++++ obexd/src/obex.h | 3 +++ obexd/src/service.c | 11 ++--------- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/obexd/src/mimetype.c b/obexd/src/mimetype.c index 27acf5ff7..4ec7665db 100644 --- a/obexd/src/mimetype.c +++ b/obexd/src/mimetype.c @@ -46,18 +46,6 @@ struct io_watch { gpointer user_data; }; -/* Just thin wrapper around memcmp to deal with NULL values */ -static int memcmp0(const void *a, const void *b, size_t n) -{ - if (a == NULL) - return -(a != b); - - if (b == NULL) - return a != b; - - return memcmp(a, b, n); -} - void obex_object_set_io_flags(gpointer object, int flags, int err) { GSList *l; diff --git a/obexd/src/obex.c b/obexd/src/obex.c index 9fc7d3791..cd52fb99a 100644 --- a/obexd/src/obex.c +++ b/obexd/src/obex.c @@ -1197,3 +1197,14 @@ ssize_t obex_aparam_read(struct obex_session *os, return -EBADR; } + +int memcmp0(const void *a, const void *b, size_t n) +{ + if (a == NULL) + return -(a != b); + + if (b == NULL) + return a != b; + + return memcmp(a, b, n); +} diff --git a/obexd/src/obex.h b/obexd/src/obex.h index 6c9ba4ba9..dd5343c31 100644 --- a/obexd/src/obex.h +++ b/obexd/src/obex.h @@ -63,3 +63,6 @@ int tty_init(gint service, const gchar *folder, const gchar *capability, gboolean symlinks, const gchar *devnode); gint obex_tty_session_stop(void); void tty_closed(void); + +/* Just a thin wrapper around memcmp to deal with NULL values */ +int memcmp0(const void *a, const void *b, size_t n); diff --git a/obexd/src/service.c b/obexd/src/service.c index 7093f30c3..ad0889d85 100644 --- a/obexd/src/service.c +++ b/obexd/src/service.c @@ -47,17 +47,10 @@ struct obex_service_driver *obex_service_driver_find(GSList *list, for (l = list; l; l = l->next) { struct obex_service_driver *driver = l->data; - if (driver->who && who && - (driver->who_size != who_size || - memcmp(driver->who, who, who_size) != 0)) + if (memcmp0(who, driver->who, who_size)) continue; - if (driver->target == NULL && target == NULL) - return driver; - - if (driver->target && target && - driver->target_size == target_size && - memcmp(driver->target, target, target_size) == 0) + if (memcmp0(target, driver->target, target_size) == 0) return driver; } -- 2.47.3