Skip to content

Commit

Permalink
Make get_avatar_url() thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeokkim committed Aug 5, 2022
1 parent 724fbca commit 2b9b855
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 24 deletions.
6 changes: 4 additions & 2 deletions include/saerom.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define SAEROM_H

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <concord/cog-utils.h>
#include <concord/discord.h>
Expand All @@ -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"

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include <pthread.h>

#include <saerom.h>
Expand Down
6 changes: 5 additions & 1 deletion src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -156,4 +158,6 @@ void sr_command_info_run(
&params,
NULL
);

free(avatar_url);
}
7 changes: 2 additions & 5 deletions src/krdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include <yxml.h>

#include <saerom.h>
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
7 changes: 3 additions & 4 deletions src/owner.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include <saerom.h>

/* | `owner` 모듈 함수... | */
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 2 additions & 5 deletions src/papago.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include <json.h>

#include <saerom.h>
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>
#include <json.h>
Expand All @@ -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(
Expand Down

0 comments on commit 2b9b855

Please sign in to comment.