diff options
author | Pete Batard <pbatard@gmail.com> | 2010-10-18 16:31:13 +0100 |
---|---|---|
committer | Pete Batard <pbatard@gmail.com> | 2010-10-18 16:31:13 +0100 |
commit | 9f5df35b251a1961de0695caf0cf36f8e6ac2504 (patch) | |
tree | 0cef98640621c5cfd6672c88ddaa030faf762ea6 | |
parent | 67ece4d74b9e9537c67ac91bfcb2137ede81c79a (diff) | |
download | libusb-9f5df35b251a1961de0695caf0cf36f8e6ac2504.tar.gz |
fixed crash on add pollfds during init on Linux
* also set hash init to return proper libusb codes
-rw-r--r-- | libusb/core.c | 24 | ||||
-rw-r--r-- | libusb/io.c | 3 |
2 files changed, 17 insertions, 10 deletions
diff --git a/libusb/core.c b/libusb/core.c index cb0038d..a9fece5 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -1558,12 +1558,12 @@ static int usbi_htab_create(struct libusb_context *ctx, unsigned long nel) { if (ctx == NULL) { usbi_err(ctx, "null context"); - return 0; + return LIBUSB_ERROR_INVALID_PARAM; } if (ctx->htab_table != NULL) { usbi_err(ctx, "hash table already allocated"); - return 0; + return LIBUSB_ERROR_ACCESS; } // Create a mutex @@ -1582,10 +1582,10 @@ static int usbi_htab_create(struct libusb_context *ctx, unsigned long nel) ctx->htab_table = (struct htab_entry*)calloc(ctx->htab_size + 1, sizeof(struct htab_entry)); if (ctx->htab_table == NULL) { usbi_err(ctx, "could not allocate space for hash table"); - return 0; + return LIBUSB_ERROR_NO_MEM; } - return 1; + return LIBUSB_SUCCESS; } /* After using the hash table it has to be destroyed. */ @@ -1758,6 +1758,12 @@ int API_EXPORTED libusb_init(libusb_context **context) usbi_dbg(""); + // Some of the backends (Linux) require the following to be setup + // before calling backend init + usbi_mutex_init(&ctx->pollfds_lock, NULL); + usbi_mutex_init(&ctx->pollfd_modify_lock, NULL); + list_init(&ctx->pollfds); + if (usbi_backend->init) { r = usbi_backend->init(ctx); if (r) @@ -1780,6 +1786,13 @@ int API_EXPORTED libusb_init(libusb_context **context) goto err_destroy_mutex; } + r = usbi_htab_create(ctx, DEFAULT_HTAB_SIZE); + if (r < 0) { + usbi_backend->exit(ctx); + usbi_io_exit(ctx); + goto err_destroy_mutex; + } + if (context) { *context = ctx; } else if (!usbi_default_context) { @@ -1788,9 +1801,6 @@ int API_EXPORTED libusb_init(libusb_context **context) default_context_refcnt++; } - // TODO: check return - usbi_htab_create(ctx, DEFAULT_HTAB_SIZE); - usbi_mutex_static_unlock(&default_context_lock); return 0; diff --git a/libusb/io.c b/libusb/io.c index 194b392..0c6d833 100644 --- a/libusb/io.c +++ b/libusb/io.c @@ -1021,8 +1021,6 @@ int usbi_io_init(struct libusb_context *ctx) } #endif usbi_mutex_init(&ctx->flying_transfers_lock, NULL); - usbi_mutex_init(&ctx->pollfds_lock, NULL); - usbi_mutex_init(&ctx->pollfd_modify_lock, NULL); #if defined(OS_WINDOWS) usbi_mutex_init(&ctx->events_lock, NULL); #else @@ -1032,7 +1030,6 @@ int usbi_io_init(struct libusb_context *ctx) usbi_mutex_init(&ctx->event_waiters_lock, NULL); usbi_cond_init(&ctx->event_waiters_cond, NULL); list_init(&ctx->flying_transfers); - list_init(&ctx->pollfds); /* FIXME should use an eventfd on kernels that support it */ r = usbi_pipe(ctx->ctrl_pipe); |