1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-24 12:36:01 -07:00

Implement sodium_set_misuse_handler()

This commit is contained in:
Frank Denis 2017-07-17 01:00:00 +02:00
parent 9def4d9a8a
commit 0e8d7c9268
3 changed files with 40 additions and 1 deletions

View File

@ -14,6 +14,9 @@ int sodium_init(void)
/* ---- */ /* ---- */
SODIUM_EXPORT
int sodium_set_misuse_handler(void (*handler)(const char *err));
SODIUM_EXPORT SODIUM_EXPORT
void sodium_misuse(const char *err) void sodium_misuse(const char *err)
__attribute__ ((noreturn)); __attribute__ ((noreturn));

View File

@ -171,9 +171,30 @@ sodium_crit_leave(void)
#endif #endif
static void (*_misuse_handler)(const char *err);
void void
sodium_misuse(const char *err) sodium_misuse(const char *err)
{ {
(void) err; void (*handler)(const char *err);
if (sodium_crit_enter() == 0) {
handler = _misuse_handler;
if (sodium_crit_leave() == 0 && handler != NULL) {
handler(err);
}
}
abort(); abort();
} }
int
sodium_set_misuse_handler(void (*handler)(const char *err))
{
if (sodium_crit_enter() != 0) {
return -1;
}
_misuse_handler = handler;
if (sodium_crit_leave() != 0) {
return -1;
}
}

View File

@ -2,9 +2,20 @@
#define TEST_NAME "sodium_core" #define TEST_NAME "sodium_core"
#include "cmptest.h" #include "cmptest.h"
static void
misuse_handler(const char *err)
{
printf("misuse_handler(): [%s]\n", err);
exit(0);
}
int int
main(void) main(void)
{ {
sodium_set_misuse_handler(NULL);
sodium_set_misuse_handler(misuse_handler);
sodium_set_misuse_handler(NULL);
printf("%d\n", sodium_init()); printf("%d\n", sodium_init());
(void) sodium_runtime_has_neon(); (void) sodium_runtime_has_neon();
@ -15,5 +26,9 @@ main(void)
(void) sodium_runtime_has_pclmul(); (void) sodium_runtime_has_pclmul();
(void) sodium_runtime_has_aesni(); (void) sodium_runtime_has_aesni();
sodium_set_misuse_handler(misuse_handler);
sodium_misuse("TEST");
printf("Misuse handler returned\n");
return 0; return 0;
} }