Skip to content

Commit

Permalink
Fix nyx_device_close memory deallocations
Browse files Browse the repository at this point in the history
- free name and description using g_free (allocated using g_strndup)
- cleanup logic by caching whole nyx_device struct instead of
  picking individual members

Open-webOS-DCO-1.0-Signed-off-by: Janne Kiiski <janne.kiiski@palm.com>

Change-Id: I9ce32cd8dd80daf93f260cd54aabe7df58835196
  • Loading branch information
Janne Kiiski committed Feb 21, 2013
1 parent 6b6ab2a commit c6f3c82
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/device/nyx_device_impl.c
Expand Up @@ -322,43 +322,32 @@ nyx_error_t nyx_device_close(nyx_device_handle_t handle)
CHECK_DEVICE(d);

/*
* we need to cache method hash table as it might be
* used during the close, we will destroy it after
* a call to close.
* cleanup device local members after call to close because members
* might be used during the close.
*
* need to setup copy from device because call to close will deallocate
* device (d) structure (cant access device members through pointer d)
*/
GHashTable* method_hash_table = (GHashTable*)d->method_hash_table;
char* name = d->name;
char* desc = d->description;
nyx_device_t cache;
memcpy(&cache, d, sizeof(cache));

/*
* Don't know if necessary but since we are deallocating
* the device structure during the call to close method
* let's first cache the pointer to close method.
*/
void* module_ptr = d->module_ptr;
nyx_close_function_t close_ptr = d->close_ptr;
nyx_error_t error = close_ptr(d);
nyx_error_t error = cache.close_ptr(d);

/*
* let's destroy the method hash table if necessary.
* do rest of the cleanup using cache
*/
if (method_hash_table) {
g_hash_table_destroy (method_hash_table);
}
if (name) {
free (name);
name = NULL;
}
if (desc) {
free (desc);
desc = NULL;

if (NULL != cache.method_hash_table) {
g_hash_table_destroy ((GHashTable*)cache.method_hash_table);
}

g_free(cache.name);
g_free(cache.description);

/*
* We are done so, we can close the module.
*/
dlclose(module_ptr);

dlclose(cache.module_ptr);

return error;
}
Expand Down

0 comments on commit c6f3c82

Please sign in to comment.