Skip to content

Commit 8bdef95

Browse files
committed
OptionalNullPtr<>, deal with potential ub in samples
1 parent 42567f5 commit 8bdef95

File tree

14 files changed

+24
-21
lines changed

14 files changed

+24
-21
lines changed

include/tgbot/Optional.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
namespace TgBot {
77

8+
// Optional via boost::optional
89
template<typename T>
9-
using Optional = boost::optional<T>;
10+
using Optional = boost::optional<T>;
1011

11-
// use for: OptionalPtr<std::shared/unique_ptr<TYPE>>
12-
// for pointers, we assume optional value == nullptr (or not owned, etc)
12+
// Optional is nullptr (for std::shared/unique_ptr<> etc)
1313
template<typename T>
14-
using OptionalPtr = T;
14+
using OptionalNullPtr = T;
1515

1616
template<typename T>
17-
using Required = T;
17+
using Required = T;
1818

1919
}
2020

include/tgbot/TgTypeParser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ class TGBOT_API TgTypeParser {
899899

900900
private:
901901
inline void removeLastComma(std::string& input) const {
902-
if (!input.empty() && input.back() == ',') input.erase(input.length() - 1);
902+
if (!input.empty() && input.back() == ',') input.pop_back();
903903
}
904904

905905
template<typename T>

include/tgbot/types/InlineKeyboardButton.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ class InlineKeyboardButton {
4747
* The Web App will be able to send an arbitrary message on behalf of the user using the method Api::answerWebAppQuery.
4848
* Available only in private chats between a user and the bot.
4949
*/
50-
OptionalPtr<WebAppInfo::Ptr> webApp;
50+
OptionalNullPtr<WebAppInfo::Ptr> webApp;
5151

5252
/**
5353
* @brief Optional. An HTTPS URL used to automatically authorize the user.
5454
*
5555
* Can be used as a replacement for the [Telegram Login Widget](https://core.telegram.org/widgets/login).
5656
*/
57-
OptionalPtr<LoginUrl::Ptr> loginUrl;
57+
OptionalNullPtr<LoginUrl::Ptr> loginUrl;
5858

5959
/**
6060
* @brief Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field.
@@ -74,14 +74,14 @@ class InlineKeyboardButton {
7474
/**
7575
* @brief Optional. If set, pressing the button will prompt the user to select one of their chats of the specified type, open that chat and insert the bot's username and the specified inline query in the input field
7676
*/
77-
OptionalPtr<SwitchInlineQueryChosenChat::Ptr> switchInlineQueryChosenChat;
77+
OptionalNullPtr<SwitchInlineQueryChosenChat::Ptr> switchInlineQueryChosenChat;
7878

7979
/**
8080
* @brief Optional. Description of the game that will be launched when the user presses the button.
8181
*
8282
* NOTE: This type of button must always be the first button in the first row.
8383
*/
84-
OptionalPtr<CallbackGame::Ptr> callbackGame;
84+
OptionalNullPtr<CallbackGame::Ptr> callbackGame;
8585

8686
/**
8787
* @brief Optional. Specify True, to send a [Pay button](https://core.telegram.org/bots/api#payments).

include/tgbot/types/MessageEntity.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MessageEntity {
5555
/**
5656
* @brief Optional. For Type::TextMention only, the mentioned user
5757
*/
58-
OptionalPtr<User::Ptr> user;
58+
OptionalNullPtr<User::Ptr> user;
5959

6060
/**
6161
* @brief Optional. For Type::Pre only, the programming language of the entity text

samples/echobot-curl-client/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main() {
3333
});
3434

3535
try {
36-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
36+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3737
bot.getApi().deleteWebhook();
3838

3939
TgLongPoll longPoll(bot);

samples/echobot-setmycommands/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main() {
5959
});
6060

6161
try {
62-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
62+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
6363
bot.getApi().deleteWebhook();
6464

6565
TgLongPoll longPoll(bot);

samples/echobot-submodule/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main() {
3131
});
3232

3333
try {
34-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
34+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3535
bot.getApi().deleteWebhook();
3636

3737
TgLongPoll longPoll(bot);

samples/echobot-webhook-server/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main() {
3333
});
3434

3535
try {
36-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
36+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3737

3838
TgWebhookTcpServer webhookServer(8080, bot);
3939

samples/echobot/src/main.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ int main() {
3131
});
3232

3333
try {
34-
printf("Bot username: %s\n", bot.getApi().getMe()->username.value_or(string{"unknown"}).c_str());
34+
auto user = bot.getApi().getMe();
35+
printf("Bot name: %s, username: %s\n",
36+
user->firstName.c_str(),
37+
user->username.value_or(string{"unknown"}).c_str());
3538
bot.getApi().deleteWebhook();
3639

3740
TgLongPoll longPoll(bot);

samples/inline-keyboard/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main() {
4444
});
4545

4646
try {
47-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
47+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
4848
bot.getApi().deleteWebhook();
4949

5050
TgLongPoll longPoll(bot);

samples/photo/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int main() {
3030
});
3131

3232
try {
33-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
33+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
3434
bot.getApi().deleteWebhook();
3535

3636
TgLongPoll longPoll(bot);

samples/receive-file/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main() {
3636
});
3737

3838
try {
39-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
39+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
4040
bot.getApi().deleteWebhook();
4141

4242
TgLongPoll longPoll(bot);

samples/received-text-processing/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main() {
4646
});
4747

4848
try {
49-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
49+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
5050
bot.getApi().deleteWebhook();
5151

5252
while (true) {

samples/reply-keyboard/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main() {
7373
});
7474

7575
try {
76-
printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str());
76+
printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
7777
bot.getApi().deleteWebhook();
7878

7979
TgLongPoll longPoll(bot);

0 commit comments

Comments
 (0)