From d336b7a96ec8166835cc74303eaded3ee8815233 Mon Sep 17 00:00:00 2001 From: uralm1 Date: Mon, 18 Nov 2024 13:44:48 +0500 Subject: [PATCH] README.md, OptionalNullPtr<>, potential ub in samples --- README.md | 2 +- include/tgbot/Optional.h | 10 +++++----- include/tgbot/TgTypeParser.h | 8 ++------ include/tgbot/types/InlineKeyboardButton.h | 8 ++++---- include/tgbot/types/MessageEntity.h | 2 +- samples/echobot-curl-client/src/main.cpp | 2 +- samples/echobot-setmycommands/src/main.cpp | 2 +- samples/echobot-submodule/src/main.cpp | 2 +- samples/echobot-webhook-server/src/main.cpp | 2 +- samples/echobot/src/main.cpp | 5 ++++- samples/inline-keyboard/src/main.cpp | 2 +- samples/photo/src/main.cpp | 2 +- samples/receive-file/src/main.cpp | 2 +- samples/received-text-processing/src/main.cpp | 2 +- samples/reply-keyboard/src/main.cpp | 2 +- 15 files changed, 26 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d20f8fc08..1739eca04 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ int main() { bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text); }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); TgBot::TgLongPoll longPoll(bot); while (true) { printf("Long poll started\n"); diff --git a/include/tgbot/Optional.h b/include/tgbot/Optional.h index b3563d486..a1b5abdd4 100644 --- a/include/tgbot/Optional.h +++ b/include/tgbot/Optional.h @@ -5,16 +5,16 @@ namespace TgBot { +// Optional via boost::optional template - using Optional = boost::optional; + using Optional = boost::optional; -// use for: OptionalPtr> -// for pointers, we assume optional value == nullptr (or not owned, etc) +// Optional is nullptr (for std::shared/unique_ptr<> etc) template - using OptionalPtr = T; + using OptionalNullPtr = T; template - using Required = T; + using Required = T; } diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 850c41f66..eba210c9a 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -899,7 +899,7 @@ class TGBOT_API TgTypeParser { private: inline void removeLastComma(std::string& input) const { - if (!input.empty() && input.back() == ',') input.erase(input.length() - 1); + if (!input.empty() && input.back() == ',') input.pop_back(); } template @@ -907,11 +907,7 @@ class TGBOT_API TgTypeParser { if (!value) { return; } - json += '"'; - json += varName; - json += R"(":)"; - json += *value; - json += ','; + appendToJson(json, varName, *value); } template diff --git a/include/tgbot/types/InlineKeyboardButton.h b/include/tgbot/types/InlineKeyboardButton.h index 35bad3f12..925493d6c 100644 --- a/include/tgbot/types/InlineKeyboardButton.h +++ b/include/tgbot/types/InlineKeyboardButton.h @@ -47,14 +47,14 @@ class InlineKeyboardButton { * The Web App will be able to send an arbitrary message on behalf of the user using the method Api::answerWebAppQuery. * Available only in private chats between a user and the bot. */ - OptionalPtr webApp; + OptionalNullPtr webApp; /** * @brief Optional. An HTTPS URL used to automatically authorize the user. * * Can be used as a replacement for the [Telegram Login Widget](https://core.telegram.org/widgets/login). */ - OptionalPtr loginUrl; + OptionalNullPtr loginUrl; /** * @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 { /** * @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 */ - OptionalPtr switchInlineQueryChosenChat; + OptionalNullPtr switchInlineQueryChosenChat; /** * @brief Optional. Description of the game that will be launched when the user presses the button. * * NOTE: This type of button must always be the first button in the first row. */ - OptionalPtr callbackGame; + OptionalNullPtr callbackGame; /** * @brief Optional. Specify True, to send a [Pay button](https://core.telegram.org/bots/api#payments). diff --git a/include/tgbot/types/MessageEntity.h b/include/tgbot/types/MessageEntity.h index 12da9d400..de07ee800 100644 --- a/include/tgbot/types/MessageEntity.h +++ b/include/tgbot/types/MessageEntity.h @@ -55,7 +55,7 @@ class MessageEntity { /** * @brief Optional. For Type::TextMention only, the mentioned user */ - OptionalPtr user; + OptionalNullPtr user; /** * @brief Optional. For Type::Pre only, the programming language of the entity text diff --git a/samples/echobot-curl-client/src/main.cpp b/samples/echobot-curl-client/src/main.cpp index d845bbd78..0aee5e2f2 100644 --- a/samples/echobot-curl-client/src/main.cpp +++ b/samples/echobot-curl-client/src/main.cpp @@ -33,7 +33,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/echobot-setmycommands/src/main.cpp b/samples/echobot-setmycommands/src/main.cpp index 7b0ef606c..16d6d5f98 100644 --- a/samples/echobot-setmycommands/src/main.cpp +++ b/samples/echobot-setmycommands/src/main.cpp @@ -59,7 +59,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/echobot-submodule/src/main.cpp b/samples/echobot-submodule/src/main.cpp index b12631a0e..aa15aee87 100644 --- a/samples/echobot-submodule/src/main.cpp +++ b/samples/echobot-submodule/src/main.cpp @@ -31,7 +31,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/echobot-webhook-server/src/main.cpp b/samples/echobot-webhook-server/src/main.cpp index 0e48ddc01..afc4e0771 100644 --- a/samples/echobot-webhook-server/src/main.cpp +++ b/samples/echobot-webhook-server/src/main.cpp @@ -33,7 +33,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); TgWebhookTcpServer webhookServer(8080, bot); diff --git a/samples/echobot/src/main.cpp b/samples/echobot/src/main.cpp index 7ea3abba0..9aa6a8eec 100644 --- a/samples/echobot/src/main.cpp +++ b/samples/echobot/src/main.cpp @@ -31,7 +31,10 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username.value_or(string{"unknown"}).c_str()); + auto user = bot.getApi().getMe(); + printf("Bot name: %s, username: %s\n", + user->firstName.c_str(), + user->username.value_or(string{"unknown"}).c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/inline-keyboard/src/main.cpp b/samples/inline-keyboard/src/main.cpp index 6dd4a3f30..94359d4c6 100644 --- a/samples/inline-keyboard/src/main.cpp +++ b/samples/inline-keyboard/src/main.cpp @@ -44,7 +44,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/photo/src/main.cpp b/samples/photo/src/main.cpp index 083227567..6ba74ea4c 100644 --- a/samples/photo/src/main.cpp +++ b/samples/photo/src/main.cpp @@ -30,7 +30,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/receive-file/src/main.cpp b/samples/receive-file/src/main.cpp index a17da66de..5d29c6162 100644 --- a/samples/receive-file/src/main.cpp +++ b/samples/receive-file/src/main.cpp @@ -36,7 +36,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); diff --git a/samples/received-text-processing/src/main.cpp b/samples/received-text-processing/src/main.cpp index 9cf4887a4..97a203e40 100644 --- a/samples/received-text-processing/src/main.cpp +++ b/samples/received-text-processing/src/main.cpp @@ -46,7 +46,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); while (true) { diff --git a/samples/reply-keyboard/src/main.cpp b/samples/reply-keyboard/src/main.cpp index cf7169002..fb4d122af 100644 --- a/samples/reply-keyboard/src/main.cpp +++ b/samples/reply-keyboard/src/main.cpp @@ -73,7 +73,7 @@ int main() { }); try { - printf("Bot username: %s\n", bot.getApi().getMe()->username->c_str()); + printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot);