Commit 8ecb3b22 authored by Shawn Webb's avatar Shawn Webb
Browse files

Cleanup and new approach to sanity checking



Instead of using asserts, which the compiler may leave out, do proper
sanity checking with normal conditionals.
Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
parent 7041b121
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -51,6 +50,7 @@ struct _pushover_message { ...@@ -51,6 +50,7 @@ struct _pushover_message {
static char *msg_to_str(pushover_ctx_t *, pushover_message_t *, CURL *); static char *msg_to_str(pushover_ctx_t *, pushover_message_t *, CURL *);
static size_t pushover_curl_write_data(void *, size_t, size_t, void *); static size_t pushover_curl_write_data(void *, size_t, size_t, void *);
static bool _msg_can_submit(pushover_ctx_t *, pushover_message_t *);
EXPORTED_SYM EXPORTED_SYM
pushover_ctx_t * pushover_ctx_t *
...@@ -72,7 +72,12 @@ pushover_init_ctx(const char *token) ...@@ -72,7 +72,12 @@ pushover_init_ctx(const char *token)
if (token != NULL) { if (token != NULL) {
res->psh_token = strdup(token); res->psh_token = strdup(token);
assert(res->psh_token != NULL); if (res->psh_token == NULL) {
free(res->psh_uri);
free(res);
res = NULL;
goto out;
}
} }
res->psh_version = LIBPUSHOVER_VERSION; res->psh_version = LIBPUSHOVER_VERSION;
...@@ -105,9 +110,13 @@ bool ...@@ -105,9 +110,13 @@ bool
pushover_set_uri(pushover_ctx_t *ctx, const char *uri) pushover_set_uri(pushover_ctx_t *ctx, const char *uri)
{ {
assert(ctx != NULL); if (ctx == NULL || uri == NULL) {
assert(uri != NULL); return (false);
assert(ctx->psh_uri == NULL); }
if (ctx->psh_uri != NULL) {
free(ctx->psh_uri);
}
ctx->psh_uri = strdup(uri); ctx->psh_uri = strdup(uri);
return (ctx->psh_uri != NULL); return (ctx->psh_uri != NULL);
...@@ -118,9 +127,13 @@ bool ...@@ -118,9 +127,13 @@ bool
pushover_set_token(pushover_ctx_t *ctx, const char *token) pushover_set_token(pushover_ctx_t *ctx, const char *token)
{ {
assert(ctx != NULL); if (ctx == NULL || token == NULL) {
assert(token != NULL); return (false);
assert(ctx->psh_token == NULL); }
if (ctx->psh_token != NULL) {
free(ctx->psh_token);
}
ctx->psh_token = strdup(token); ctx->psh_token = strdup(token);
return (ctx->psh_token != NULL); return (ctx->psh_token != NULL);
...@@ -181,9 +194,11 @@ bool ...@@ -181,9 +194,11 @@ bool
pushover_message_set_msg(pushover_message_t *msg, char *data) pushover_message_set_msg(pushover_message_t *msg, char *data)
{ {
assert(msg != NULL); if (msg == NULL || data == NULL) {
assert(data != NULL); return (false);
assert(msg->psh_msg == NULL); }
free(msg->psh_msg);
msg->psh_msg = strdup(data); msg->psh_msg = strdup(data);
return (msg->psh_msg != NULL); return (msg->psh_msg != NULL);
...@@ -194,9 +209,11 @@ bool ...@@ -194,9 +209,11 @@ bool
pushover_message_set_dest(pushover_message_t *msg, char *dest) pushover_message_set_dest(pushover_message_t *msg, char *dest)
{ {
assert(msg != NULL); if (msg == NULL || dest == NULL) {
assert(dest != NULL); return (false);
assert(msg->psh_dest == NULL); }
free(msg->psh_dest);
msg->psh_dest = strdup(dest); msg->psh_dest = strdup(dest);
return (msg->psh_dest != NULL); return (msg->psh_dest != NULL);
...@@ -207,9 +224,11 @@ bool ...@@ -207,9 +224,11 @@ bool
pushover_message_set_title(pushover_message_t *msg, char *title) pushover_message_set_title(pushover_message_t *msg, char *title)
{ {
assert(msg != NULL); if (msg == NULL || title == NULL) {
assert(title != NULL); return (false);
assert(msg->psh_title == NULL); }
free(msg->psh_title);
msg->psh_title = strdup(title); msg->psh_title = strdup(title);
return (msg->psh_title != NULL); return (msg->psh_title != NULL);
...@@ -220,9 +239,11 @@ bool ...@@ -220,9 +239,11 @@ bool
pushover_message_set_device(pushover_message_t *msg, char *device) pushover_message_set_device(pushover_message_t *msg, char *device)
{ {
assert(msg != NULL); if (msg == NULL || device == NULL) {
assert(device != NULL); return (false);
assert(msg->psh_device == NULL); }
free(msg->psh_device);
msg->psh_device = strdup(device); msg->psh_device = strdup(device);
return (msg->psh_device != NULL); return (msg->psh_device != NULL);
...@@ -234,7 +255,9 @@ pushover_message_set_priority(pushover_message_t *msg, ...@@ -234,7 +255,9 @@ pushover_message_set_priority(pushover_message_t *msg,
pushover_priority_t prio) pushover_priority_t prio)
{ {
assert(msg != NULL); if (msg == NULL) {
return (false);
}
if (!pushover_message_priority_sane(prio)) { if (!pushover_message_priority_sane(prio)) {
return (false); return (false);
...@@ -254,14 +277,9 @@ pushover_submit_message(pushover_ctx_t *ctx, pushover_message_t *msg) ...@@ -254,14 +277,9 @@ pushover_submit_message(pushover_ctx_t *ctx, pushover_message_t *msg)
CURL *curl; CURL *curl;
bool res; bool res;
assert(ctx != NULL); if (!_msg_can_submit(ctx, msg)) {
assert(ctx->psh_uri != NULL); return (false);
assert(ctx->psh_token != NULL); }
assert(msg != NULL);
assert(msg->psh_dest != NULL);
assert(msg->psh_msg != NULL);
assert(pushover_message_priority_sane(msg->psh_priority));
res = false; res = false;
post_str = NULL; post_str = NULL;
...@@ -297,8 +315,9 @@ msg_to_str(pushover_ctx_t *ctx, pushover_message_t *msg, CURL *curl) ...@@ -297,8 +315,9 @@ msg_to_str(pushover_ctx_t *ctx, pushover_message_t *msg, CURL *curl)
struct sbuf *sb; struct sbuf *sb;
char *p, *res; char *p, *res;
assert(ctx != NULL); if (ctx == NULL || msg == NULL) {
assert(msg != NULL); return (NULL);
}
sb = sbuf_new_auto(); sb = sbuf_new_auto();
if (sb == NULL) { if (sb == NULL) {
...@@ -386,6 +405,26 @@ pushover_curl_write_data(void *buffer, size_t sz, size_t nmemb, ...@@ -386,6 +405,26 @@ pushover_curl_write_data(void *buffer, size_t sz, size_t nmemb,
return (sz * nmemb); return (sz * nmemb);
} }
static bool
_msg_can_submit(pushover_ctx_t *ctx, pushover_message_t *msg)
{
if (ctx == NULL || ctx->psh_uri == NULL || ctx->psh_token == NULL) {
return (false);
}
if (msg == NULL || msg->psh_dest == NULL || msg->psh_msg == NULL) {
return (false);
}
if (!pushover_message_priority_sane(msg->psh_priority)) {
return (false);
}
return (true);
}
__attribute__((constructor)) __attribute__((constructor))
static void static void
init_libpushover(void) init_libpushover(void)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment