summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYann Ylavic <ylavic@apache.org>2023-04-13 09:14:08 +0000
committerYann Ylavic <ylavic@apache.org>2023-04-13 09:14:08 +0000
commit0a67578d96545785feb5a987cbe2dbd184127d79 (patch)
treeb655889cb6db297f1108e4feb95f9a2f8a611270
parentc6ae8671557243be706633b537cb7ec879b89b35 (diff)
downloadapr-0a67578d96545785feb5a987cbe2dbd184127d79.tar.gz
apr_base64: Don't fault with assert() when NDEBUG is defined, abort() directly.
Per Evgeny: """ 1) The debug implementation of an assert() may print a diagnostic message, for example to stderr. A caller of the library function may not be ready for this to happen when using a non-debug version of the library. 2) The actual destination of the message seems to be implementation-defined. For example, in Windows-based applications this may show a message box [1], which is probably even more unexpected for the user of the library. 3) Undefining NDEBUG before other headers may silently cause unexpected effects if any of those headers make some decisions based on the NDEBUG value, which isn't an entirely unreasonable thing to expect. """ We want to always fault on failure though, so define and use our own APR__ASSERT() macro which calls abort() directly when NDEBUG is defined. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1909117 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--encoding/apr_base64.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/encoding/apr_base64.c b/encoding/apr_base64.c
index d8e54a4ae..448085843 100644
--- a/encoding/apr_base64.c
+++ b/encoding/apr_base64.c
@@ -20,14 +20,27 @@
* ugly 'len' functions, which is quite a nasty cost.
*/
-#undef NDEBUG /* always abort() on assert()ion failure */
-#include <assert.h>
-
#include "apr_base64.h"
#if APR_CHARSET_EBCDIC
#include "apr_xlate.h"
#endif /* APR_CHARSET_EBCDIC */
+/* APR__ASSERT() to always abort() on failure (no output when NDEBUG) */
+#ifndef NDEBUG
+#include <assert.h>
+#define APR__ASSERT(cond) assert(cond)
+#else
+#include "apr.h"
+#if APR_HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#define APR__ASSERT(cond) do { \
+ if (!(cond)) { \
+ abort(); \
+ } \
+} while (0)
+#endif
+
/* Above APR_BASE64_ENCODE_MAX length the encoding can't fit in an int >= 0 */
#define APR_BASE64_ENCODE_MAX 1610612733
@@ -124,7 +137,7 @@ APR_DECLARE(int) apr_base64_decode_len(const char *bufcoded)
bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
- assert(nprbytes <= APR_BASE64_DECODE_MAX);
+ APR__ASSERT(nprbytes <= APR_BASE64_DECODE_MAX);
return (int)(((nprbytes + 3u) / 4u) * 3u + 1u);
}
@@ -161,7 +174,7 @@ APR_DECLARE(int) apr_base64_decode_binary(unsigned char *bufplain,
bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
- assert(nprbytes <= APR_BASE64_DECODE_MAX);
+ APR__ASSERT(nprbytes <= APR_BASE64_DECODE_MAX);
nbytesdecoded = (int)(((nprbytes + 3u) / 4u) * 3u);
bufout = (unsigned char *) bufplain;
@@ -206,7 +219,7 @@ static const char basis_64[] =
APR_DECLARE(int) apr_base64_encode_len(int len)
{
- assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
+ APR__ASSERT(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
return ((len + 2) / 3 * 4) + 1;
}
@@ -219,7 +232,7 @@ APR_DECLARE(int) apr_base64_encode(char *encoded, const char *string, int len)
int i;
char *p;
- assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
+ APR__ASSERT(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
p = encoded;
for (i = 0; i < len - 2; i += 3) {
@@ -258,7 +271,7 @@ APR_DECLARE(int) apr_base64_encode_binary(char *encoded,
int i;
char *p;
- assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
+ APR__ASSERT(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
p = encoded;
for (i = 0; i < len - 2; i += 3) {
@@ -292,7 +305,7 @@ APR_DECLARE(char *) apr_pbase64_encode(apr_pool_t *p, const char *string)
char *encoded;
apr_size_t len = strlen(string);
- assert(len <= (apr_size_t)APR_BASE64_ENCODE_MAX);
+ APR__ASSERT(len <= (apr_size_t)APR_BASE64_ENCODE_MAX);
encoded = (char *) apr_palloc(p, apr_base64_encode_len((int)len));
apr_base64_encode(encoded, string, (int)len);