Skip to content

Commit ce78512

Browse files
KGronek-Pubnubjakub-grzesiowskiXavrax
authored
Fix crashes when limit is not in correct range. (#28)
Co-authored-by: PUBNUB\jakub.grzesiowski <[email protected]> Co-authored-by: Mateusz Dahlke <[email protected]>
1 parent 15d92fa commit ce78512

7 files changed

+71
-8
lines changed

CMakeLists.txt

+7-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ set(USE_CALLBACK_API ON)
5454
set(USE_SET_DNS_SERVERS ON)
5555
set(USE_IPV6 ON)
5656
set(USE_SUBSCRIBE_EVENT_ENGINE ON)
57+
set(USE_NTF_RUNTIME_SELECTION ON)
5758

5859
if(WIN32 OR WIN64 OR MSVC)
5960
set(CUSTOM_BOOL_TYPE _Bool)
@@ -62,7 +63,7 @@ endif()
6263
FetchContent_Declare(
6364
pubnub
6465
GIT_REPOSITORY https://github.com/pubnub/c-core.git
65-
GIT_TAG feat/ee-listener-void
66+
GIT_TAG feat/runtime-api-enforcement
6667
GIT_SHALLOW TRUE
6768
GIT_PROGRESS ON
6869
SYSTEM
@@ -85,11 +86,11 @@ endif()
8586

8687
# TODO: this flag is okay until we release the Cpp-Chat as an independent library
8788
if(${ENABLE_C_ABI})
88-
target_compile_options(pubnub PUBLIC -DPUBNUB_SDK_VERSION_SUFFIX=\"/CA-Unity/0.4.2\")
89+
target_compile_options(pubnub PUBLIC -DPUBNUB_SDK_VERSION_SUFFIX=\"/CA-Unity/0.4.3\")
8990
else()
9091
target_compile_options(pubnub PUBLIC -DPUBNUB_SDK_VERSION_SUFFIX=\"/CA-Unreal/0.3.0\")
9192
endif()
92-
target_compile_options(pubnub PUBLIC -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SYNC_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
93+
target_compile_options(pubnub PUBLIC -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
9394

9495
FetchContent_Declare(
9596
json
@@ -211,19 +212,19 @@ set(SOURCES
211212

212213
add_library(pubnub-chat SHARED ${SOURCES})
213214

214-
target_compile_options(pubnub-chat PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SYNC_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
215+
target_compile_options(pubnub-chat PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
215216

216217
target_link_libraries(pubnub-chat PRIVATE -lpthread pubnub)
217218

218219
# TODO: kept for debugging purposes - should be deleted before release
219220
add_executable(pubnub-chat-example example/main.cpp)
220-
target_compile_options(pubnub-chat-example PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SYNC_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
221+
target_compile_options(pubnub-chat-example PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
221222
target_link_libraries(pubnub-chat-example PUBLIC -lpthread pubnub-chat)
222223

223224
if(${COMPILE_EXAMPLES})
224225
function(example name)
225226
add_executable(pubnub_${name} example/${name}.cpp)
226-
target_compile_options(pubnub_${name} PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SYNC_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
227+
target_compile_options(pubnub_${name} PRIVATE -DPUBNUB_NTF_RUNTIME_SELECTION -DPUBNUB_USE_SUBSCRIBE_EVENT_ENGINE=1 -DPUBNUB_USE_SUBSCRIBE_V2=1 -DPUBNUB_CALLBACK_API=1 -DPUBNUB_SET_DNS_SERVERS=1 -DPUBNUB_USE_IPV6=1)
227228
target_link_libraries(pubnub_${name} PRIVATE pubnub-chat)
228229
endfunction()
229230

src/application/channel_service.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ Channel ChannelService::get_channel(const String& channel_id) const {
146146
}
147147

148148
std::tuple<std::vector<Pubnub::Channel>, Pubnub::Page, int> ChannelService::get_channels(const Pubnub::String &filter, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const {
149+
if(limit < 0 || limit > PN_MAX_LIMIT)
150+
{
151+
throw std::invalid_argument("can't get channels, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
152+
}
153+
149154
Pubnub::String include = "custom,totalCount,channel";
150155
auto channels_response = [this, include, limit, filter, sort, page] {
151156
auto pubnub_handle = this->pubnub->lock();
@@ -451,6 +456,11 @@ void ChannelService::emit_user_mention(const Pubnub::String &channel_id, const P
451456

452457
std::vector<Pubnub::Channel> ChannelService::get_channel_suggestions(Pubnub::String text, int limit) const
453458
{
459+
if(limit < 0 || limit > PN_MAX_LIMIT)
460+
{
461+
throw std::invalid_argument("can't get channel suggestions, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
462+
}
463+
454464
auto chat_shared = this->chat_service.lock();
455465

456466
if(!chat_shared)
@@ -475,6 +485,11 @@ std::vector<Pubnub::Channel> ChannelService::get_channel_suggestions(Pubnub::Str
475485

476486
std::vector<Pubnub::Membership> ChannelService::get_user_suggestions_for_channel(const String& channel_id, ChannelDAO& channel_data, Pubnub::String text, int limit) const
477487
{
488+
if(limit < 0 || limit > PN_MAX_LIMIT)
489+
{
490+
throw std::invalid_argument("can't get users suggestions, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
491+
}
492+
478493
auto chat_shared = this->chat_service.lock();
479494

480495
if(!chat_shared)

src/application/chat_service.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef PN_CHAT_CHAT_SERVICE_HPP
22
#define PN_CHAT_CHAT_SERVICE_HPP
33

4+
#define PN_MAX_LIMIT 100
5+
46
#include "application/subscription.hpp"
57
#include "chat.hpp"
68
#include "infra/sync.hpp"

src/application/membership_service.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ MembershipService::MembershipService(ThreadSafePtr<PubNub> pubnub, std::weak_ptr
2828
{}
2929

3030
std::tuple<std::vector<Pubnub::Membership>, Pubnub::Page, int, Pubnub::String> MembershipService::get_channel_members(const String& channel_id, const ChannelDAO& channel_data, const Pubnub::String &filter, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const {
31+
32+
if(limit < 0 || limit > PN_MAX_LIMIT)
33+
{
34+
throw std::invalid_argument("can't get members, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
35+
}
36+
3137
String include_string = "custom,channel,totalCount,customChannel";
3238

3339
auto get_channel_members_response = [this, channel_id, include_string, limit, filter, sort, page] {
@@ -70,6 +76,11 @@ std::tuple<std::vector<Pubnub::Membership>, Pubnub::Page, int, Pubnub::String> M
7076
}
7177

7278
std::tuple<std::vector<Pubnub::Membership>, Pubnub::Page, int, Pubnub::String> MembershipService::get_user_memberships(const String& user_id, const UserDAO& user_data, const Pubnub::String &filter, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const {
79+
if(limit < 0 || limit > PN_MAX_LIMIT)
80+
{
81+
throw std::invalid_argument("can't get memberships, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
82+
}
83+
7384
String include_string = "totalCount,custom,channel,customChannel,channelType,status,channelStatus";
7485

7586
auto get_memberships_response = [this, user_id, include_string, filter, sort, limit, page] {
@@ -272,6 +283,12 @@ int MembershipService::get_unread_messages_count_one_channel(const Membership& m
272283
}
273284

274285
std::vector<std::tuple<Pubnub::Channel, Pubnub::Membership, int>> MembershipService::get_all_unread_messages_counts(const Pubnub::String &filter, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const {
286+
287+
if(limit < 0 || limit > PN_MAX_LIMIT)
288+
{
289+
throw std::invalid_argument("can't get unread messages counts, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
290+
}
291+
275292
auto chat_service_shared = chat_service.lock();
276293
std::vector<std::tuple<Pubnub::Channel, Pubnub::Membership, int>> return_tuples;
277294

@@ -318,6 +335,11 @@ std::vector<std::tuple<Pubnub::Channel, Pubnub::Membership, int>> MembershipServ
318335

319336
std::tuple<Pubnub::Page, int, int, std::vector<Pubnub::Membership>> MembershipService::mark_all_messages_as_read(const Pubnub::String &filter, const Pubnub::String &sort, int limit, const Pubnub::Page& page) const
320337
{
338+
if(limit < 0 || limit > PN_MAX_LIMIT)
339+
{
340+
throw std::invalid_argument("can't mark all messages as read, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
341+
}
342+
321343
auto chat_service_shared = chat_service.lock();
322344
std::tuple<Pubnub::Page, int, int, std::vector<Pubnub::Membership>> return_tuple;
323345

src/application/restrictions_service.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ Restriction RestrictionsService::get_user_restrictions(const String& user_id, co
121121

122122
std::tuple<std::vector<Pubnub::UserRestriction>, Pubnub::Page, int, Pubnub::String> RestrictionsService::get_users_restrictions(const Pubnub::String &channel_id, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const
123123
{
124+
if(limit < 0 || limit > PN_MAX_LIMIT)
125+
{
126+
throw std::invalid_argument("can't get users restrictions, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
127+
}
128+
124129
String full_channel_id = INTERNAL_MODERATION_PREFIX + channel_id;
125130

126131
auto get_restrictions_response = [this, limit, sort, page, full_channel_id] {
@@ -132,7 +137,7 @@ std::tuple<std::vector<Pubnub::UserRestriction>, Pubnub::Page, int, Pubnub::Stri
132137

133138
if(response_json.is_null())
134139
{
135-
throw std::runtime_error("can't get channel restrictions, response is incorrect");
140+
throw std::runtime_error("can't get users restrictions, response is incorrect");
136141
}
137142

138143
Json response_data_json = response_json["data"];
@@ -168,6 +173,11 @@ std::tuple<std::vector<Pubnub::UserRestriction>, Pubnub::Page, int, Pubnub::Stri
168173
}
169174

170175
Restriction RestrictionsService::get_channel_restrictions(const String& user_id, const String& channel_id, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const {
176+
if(limit < 0 || limit > PN_MAX_LIMIT)
177+
{
178+
throw std::invalid_argument("can't get channels restrictions, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
179+
}
180+
171181
String full_channel_id = INTERNAL_MODERATION_PREFIX + channel_id;
172182

173183
auto get_restrictions_response = [this, full_channel_id, limit, sort, page] {

src/application/user_service.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ User UserService::get_user(const String& user_id) const
9292
}
9393

9494
std::tuple<std::vector<Pubnub::User>, Pubnub::Page, int> UserService::get_users(const Pubnub::String &filter, const Pubnub::String &sort, int limit, const Pubnub::Page &page) const {
95-
95+
if(limit < 0 || limit > PN_MAX_LIMIT)
96+
{
97+
throw std::invalid_argument("can't get users, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
98+
}
99+
96100
Pubnub::String include = "custom,totalCount";
97101
auto users_response = [this, include, limit, filter, sort, page] {
98102
auto pubnub_handle = this->pubnub->lock();
@@ -187,6 +191,11 @@ std::shared_ptr<SubscriptionSet> UserService::stream_updates_on(Pubnub::User cal
187191

188192
std::vector<Pubnub::User> UserService::get_users_suggestions(Pubnub::String text, int limit) const
189193
{
194+
if(limit < 0 || limit > PN_MAX_LIMIT)
195+
{
196+
throw std::invalid_argument("can't get users suggestions, limit has to be within 0 - " + std::to_string(PN_MAX_LIMIT) + " range");
197+
}
198+
190199
auto chat_shared = this->chat_service.lock();
191200

192201
if(!chat_shared)

src/infra/pubnub.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ Pubnub::String PubNub::fetch_history(
568568

569569
auto fetch_history_response = pubnub_get_fetch_history(this->main_context.get());
570570

571+
//Force to clear the buffer, looks like pubnub_get_fetch_history doesn't clear it properly
572+
// TODO: werify if it is the real root cause.
573+
auto _message = pubnub_get(this->main_context.get());
574+
571575
if(!fetch_history_response.ptr)
572576
{
573577
throw std::runtime_error("Fetch history response is invalid");

0 commit comments

Comments
 (0)