Skip to content

Commit

Permalink
coap_threadsafe.c: Fix NPE in coap_delete_resource()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Dec 20, 2023
1 parent c162187 commit e710528
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/coap3/coap_mutex_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ int coap_lock_lock_func(coap_lock_t *lock);
} \
} while (0)

#define coap_lock_check_locked(s)
#define coap_lock_check_locked(s) {}

# endif /* ! COAP_THREAD_RECURSIVE_CHECK */

Expand All @@ -352,7 +352,7 @@ typedef coap_mutex_t coap_lock_t;
#define coap_lock_unlock(s)
#define coap_lock_init(s)
#define coap_lock_being_freed(s,failed)
#define coap_lock_check_locked(s)
#define coap_lock_check_locked(s) {}
#define coap_lock_callback(s,func) func
#define coap_lock_callback_ret(r,s,func) ret = func
#define coap_lock_invert(s,func,f) func
Expand Down
4 changes: 3 additions & 1 deletion src/coap_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,10 @@ coap_delete_resource(coap_context_t *context, coap_resource_t *resource) {
if (!resource)
return 0;

coap_lock_check_locked(context);
context = resource->context;
if (context) {
coap_lock_check_locked(context);
}

if (resource->is_unknown) {
if (context && context->unknown_resource == resource) {
Expand Down
16 changes: 13 additions & 3 deletions src/coap_threadsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,17 @@ int
coap_delete_resource(coap_context_t *context, coap_resource_t *resource) {
int ret;

coap_lock_lock(context, return 0);
ret = coap_delete_resource_locked(context, resource);
coap_lock_unlock(context);
if (!resource)
return 0;

context = resource->context;
if (context) {
coap_lock_lock(context, return 0);
ret = coap_delete_resource_locked(context, resource);
coap_lock_unlock(context);
} else {
ret = coap_delete_resource_locked(context, resource);
}
return ret;
}

Expand Down Expand Up @@ -485,6 +493,8 @@ coap_delete_oscore_recipient(coap_context_t *context,
coap_bin_const_t *recipient_id) {
int ret;

if (!context || !recipient_id)
return 0;
coap_lock_lock(context, return 0);
ret = coap_delete_oscore_recipient_locked(context, recipient_id);
coap_lock_unlock(context);
Expand Down

0 comments on commit e710528

Please sign in to comment.