From 2b86f9bde1bd3aafd8b46c677fdb7c008a12e3e3 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 1 Aug 2014 10:30:43 +1000 Subject: [PATCH] Add a safe strcmp helper function --- src/libckpool.c | 21 +++++++++++++++++++++ src/libckpool.h | 1 + 2 files changed, 22 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index 72df31d8..0a478649 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1061,6 +1061,27 @@ void b58tobin(char *b58bin, const char *b58) } } +/* Does a safe string comparison tolerating zero length and NULL strings */ +int safecmp(const char *a, const char *b) +{ + int lena, lenb; + + if (unlikely(!a || !b)) { + if (a != b) + return -1; + return 0; + } + lena = strlen(a); + lenb = strlen(b); + if (unlikely(!lena || !lenb)) { + if (lena != lenb) + return -1; + return 0; + } + return (strcmp(a, b)); +} + + static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* Return a malloced string of *src encoded into mime base 64 */ diff --git a/src/libckpool.h b/src/libckpool.h index 888bacdb..08d30242 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -414,6 +414,7 @@ bool _hex2bin(void *p, const void *vhexstr, size_t len, const char *file, const #define hex2bin(p, vhexstr, len) _hex2bin(p, vhexstr, len, __FILE__, __func__, __LINE__) char *http_base64(const char *src); void b58tobin(char *b58bin, const char *b58); +int safecmp(const char *a, const char *b); void address_to_pubkeytxn(char *pkh, const char *addr); int ser_number(uchar *s, int32_t val);