From 2b9b85574d8d0296d297692fe5c4c5d4561a3b78 Mon Sep 17 00:00:00 2001 From: jdeokkim Date: Fri, 5 Aug 2022 12:48:16 +0900 Subject: [PATCH] Make `get_avatar_url()` thread-safe --- include/saerom.h | 6 ++++-- src/config.c | 3 --- src/info.c | 6 +++++- src/krdict.c | 7 ++----- src/owner.c | 7 +++---- src/papago.c | 7 ++----- src/utils.c | 6 ++---- 7 files changed, 18 insertions(+), 24 deletions(-) diff --git a/include/saerom.h b/include/saerom.h index 9157417..f47dec5 100644 --- a/include/saerom.h +++ b/include/saerom.h @@ -19,6 +19,8 @@ #define SAEROM_H #include +#include +#include #include #include @@ -29,7 +31,7 @@ /* | 매크로 정의... | */ #define APPLICATION_NAME "jdeokkim/saerom" -#define APPLICATION_VERSION "v0.4.1" +#define APPLICATION_VERSION "v0.4.2" #define APPLICATION_DESCRIPTION "A C99 Discord bot for Korean learning servers." #define APPLICATION_PROJECT_URL "https://github.com/jdeokkim/saerom" @@ -239,7 +241,7 @@ void sr_command_papago_handle_error( /* | `utils` 모듈 함수... | */ /* 주어진 사용자의 프로필 사진 URL을 반환한다. */ -const char *get_avatar_url(const struct discord_user *user); +char *get_avatar_url(const struct discord_user *user); /* 표준 입력 스트림 (`stdin`)에서 명령어를 입력받는다. */ void *read_input(void *arg); diff --git a/src/config.c b/src/config.c index 8de351b..b6c450e 100644 --- a/src/config.c +++ b/src/config.c @@ -15,9 +15,6 @@ along with this program. If not, see . */ -#include -#include - #include #include diff --git a/src/info.c b/src/info.c index fa94e0d..c9912f3 100644 --- a/src/info.c +++ b/src/info.c @@ -120,6 +120,8 @@ void sr_command_info_run( }, }; + char *avatar_url = get_avatar_url(discord_get_self(client)); + struct discord_embed embeds[] = { { .title = APPLICATION_NAME, @@ -130,7 +132,7 @@ void sr_command_info_run( .text = "✨" }, .thumbnail = &(struct discord_embed_thumbnail) { - .url = (char *) get_avatar_url(discord_get_self(client)) + .url = get_avatar_url(discord_get_self(client)) }, .fields = &(struct discord_embed_fields) { .size = sizeof(fields) / sizeof(*fields), @@ -156,4 +158,6 @@ void sr_command_info_run( ¶ms, NULL ); + + free(avatar_url); } \ No newline at end of file diff --git a/src/krdict.c b/src/krdict.c index dc9b41b..62e22c0 100644 --- a/src/krdict.c +++ b/src/krdict.c @@ -15,9 +15,6 @@ along with this program. If not, see . */ -#include -#include - #include #include @@ -218,7 +215,7 @@ void sr_command_krdict_create_request( curl_easy_setopt(request.easy, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(request.easy, CURLOPT_POST, 1); - struct sr_command_context *context = malloc(sizeof(struct sr_command_context)); + struct sr_command_context *context = malloc(sizeof(*context)); context->event = discord_claim(client, event); @@ -283,7 +280,7 @@ int sr_command_krdict_parse_data( ) { if (xml.len == 0 || buffer == NULL) return 0; - yxml_t *parser = malloc(sizeof(yxml_t) + xml.len); + yxml_t *parser = malloc(sizeof(*parser) + xml.len); yxml_init(parser, parser + 1, xml.len); diff --git a/src/owner.c b/src/owner.c index 526e270..d5f8587 100644 --- a/src/owner.c +++ b/src/owner.c @@ -15,9 +15,6 @@ along with this program. If not, see . */ -#include -#include - #include /* | `owner` 모듈 함수... | */ @@ -176,8 +173,10 @@ void sr_command_msg_run( sizeof(struct discord_interaction) ); + const size_t token_length = strlen(event->token) + 1; + event_clone->id = event->id; - event_clone->token = malloc(strlen(event->token) + 1); + event_clone->token = malloc(token_length * sizeof(*(event->token))); strcpy(event_clone->token, event->token); diff --git a/src/papago.c b/src/papago.c index 13c9c32..6e7b756 100644 --- a/src/papago.c +++ b/src/papago.c @@ -15,9 +15,6 @@ along with this program. If not, see . */ -#include -#include - #include #include @@ -223,10 +220,10 @@ void sr_command_papago_run( request.header = curl_slist_append(request.header, buffer); - struct sr_command_context *context = malloc(sizeof(struct sr_command_context)); + struct sr_command_context *context = malloc(sizeof(*context)); context->event = discord_claim(client, event); - context->data = malloc((strlen(text) + 1) * sizeof(char)); + context->data = malloc((strlen(text) + 1) * sizeof(*text)); strcpy(context->data, text); diff --git a/src/utils.c b/src/utils.c index c2462ec..9269d35 100644 --- a/src/utils.c +++ b/src/utils.c @@ -16,8 +16,6 @@ */ #include -#include -#include #include #include @@ -31,8 +29,8 @@ /* | `utils` 모듈 함수... | */ /* 주어진 사용자의 프로필 사진 URL을 반환한다. */ -const char *get_avatar_url(const struct discord_user *user) { - static char buffer[MAX_STRING_SIZE]; +char *get_avatar_url(const struct discord_user *user) { + char *buffer = malloc(MAX_STRING_SIZE * sizeof(*buffer)); if (user->avatar != NULL) { snprintf(