diff options
author | Takashi Sakamoto <o-takashi@sakamocchi.jp> | 2022-05-05 16:16:39 +0900 |
---|---|---|
committer | 坂本 貴史 <o-takashi@sakamocchi.jp> | 2022-05-05 20:51:53 +0900 |
commit | 624a42b72c749310a42a5673f8f4449436cabda4 (patch) | |
tree | 5ab1b45363f9b2cc3e109620bc13ef0afcac9f0f | |
parent | fa04b9ba70226b036831166c9e7cb7c612be0ce8 (diff) | |
download | libhinoko-624a42b72c749310a42a5673f8f4449436cabda4.tar.gz |
fw_iso_ctx_private: code refactoring for coded error
Some missing error checks are added.
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
-rw-r--r-- | src/fw_iso_resource_auto.c | 8 | ||||
-rw-r--r-- | src/fw_iso_resource_once.c | 8 | ||||
-rw-r--r-- | src/fw_iso_resource_private.c | 14 | ||||
-rw-r--r-- | src/fw_iso_resource_private.h | 6 |
4 files changed, 30 insertions, 6 deletions
diff --git a/src/fw_iso_resource_auto.c b/src/fw_iso_resource_auto.c index e4fd783..c7a69a8 100644 --- a/src/fw_iso_resource_auto.c +++ b/src/fw_iso_resource_auto.c @@ -283,6 +283,10 @@ void hinoko_fw_iso_resource_auto_allocate_async(HinokoFwIsoResourceAuto *self, g_return_if_fail(error == NULL || *error == NULL); priv = hinoko_fw_iso_resource_auto_get_instance_private(self); + if (priv->state.fd < 0) { + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED); + return; + } g_return_if_fail(channel_candidates != NULL); g_return_if_fail(channel_candidates_count > 0); @@ -328,6 +332,10 @@ void hinoko_fw_iso_resource_auto_deallocate_async(HinokoFwIsoResourceAuto *self, g_return_if_fail(error == NULL || *error == NULL); priv = hinoko_fw_iso_resource_auto_get_instance_private(self); + if (priv->state.fd < 0) { + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED); + return; + } g_mutex_lock(&priv->mutex); diff --git a/src/fw_iso_resource_once.c b/src/fw_iso_resource_once.c index 5f46ce7..8c32378 100644 --- a/src/fw_iso_resource_once.c +++ b/src/fw_iso_resource_once.c @@ -196,6 +196,10 @@ void hinoko_fw_iso_resource_once_allocate_async(HinokoFwIsoResourceOnce *self, g_return_if_fail(bandwidth > 0); priv = hinoko_fw_iso_resource_once_get_instance_private(self); + if (priv->state.fd < 0) { + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED); + return; + } for (i = 0; i < channel_candidates_count; ++i) { if (channel_candidates[i] < 64) @@ -234,6 +238,10 @@ void hinoko_fw_iso_resource_once_deallocate_async(HinokoFwIsoResourceOnce *self, g_return_if_fail(bandwidth > 0); priv = hinoko_fw_iso_resource_once_get_instance_private(self); + if (priv->state.fd < 0) { + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED); + return; + } res.channels = 1ull << channel; res.bandwidth = bandwidth; diff --git a/src/fw_iso_resource_private.c b/src/fw_iso_resource_private.c index ae936f2..44011d8 100644 --- a/src/fw_iso_resource_private.c +++ b/src/fw_iso_resource_private.c @@ -5,7 +5,7 @@ #include <sys/stat.h> #include <fcntl.h> -static const char *const err_msgs[] = { +const char *const fw_iso_resource_err_msgs[HINOKO_FW_ISO_RESOURCE_ERROR_EVENT + 1] = { [HINOKO_FW_ISO_RESOURCE_ERROR_OPENED] = "The instance is already associated to any firewire character device", [HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED] = @@ -14,9 +14,6 @@ static const char *const err_msgs[] = { "No event to the request arrives within timeout.", }; -#define generate_local_error(error, code) \ - g_set_error_literal(error, HINOKO_FW_ISO_RESOURCE_ERROR, code, err_msgs[code]) - #define generate_file_error(error, code, format, arg) \ g_set_error(error, G_FILE_ERROR, code, format, arg) @@ -73,7 +70,7 @@ gboolean fw_iso_resource_state_open(struct fw_iso_resource_state *state, const g g_return_val_if_fail(error == NULL || *error == NULL, FALSE); if (state->fd >= 0) { - generate_local_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_OPENED); + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_OPENED); return FALSE; } @@ -170,6 +167,11 @@ gboolean fw_iso_resource_state_create_source(struct fw_iso_resource_state *state g_return_val_if_fail(source != NULL, FALSE); g_return_val_if_fail(error != NULL && *error == NULL, FALSE); + if (state->fd < 0) { + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_NOT_OPENED); + return FALSE; + } + *source = g_source_new(&funcs, sizeof(FwIsoResourceSource)); g_source_set_name(*source, "HinokoFwIsoResource"); @@ -248,7 +250,7 @@ void fw_iso_resource_waiter_wait(struct fw_iso_resource_waiter *w, HinokoFwIsoRe g_mutex_unlock(&w->mutex); if (w->handled == FALSE) - generate_local_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_TIMEOUT); + generate_coded_error(error, HINOKO_FW_ISO_RESOURCE_ERROR_TIMEOUT); else if (w->error != NULL) *error = w->error; // Delegate ownership. } diff --git a/src/fw_iso_resource_private.h b/src/fw_iso_resource_private.h index cb14293..8246e7c 100644 --- a/src/fw_iso_resource_private.h +++ b/src/fw_iso_resource_private.h @@ -13,6 +13,12 @@ #define ALLOCATED_SIGNAL_NAME "allocated" #define DEALLOCATED_SIGNAL_NAME "deallocated" +extern const char *const fw_iso_resource_err_msgs[HINOKO_FW_ISO_RESOURCE_ERROR_EVENT + 1]; + +#define generate_coded_error(error, code) \ + g_set_error_literal(error, HINOKO_FW_ISO_RESOURCE_ERROR, code, \ + fw_iso_resource_err_msgs[code]) + #define generate_syscall_error(error, errno, format, arg) \ g_set_error(error, HINOKO_FW_ISO_RESOURCE_ERROR, \ HINOKO_FW_ISO_RESOURCE_ERROR_FAILED, \ |