From 43146709b2ad6cfdd9ebfd4668a7f7399e0aee33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sat, 20 Sep 2014 20:28:03 +0200 Subject: [PATCH] Added a size argument to hex2bin. This avoids buffer overflows and related bad things. --- src/tox-weechat-commands.c | 2 +- src/tox-weechat-friend-requests.c | 2 +- src/tox-weechat-identities.c | 2 +- src/tox-weechat-utils.c | 5 ++--- src/tox-weechat-utils.h | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/tox-weechat-commands.c b/src/tox-weechat-commands.c index 70d1218..09db0fe 100644 --- a/src/tox-weechat-commands.c +++ b/src/tox-weechat-commands.c @@ -125,7 +125,7 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer, else if (argc >= 3 && (weechat_strcasecmp(argv[1], "add") == 0)) { char address[TOX_FRIEND_ADDRESS_SIZE]; - tox_weechat_hex2bin(argv[2], address); + tox_weechat_hex2bin(argv[2], TOX_FRIEND_ADDRESS_SIZE * 2, address); char *message; if (argc == 3 || strlen(argv_eol[3]) == 0) diff --git a/src/tox-weechat-friend-requests.c b/src/tox-weechat-friend-requests.c index e84bef7..bd4e34d 100644 --- a/src/tox-weechat-friend-requests.c +++ b/src/tox-weechat-friend-requests.c @@ -141,7 +141,7 @@ tox_weechat_friend_request_init_identity(struct t_tox_weechat_identity *identity json_t *json_message = json_object_get(json_request, tox_weechat_json_friend_request_key_message); - tox_weechat_hex2bin(json_string_value(json_id), client_id); + tox_weechat_hex2bin(json_string_value(json_id), TOX_CLIENT_ID_SIZE * 2, client_id); message = json_string_value(json_message); tox_weechat_friend_request_add(identity, diff --git a/src/tox-weechat-identities.c b/src/tox-weechat-identities.c index 6a6679d..b28ad3a 100644 --- a/src/tox-weechat-identities.c +++ b/src/tox-weechat-identities.c @@ -165,7 +165,7 @@ int tox_weechat_bootstrap_tox(Tox *tox, const char *address, uint16_t port, const char *public_key) { char binary_key[TOX_FRIEND_ADDRESS_SIZE]; - tox_weechat_hex2bin(public_key, binary_key); + tox_weechat_hex2bin(public_key, TOX_FRIEND_ADDRESS_SIZE * 2, binary_key); int result = tox_bootstrap_from_address(tox, address, diff --git a/src/tox-weechat-utils.c b/src/tox-weechat-utils.c index ad80b8a..76a70be 100644 --- a/src/tox-weechat-utils.c +++ b/src/tox-weechat-utils.c @@ -30,12 +30,11 @@ #include "tox-weechat-utils.h" void -tox_weechat_hex2bin(const char *hex, char *out) +tox_weechat_hex2bin(const char *hex, size_t length, char *out) { - size_t length = strlen(hex) / 2; const char *position = hex; - for (size_t i = 0; i < length; ++i) + for (size_t i = 0; i < length / 2; ++i) { sscanf(position, "%2hhx", &out[i]); position += 2; diff --git a/src/tox-weechat-utils.h b/src/tox-weechat-utils.h index d8c4fdf..3f8a1fd 100644 --- a/src/tox-weechat-utils.h +++ b/src/tox-weechat-utils.h @@ -26,7 +26,7 @@ #include void -tox_weechat_hex2bin(const char *hex, char *out); +tox_weechat_hex2bin(const char *hex, size_t length, char *out); void tox_weechat_bin2hex(const uint8_t *bin, size_t size, char *out);