Skip to content

Commit

Permalink
fix free on not initialised PubNub (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Nov 14, 2023
1 parent c1975f2 commit 43856ef
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
21 changes: 13 additions & 8 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: c-core
schema: 1
version: "4.6.1"
version: "4.6.2"
scm: github.com/pubnub/c-core
changelog:
- date: 2023-11-14
version: v4.6.2
changes:
- type: bug
text: "Fix `pubnub_free()` function on not initialised PubNub that can cause exceptions/undefined behaviours."
- date: 2023-11-08
version: v4.6.1
changes:
Expand Down Expand Up @@ -753,7 +758,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down Expand Up @@ -819,7 +824,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down Expand Up @@ -885,7 +890,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down Expand Up @@ -947,7 +952,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1008,7 +1013,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1064,7 +1069,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1117,7 +1122,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.6.1
location: https://github.com/pubnub/c-core/releases/tag/v4.6.2
requires:
-
name: "miniz"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v4.6.2
November 14 2023

#### Fixed
- Fix `pubnub_free()` function on not initialised PubNub that can cause exceptions/undefined behaviours.

## v4.6.1
November 08 2023

Expand Down
20 changes: 12 additions & 8 deletions core/pubnub_alloc_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ void pballoc_free_at_last(pubnub_t *pb)

int pubnub_free(pubnub_t *pb)
{
int result = -1;

PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));

PUBNUB_LOG_TRACE("pubnub_free(%p)\n", pb);

if (pb->state == PBS_NULL) {
PUBNUB_LOG_TRACE("pubnub_free(%p) not initialized - freeing...\n", pb);
free(pb);

return 0;
}

pubnub_mutex_lock(pb->monitor);
pbnc_stop(pb, PNR_CANCELLED);
if (PBS_IDLE == pb->state) {
Expand All @@ -80,12 +85,11 @@ int pubnub_free(pubnub_t *pb)
pubnub_mutex_unlock(pb->monitor);
pballoc_free_at_last(pb);
#endif
result = 0;
}
else {
PUBNUB_LOG_TRACE("pubnub_free(%p) pb->state=%d\n", pb, pb->state);
pubnub_mutex_unlock(pb->monitor);
return 0;
}

return result;
PUBNUB_LOG_TRACE("pubnub_free(%p) pb->state=%d\n", pb, pb->state);
pubnub_mutex_unlock(pb->monitor);

return -1;
}
21 changes: 13 additions & 8 deletions core/pubnub_alloc_std.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,18 @@ void pballoc_free_at_last(pubnub_t* pb)

int pubnub_free(pubnub_t* pb)
{
int result = -1;

PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));

PUBNUB_LOG_TRACE("pubnub_free(%p)\n", pb);

if (pb->state == PBS_NULL) {
PUBNUB_LOG_TRACE("pubnub_free(%p) not initialized - freeing...\n", pb);
remove_allocated(pb);
free(pb);

return 0;
}

pubnub_mutex_lock(pb->monitor);
pbnc_stop(pb, PNR_CANCELLED);
if (PBS_IDLE == pb->state) {
Expand All @@ -156,12 +162,11 @@ int pubnub_free(pubnub_t* pb)
pballoc_free_at_last(pb);
#endif

result = 0;
}
else {
PUBNUB_LOG_TRACE("pubnub_free(%p) pb->state=%d\n", pb, pb->state);
pubnub_mutex_unlock(pb->monitor);
return 0;
}

return result;
PUBNUB_LOG_TRACE("pubnub_free(%p) pb->state=%d\n", pb, pb->state);
pubnub_mutex_unlock(pb->monitor);

return -1;
}
4 changes: 4 additions & 0 deletions core/pubnub_core_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4547,6 +4547,10 @@ Ensure(single_context_pubnub, gzip_bad_compression_format)
equals(PNR_BAD_COMPRESSION_FORMAT));
}

Ensure(single_context_pubnub, wont_fail_on_freed) {
// Do nothing, just let alloc and free happen
}

/* Verify ASSERT gets fired */
Ensure(single_context_pubnub, illegal_context_fires_assert)
{
Expand Down
2 changes: 1 addition & 1 deletion core/pubnub_version_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define INC_PUBNUB_VERSION_INTERNAL


#define PUBNUB_SDK_VERSION "4.6.1"
#define PUBNUB_SDK_VERSION "4.6.2"


#endif /* !defined INC_PUBNUB_VERSION_INTERNAL */

0 comments on commit 43856ef

Please sign in to comment.